Automatic Copyright Year update after running gdb/copyright.py
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.rust / simple.exp
CommitLineData
88b9d363 1# Copyright (C) 2016-2022 Free Software Foundation, Inc.
67218854
TT
2
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 3 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16# Test expression parsing and evaluation that requires Rust compiler.
17
18load_lib rust-support.exp
19if {[skip_rust_tests]} {
20 continue
21}
22
23standard_testfile .rs
5b362f04 24if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug rust}]} {
67218854
TT
25 return -1
26}
27
28set line [gdb_get_line_number "set breakpoint here"]
29if {![runto ${srcfile}:$line]} {
5b362f04 30 untested "could not run to breakpoint"
67218854
TT
31 return -1
32}
33
34gdb_test "print a" " = \\(\\)"
35gdb_test "ptype a" " = \\(\\)"
cdf5a07c 36gdb_test "print sizeof(a)" " = 0"
67218854
TT
37
38gdb_test "print b" " = \\\[\\\]"
39gdb_test "ptype b" " = \\\[i32; 0\\\]"
40gdb_test "print *(&b as *const \[i32; 0\])" " = \\\[\\\]"
41gdb_test "print *(&b as *const \[i32; 0_0\])" " = \\\[\\\]"
42
43gdb_test "print c" " = 99"
44gdb_test "ptype c" " = i32"
cdf5a07c 45gdb_test "print sizeof(c)" " = 4"
67218854
TT
46
47gdb_test "print c = 87" " = \\(\\)"
7d874253 48gdb_test "print c" " = 87" "print after assignment"
67218854 49gdb_test "print c += 3" " = \\(\\)"
7d874253 50gdb_test "print c" " = 90" "print after plus assignment"
67218854 51gdb_test "print c -= 90" " = \\(\\)"
7d874253 52gdb_test "print c" " = 0" "print after minus assignment"
67218854
TT
53gdb_test "print *&c" " = 0"
54gdb_test "print *(&c as &i32)" " = 0"
55gdb_test "print *(&c as *const i32)" " = 0"
56gdb_test "print *(&c as *mut i32)" " = 0"
73fc52c4 57gdb_test "ptype &c as *mut i32" "\\*mut i32"
67218854 58
01af5e0d
TT
59gdb_test "print/c f\[0\]" " = 104 'h'"
60
67218854
TT
61gdb_test "print j" " = simple::Unit"
62gdb_test "ptype j" " = struct simple::Unit"
12df5c00
TT
63gdb_test "print j2" " = simple::Unit"
64gdb_test "ptype j2" " = struct simple::Unit"
67218854 65gdb_test "print simple::Unit" " = simple::Unit"
12df5c00 66gdb_test "print simple::Unit{}" " = simple::Unit"
22f80c0f 67gdb_test "print simple::Unit{23}" "'}', '\.\.', or identifier expected"
67218854 68
45320ffa
TT
69gdb_test "print f" " = \"hi bob\""
70gdb_test "print fslice" " = \"bob\""
71gdb_test "print &f\[3..\]" " = \"bob\""
72
73fc52c4
TT
73gdb_test "print g" " = \\(\\*mut \\\[u8; 6\\\]\\) $hex b\"hi bob\""
74gdb_test "ptype g" " = \\*mut \\\[u8; 6\\\]"
67218854
TT
75
76gdb_test "print v" " = simple::Something::Three"
77gdb_test_sequence "ptype v" "" {
78 " = enum simple::Something \\{"
79 " One,"
80 " Two,"
81 " Three,"
82 "\\}"
83}
84
85gdb_test "print w" " = \\\[1, 2, 3, 4\\\]"
86gdb_test "ptype w" " = \\\[i32; 4\\\]"
87gdb_test "print w\[2\]" " = 3"
88gdb_test "print w\[2\] @ 2" " = \\\[3, 4\\\]"
42d94011 89gdb_test "print w_ptr\[2\]" " = 3"
67218854
TT
90gdb_test "print fromslice" " = 3"
91gdb_test "print slice\[0\]" " = 3"
458620aa 92gdb_test "print (slice as &\[i32\])\[0\]" " = 3"
67218854 93
22f80c0f
TT
94gdb_test "print slice as \[i32; 73.9\]" "integer expected"
95
b3e3859b
TT
96gdb_test_sequence "ptype slice" "" {
97 " = struct &\\\[i32\\\] \\{"
73fc52c4 98 " data_ptr: \\*mut i32,"
b3e3859b
TT
99 " length: usize,"
100 "\\}"
101}
102gdb_test_sequence "ptype &slice\[..\]" "" {
103 " = struct &\\\[i32\\\] \\{"
73fc52c4 104 " data_ptr: \\*mut i32,"
b3e3859b
TT
105 " length: usize,"
106 "\\}"
107}
108gdb_test_sequence "ptype &b\[..\]" "" {
109 " = struct &\\\[\\*gdb\\*\\\] \\{"
73fc52c4 110 " data_ptr: \\*mut i32,"
b3e3859b
TT
111 " length: usize,"
112 "\\}"
113}
114
67218854
TT
115gdb_test "print x" " = \\(23, 25\\.5\\)"
116gdb_test "ptype x" " = \\(i32, f64\\)"
117gdb_test "print x as (i32,f64)" " = \\(23, 25\\.5\\)"
118
119gdb_test "print y" " = simple::HiBob \\{field1: 7, field2: 8\\}"
120gdb_test_sequence "ptype y" "" {
121 " = struct simple::HiBob \\{"
122 " field1: i32,"
123 " field2: u64,"
124 "\\}"
125}
126gdb_test "print y.field2" " = 8"
127
128gdb_test "print z" " = simple::ByeBob \\(7, 8\\)"
129gdb_test_sequence "ptype z" "" {
130 " = struct simple::ByeBob \\("
131 " i32,"
132 " u64,"
133 "\\)"
134}
135gdb_test "print z.1" " = 8"
136
22f80c0f
TT
137# Some error checks.
138gdb_test "print z.1_0" \
139 "'_' not allowed in integers in anonymous field references"
140gdb_test "print z.mut" "field name expected"
141
51a789c3
MG
142gdb_test "print univariant" " = simple::Univariant::Foo{a: 1}"
143gdb_test "print univariant.a" " = 1"
144gdb_test "print univariant_anon" " = simple::UnivariantAnon::Foo\\(1\\)"
145gdb_test "print univariant_anon.0" " = 1"
4a3fe98f
TT
146gdb_test "print univariant_anon.sss" \
147 "Attempting to access named field sss of tuple variant simple::UnivariantAnon::Foo, which has only anonymous fields"
51a789c3 148
f0fd41c1
TT
149gdb_test_sequence "ptype simple::Univariant" "" {
150 "type = enum simple::Univariant \\{"
151 " Foo\\{a: u8\\},"
152 "\\}"
153}
154
155gdb_test_sequence "ptype simple::UnivariantAnon" "" {
156 "type = enum simple::UnivariantAnon \\{"
157 " Foo\\(u8\\),"
158 "\\}"
159}
160
67218854
TT
161gdb_test_sequence "ptype simple::ByeBob" "" {
162 " = struct simple::ByeBob \\("
163 " i32,"
164 " u64,"
165 "\\)"
166}
167gdb_test "print simple::ByeBob(0xff, 5)" \
168 " = simple::ByeBob \\(255, 5\\)"
169gdb_test "print simple::ByeBob\{field1: 0xff, field2:5\}" \
170 "Struct expression applied to non-struct type"
171
172gdb_test "print simple::HiBob(0xff, 5)" \
173 "Type simple::HiBob is not a tuple struct"
8880f2a9
TT
174gdb_test "print sizeof(simple::HiBob)" " = \[0-9\]+"
175gdb_test "print simple::HiBob + 5" \
3cbc7ac3 176 "Attempt to use a type name as an expression"
67218854
TT
177gdb_test "print nosuchsymbol" \
178 "No symbol 'nosuchsymbol' in current context"
179
92630041
TT
180gdb_test "print simple::HiBob{field1, field2}" \
181 " = simple::HiBob \\{field1: 77, field2: 88\\}"
182
50146e70
TT
183gdb_test "print simple::HiBob{field1: 99, .. y}" \
184 " = simple::HiBob \\{field1: 99, field2: 8\\}"
185
67218854
TT
186gdb_test "print e" " = simple::MoreComplicated::Two\\(73\\)"
187gdb_test "print e2" \
188 " = simple::MoreComplicated::Four\\{this: true, is: 8, a: 109 'm', struct_: 100, variant: 10\\}"
cdf5a07c 189gdb_test "print sizeof(e)" " = 24"
67218854
TT
190gdb_test_sequence "ptype e" "" {
191 " = enum simple::MoreComplicated \\{"
192 " One,"
193 " Two\\(i32\\),"
194 " Three\\(simple::HiBob\\),"
195 " Four\\{this: bool, is: u8, a: char, struct_: u64, variant: u32\\},"
196 "\\}"
197}
198
22f80c0f
TT
199# Test a parser error.
200gdb_test "print sizeof e" "'\\(' expected"
201
67218854
TT
202gdb_test "print e.0" " = 73"
203gdb_test "print e.1" \
204 "Cannot access field 1 of variant simple::MoreComplicated::Two, there are only 1 fields"
205gdb_test "print e.foo" \
206 "Attempting to access named field foo of tuple variant simple::MoreComplicated::Two, which has only anonymous fields"
207
208gdb_test "print e2.variant" " = 10"
209gdb_test "print e2.notexist" \
210 "Could not find field notexist of struct variant simple::MoreComplicated::Four"
211gdb_test "print e2.0" \
212 "Variant simple::MoreComplicated::Four is not a tuple variant"
213
a50faaf6
TV
214set pass_pattern " = simple::SpaceSaver::Nothing"
215set xfail_pattern " = simple::SpaceSaver::Thebox\\($decimal, 0x0\\)"
216gdb_test_multiple "print k" "" {
217 -re "\[\r\n\]*(?:$pass_pattern)\[\r\n\]+$gdb_prompt $" {
218 pass $gdb_test_name
219 }
220 -re "\[\r\n\]*(?:$xfail_pattern)\[\r\n\]+$gdb_prompt $" {
221 xfail $gdb_test_name
222 }
223}
67218854
TT
224gdb_test "print l" " = simple::SpaceSaver::Thebox\\(9, $hex\\)"
225gdb_test "print *l.1" " = 1729"
226
227gdb_test "print diff2(3, 7)" " = -4"
228gdb_test "print self::diff2(8, 9)" " = -1"
229gdb_test "print ::diff2(23, -23)" " = 46"
230
231gdb_test "ptype diff2" "fn \\(i32, i32\\) -> i32"
921d8f54 232gdb_test "ptype empty" "fn \\(\\)"
67218854
TT
233
234gdb_test "print (diff2 as fn(i32, i32) -> i32)(19, -2)" " = 21"
235
22f80c0f
TT
236gdb_test "print diff2(73, 74 75" "',' or '\\\)' expected"
237gdb_test "print (diff2 as fn i32, i32) -> i32)(19, -2)" "'\\\(' expected"
238gdb_test "print (diff2 as fn (i32, i32) i32)(19, -2)" "'->' expected"
239
45320ffa 240gdb_test "print \"hello rust\"" " = \"hello rust.*\""
67218854 241gdb_test "print \"hello" "Unexpected EOF in string"
45320ffa 242gdb_test "print r##\"hello \" rust\"##" " = \"hello \\\\\" rust.*\""
67218854
TT
243gdb_test "print r\"hello" "Unexpected EOF in string"
244gdb_test "print r###\"###hello\"" "Unexpected EOF in string"
245gdb_test "print r###\"###hello\"##" "Unexpected EOF in string"
246gdb_test "print r###\"hello###" "Unexpected EOF in string"
247
248gdb_test "print 0..5" " = .*::ops::Range.* \\{start: 0, end: 5\\}"
6873858b 249gdb_test "print 0..=5" " = .*::ops::RangeInclusive.* \\{start: 0, end: 5\\}"
67218854 250gdb_test "print ..5" " = .*::ops::RangeTo.* \\{end: 5\\}"
6873858b 251gdb_test "print ..=5" " = .*::ops::RangeToInclusive.* \\{end: 5\\}"
67218854
TT
252gdb_test "print 5.." " = .*::ops::RangeFrom.* \\{start: 5\\}"
253gdb_test "print .." " = .*::ops::RangeFull"
254
a50faaf6 255set pass_pattern \
fdffd6f4 256 " = core::option::Option<\[a-z\]+::string::String>::Some\\(\[a-z\]+::string::String .*"
326afb72
TT
257set xfail_pattern \
258 "( = <error reading variable>|That operation is not available on .*)"
a50faaf6
TV
259gdb_test_multiple "print str_some" "" {
260 -re "\[\r\n\]*(?:$pass_pattern)\[\r\n\]+$gdb_prompt $" {
261 pass $gdb_test_name
262 }
263 -re "\[\r\n\]*(?:$xfail_pattern)\[\r\n\]+$gdb_prompt $" {
264 xfail $gdb_test_name
265 }
266}
267
268set pass_pattern " = core::option::Option<\[a-z\]+::string::String>::None"
269gdb_test_multiple "print str_none" "" {
270 -re "\[\r\n\]*(?:$pass_pattern)\[\r\n\]+$gdb_prompt $" {
271 pass $gdb_test_name
272 }
273 -re "\[\r\n\]*(?:$xfail_pattern)\[\r\n\]+$gdb_prompt $" {
274 xfail $gdb_test_name
275 }
276}
277
c9317f21
TT
278gdb_test "print int_some" " = core::option::Option<u8>::Some\\(1\\)"
279gdb_test "print int_none" " = core::option::Option<u8>::None"
9b243007
TT
280# The result expressions are a bit lax here, to handle the fact that
281# the output varies between Rust versions. Mostly we just want to
282# check for the presence "Option", "Box", "u8", and either "Some" or
283# "None".
284gdb_test "print box_some" \
285 " = core::option::Option<\[a-z:\]*Box<u8.*>>::Some\\(.*\\)"
286gdb_test "print box_none" \
287 " = core::option::Option<\[a-z:\]*Box<u8.*>>::None"
a50faaf6
TV
288
289set pass_pattern \
fdffd6f4 290 " = simple::NonZeroOptimized::Value\\(\[a-z\]+::string::String .*"
a50faaf6
TV
291gdb_test_multiple "print custom_some" "" {
292 -re "\[\r\n\]*(?:$pass_pattern)\[\r\n\]+$gdb_prompt $" {
293 pass $gdb_test_name
294 }
295 -re "\[\r\n\]*(?:$xfail_pattern)\[\r\n\]+$gdb_prompt $" {
296 xfail $gdb_test_name
297 }
298}
299
300set pass_pattern " = simple::NonZeroOptimized::Empty"
301gdb_test_multiple "print custom_none" "" {
302 -re "\[\r\n\]*(?:$pass_pattern)\[\r\n\]+$gdb_prompt $" {
303 pass $gdb_test_name
304 }
305 -re "\[\r\n\]*(?:$xfail_pattern)\[\r\n\]+$gdb_prompt $" {
306 xfail $gdb_test_name
307 }
308}
fccb08f8 309
80062eb9
AB
310gdb_test "print st" \
311 " = simple::StringAtOffset {field1: \"hello\", field2: 1, field3: \"world\"}"
312
67218854 313proc test_one_slice {svar length base range} {
7d874253
TT
314 with_test_prefix $range {
315 global hex
67218854 316
7d874253 317 set result " = &\\\[.*\\\] \\{data_ptr: $hex, length: $length\\}"
67218854 318
7d874253
TT
319 gdb_test "print $svar" $result
320 gdb_test "print &${base}\[${range}\]" $result
321 }
67218854
TT
322}
323
324test_one_slice slice 1 w 2..3
6873858b 325test_one_slice slice 1 w 2..=2
67218854 326test_one_slice slice2 1 slice 0..1
6873858b 327test_one_slice slice2 1 slice 0..=0
67218854
TT
328
329test_one_slice all1 4 w ..
330test_one_slice all2 1 slice ..
331
332test_one_slice from1 3 w 1..
333test_one_slice from2 0 slice 1..
334
335test_one_slice to1 3 w ..3
6873858b 336test_one_slice to1 3 w ..=2
67218854 337test_one_slice to2 1 slice ..1
6873858b 338test_one_slice to2 1 slice ..=0
67218854
TT
339
340gdb_test "print w\[2..3\]" "Can't take slice of array without '&'"
341
342
343gdb_test_sequence "complete print y.f" "" \
344 {"print y.field1" "print y.field2"}
345gdb_test_sequence "complete print y." "" \
346 {"print y.field1" "print y.field2"}
347
348# Unimplemented, but we can at least test the parser productions.
349gdb_test "print (1,2,3)" "Tuple expressions not supported yet"
350gdb_test "print (1,)" "Tuple expressions not supported yet"
351gdb_test "print (1)" " = 1"
352
22f80c0f
TT
353# Test a syntax error in tuple expressions.
354gdb_test "print (1,2,," "unexpected token"
355gdb_test "print (1,2 8" "',' or '\\\)' expected"
356
67218854 357gdb_test "print 23..97.0" "Range expression with different types"
51a789c3
MG
358
359gdb_test "print (*parametrized.next.val)" \
9b243007 360 " = simple::ParametrizedStruct<i32> {next: simple::ParametrizedEnum<\[a-z:\]*Box<simple::ParametrizedStruct<i32>.*>>::Empty, value: 1}"
51a789c3 361gdb_test "print parametrized.next.val" \
73fc52c4 362 " = \\(\\*mut simple::ParametrizedStruct<i32>\\) $hex"
51a789c3 363gdb_test "print parametrized" \
9b243007 364 " = simple::ParametrizedStruct<i32> \\{next: simple::ParametrizedEnum<\[a-z:\]*Box<simple::ParametrizedStruct<i32>.*>>::Val\\{val: $hex\\}, value: 0\\}"
65547233 365
a33ccfc7 366gdb_test_sequence "ptype/o SimpleLayout" "" {
fbb46296
LS
367 "/\\* offset | size \\*/ type = struct simple::SimpleLayout {"
368 "/\\* 0 | 2 \\*/ f1: u16,"
369 "/\\* 2 | 2 \\*/ f2: u16,"
a33ccfc7 370 ""
fbb46296
LS
371 " /\\* total size \\(bytes\\): 4 \\*/"
372 " }"
a33ccfc7
TT
373}
374
08410482
DE
375gdb_test "print nonzero_offset" " = simple::EnumWithNonzeroOffset {a: core::option::Option<u8>::Some\\(1\\), b: core::option::Option<u8>::None}"
376
098b2108
TT
377# PR rust/23626 - this used to crash. Note that the results are
378# fairly lax because most existing versions of Rust (those before the
379# DW_TAG_variant patches) do not emit what gdb wants here; and there
380# was little point fixing gdb to cope with these cases as the fixed
381# compilers will be available soon
382gdb_test "print empty_enum_value" \
383 " = simple::EmptyEnum.*"
384gdb_test "ptype empty_enum_value" "simple::EmptyEnum.*"
385# Just make sure these don't crash, for the same reason.
386gdb_test "print empty_enum_value.0" ""
387gdb_test "print empty_enum_value.something" ""
388
65547233
TT
389load_lib gdb-python.exp
390if {[skip_python_tests]} {
391 continue
392}
393
394gdb_test "python print(gdb.lookup_type('simple::HiBob'))" "simple::HiBob"
1acda803
TT
395
396gdb_test_no_output "python e = gdb.parse_and_eval('e')" \
397 "get value of e for python"
398gdb_test "python print(len(e.type.fields()))" "2"
399gdb_test "python print(e.type.fields()\[0\].artificial)" "True"
400gdb_test "python print(e.type.fields()\[1\].name)" "Two"
401
402gdb_test "python print(e.type.dynamic)" "False"
08cc37dd
TT
403
404# Before LLVM 8, the rust compiler would emit two types named
405# "simple::MoreComplicated" -- the C-like "underlying" enum type and
406# the Rust enum. lookup_type seems to get the former, which isn't
407# very useful. With later versions of LLVM, this test works
408# correctly.
409set v [split [rust_llvm_version] .]
410if {[lindex $v 0] >= 8} {
411 gdb_test "python print(gdb.lookup_type('simple::MoreComplicated').dynamic)" \
412 "True"
413}
This page took 0.776633 seconds and 4 git commands to generate.