Make Rust error message mention the field name
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.rust / simple.exp
CommitLineData
e2882c85 1# Copyright (C) 2016-2018 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" " = \\(\\)"
48gdb_test "print c" " = 87"
49gdb_test "print c += 3" " = \\(\\)"
50gdb_test "print c" " = 90"
51gdb_test "print c -= 90" " = \\(\\)"
52gdb_test "print c" " = 0"
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"
57
01af5e0d
TT
58gdb_test "print/c f\[0\]" " = 104 'h'"
59
67218854
TT
60gdb_test "print j" " = simple::Unit"
61gdb_test "ptype j" " = struct simple::Unit"
12df5c00
TT
62gdb_test "print j2" " = simple::Unit"
63gdb_test "ptype j2" " = struct simple::Unit"
67218854 64gdb_test "print simple::Unit" " = simple::Unit"
12df5c00 65gdb_test "print simple::Unit{}" " = simple::Unit"
67218854 66
45320ffa
TT
67gdb_test "print f" " = \"hi bob\""
68gdb_test "print fslice" " = \"bob\""
69gdb_test "print &f\[3..\]" " = \"bob\""
70
67218854
TT
71gdb_test "print g" " = \\(u8 \\(\\*\\)\\\[6\\\]\\) $hex b\"hi bob\""
72gdb_test "ptype g" " = u8 \\(\\*\\)\\\[6\\\]"
73
74gdb_test "print v" " = simple::Something::Three"
75gdb_test_sequence "ptype v" "" {
76 " = enum simple::Something \\{"
77 " One,"
78 " Two,"
79 " Three,"
80 "\\}"
81}
82
83gdb_test "print w" " = \\\[1, 2, 3, 4\\\]"
84gdb_test "ptype w" " = \\\[i32; 4\\\]"
85gdb_test "print w\[2\]" " = 3"
86gdb_test "print w\[2\] @ 2" " = \\\[3, 4\\\]"
42d94011 87gdb_test "print w_ptr\[2\]" " = 3"
67218854
TT
88gdb_test "print fromslice" " = 3"
89gdb_test "print slice\[0\]" " = 3"
90gdb_test "print slice as &\[i32\]\[0\]" " = 3"
91
b3e3859b
TT
92gdb_test_sequence "ptype slice" "" {
93 " = struct &\\\[i32\\\] \\{"
94 " data_ptr: i32 \\*,"
95 " length: usize,"
96 "\\}"
97}
98gdb_test_sequence "ptype &slice\[..\]" "" {
99 " = struct &\\\[i32\\\] \\{"
100 " data_ptr: i32 \\*,"
101 " length: usize,"
102 "\\}"
103}
104gdb_test_sequence "ptype &b\[..\]" "" {
105 " = struct &\\\[\\*gdb\\*\\\] \\{"
106 " data_ptr: i32 \\*,"
107 " length: usize,"
108 "\\}"
109}
110
67218854
TT
111gdb_test "print x" " = \\(23, 25\\.5\\)"
112gdb_test "ptype x" " = \\(i32, f64\\)"
113gdb_test "print x as (i32,f64)" " = \\(23, 25\\.5\\)"
114
115gdb_test "print y" " = simple::HiBob \\{field1: 7, field2: 8\\}"
116gdb_test_sequence "ptype y" "" {
117 " = struct simple::HiBob \\{"
118 " field1: i32,"
119 " field2: u64,"
120 "\\}"
121}
122gdb_test "print y.field2" " = 8"
123
124gdb_test "print z" " = simple::ByeBob \\(7, 8\\)"
125gdb_test_sequence "ptype z" "" {
126 " = struct simple::ByeBob \\("
127 " i32,"
128 " u64,"
129 "\\)"
130}
131gdb_test "print z.1" " = 8"
132
51a789c3
MG
133gdb_test "print univariant" " = simple::Univariant::Foo{a: 1}"
134gdb_test "print univariant.a" " = 1"
135gdb_test "print univariant_anon" " = simple::UnivariantAnon::Foo\\(1\\)"
136gdb_test "print univariant_anon.0" " = 1"
4a3fe98f
TT
137gdb_test "print univariant_anon.sss" \
138 "Attempting to access named field sss of tuple variant simple::UnivariantAnon::Foo, which has only anonymous fields"
51a789c3 139
f0fd41c1
TT
140gdb_test_sequence "ptype simple::Univariant" "" {
141 "type = enum simple::Univariant \\{"
142 " Foo\\{a: u8\\},"
143 "\\}"
144}
145
146gdb_test_sequence "ptype simple::UnivariantAnon" "" {
147 "type = enum simple::UnivariantAnon \\{"
148 " Foo\\(u8\\),"
149 "\\}"
150}
151
67218854
TT
152gdb_test_sequence "ptype simple::ByeBob" "" {
153 " = struct simple::ByeBob \\("
154 " i32,"
155 " u64,"
156 "\\)"
157}
158gdb_test "print simple::ByeBob(0xff, 5)" \
159 " = simple::ByeBob \\(255, 5\\)"
160gdb_test "print simple::ByeBob\{field1: 0xff, field2:5\}" \
161 "Struct expression applied to non-struct type"
162
163gdb_test "print simple::HiBob(0xff, 5)" \
164 "Type simple::HiBob is not a tuple struct"
8880f2a9
TT
165gdb_test "print sizeof(simple::HiBob)" " = \[0-9\]+"
166gdb_test "print simple::HiBob + 5" \
167 "Found type 'simple::HiBob', which can't be evaluated in this context"
67218854
TT
168gdb_test "print nosuchsymbol" \
169 "No symbol 'nosuchsymbol' in current context"
170
92630041
TT
171gdb_test "print simple::HiBob{field1, field2}" \
172 " = simple::HiBob \\{field1: 77, field2: 88\\}"
173
50146e70
TT
174gdb_test "print simple::HiBob{field1: 99, .. y}" \
175 " = simple::HiBob \\{field1: 99, field2: 8\\}"
176
67218854
TT
177gdb_test "print e" " = simple::MoreComplicated::Two\\(73\\)"
178gdb_test "print e2" \
179 " = simple::MoreComplicated::Four\\{this: true, is: 8, a: 109 'm', struct_: 100, variant: 10\\}"
cdf5a07c 180gdb_test "print sizeof(e)" " = 24"
67218854
TT
181gdb_test_sequence "ptype e" "" {
182 " = enum simple::MoreComplicated \\{"
183 " One,"
184 " Two\\(i32\\),"
185 " Three\\(simple::HiBob\\),"
186 " Four\\{this: bool, is: u8, a: char, struct_: u64, variant: u32\\},"
187 "\\}"
188}
189
190gdb_test "print e.0" " = 73"
191gdb_test "print e.1" \
192 "Cannot access field 1 of variant simple::MoreComplicated::Two, there are only 1 fields"
193gdb_test "print e.foo" \
194 "Attempting to access named field foo of tuple variant simple::MoreComplicated::Two, which has only anonymous fields"
195
196gdb_test "print e2.variant" " = 10"
197gdb_test "print e2.notexist" \
198 "Could not find field notexist of struct variant simple::MoreComplicated::Four"
199gdb_test "print e2.0" \
200 "Variant simple::MoreComplicated::Four is not a tuple variant"
201
202gdb_test "print k" " = simple::SpaceSaver::Nothing"
203gdb_test "print l" " = simple::SpaceSaver::Thebox\\(9, $hex\\)"
204gdb_test "print *l.1" " = 1729"
205
206gdb_test "print diff2(3, 7)" " = -4"
207gdb_test "print self::diff2(8, 9)" " = -1"
208gdb_test "print ::diff2(23, -23)" " = 46"
209
210gdb_test "ptype diff2" "fn \\(i32, i32\\) -> i32"
921d8f54 211gdb_test "ptype empty" "fn \\(\\)"
67218854
TT
212
213gdb_test "print (diff2 as fn(i32, i32) -> i32)(19, -2)" " = 21"
214
45320ffa 215gdb_test "print \"hello rust\"" " = \"hello rust.*\""
67218854 216gdb_test "print \"hello" "Unexpected EOF in string"
45320ffa 217gdb_test "print r##\"hello \" rust\"##" " = \"hello \\\\\" rust.*\""
67218854
TT
218gdb_test "print r\"hello" "Unexpected EOF in string"
219gdb_test "print r###\"###hello\"" "Unexpected EOF in string"
220gdb_test "print r###\"###hello\"##" "Unexpected EOF in string"
221gdb_test "print r###\"hello###" "Unexpected EOF in string"
222
223gdb_test "print 0..5" " = .*::ops::Range.* \\{start: 0, end: 5\\}"
6873858b 224gdb_test "print 0..=5" " = .*::ops::RangeInclusive.* \\{start: 0, end: 5\\}"
67218854 225gdb_test "print ..5" " = .*::ops::RangeTo.* \\{end: 5\\}"
6873858b 226gdb_test "print ..=5" " = .*::ops::RangeToInclusive.* \\{end: 5\\}"
67218854
TT
227gdb_test "print 5.." " = .*::ops::RangeFrom.* \\{start: 5\\}"
228gdb_test "print .." " = .*::ops::RangeFull"
229
fccb08f8 230gdb_test "print str_some" \
fdffd6f4
TT
231 " = core::option::Option<\[a-z\]+::string::String>::Some\\(\[a-z\]+::string::String .*"
232gdb_test "print str_none" " = core::option::Option<\[a-z\]+::string::String>::None"
c9317f21
TT
233gdb_test "print int_some" " = core::option::Option<u8>::Some\\(1\\)"
234gdb_test "print int_none" " = core::option::Option<u8>::None"
ec8df234
TT
235gdb_test "print box_some" " = core::option::Option<\[a-z:\]*Box<u8>>::Some\\(.*\\)"
236gdb_test "print box_none" " = core::option::Option<\[a-z:\]*Box<u8>>::None"
fccb08f8 237gdb_test "print custom_some" \
fdffd6f4 238 " = simple::NonZeroOptimized::Value\\(\[a-z\]+::string::String .*"
fccb08f8
MG
239gdb_test "print custom_none" " = simple::NonZeroOptimized::Empty"
240
67218854
TT
241proc test_one_slice {svar length base range} {
242 global hex
243
244 set result " = &\\\[.*\\\] \\{data_ptr: $hex, length: $length\\}"
245
246 gdb_test "print $svar" $result
247 gdb_test "print &${base}\[${range}\]" $result
248}
249
250test_one_slice slice 1 w 2..3
6873858b 251test_one_slice slice 1 w 2..=2
67218854 252test_one_slice slice2 1 slice 0..1
6873858b 253test_one_slice slice2 1 slice 0..=0
67218854
TT
254
255test_one_slice all1 4 w ..
256test_one_slice all2 1 slice ..
257
258test_one_slice from1 3 w 1..
259test_one_slice from2 0 slice 1..
260
261test_one_slice to1 3 w ..3
6873858b 262test_one_slice to1 3 w ..=2
67218854 263test_one_slice to2 1 slice ..1
6873858b 264test_one_slice to2 1 slice ..=0
67218854
TT
265
266gdb_test "print w\[2..3\]" "Can't take slice of array without '&'"
267
268
269gdb_test_sequence "complete print y.f" "" \
270 {"print y.field1" "print y.field2"}
271gdb_test_sequence "complete print y." "" \
272 {"print y.field1" "print y.field2"}
273
274# Unimplemented, but we can at least test the parser productions.
275gdb_test "print (1,2,3)" "Tuple expressions not supported yet"
276gdb_test "print (1,)" "Tuple expressions not supported yet"
277gdb_test "print (1)" " = 1"
278
279gdb_test "print 23..97.0" "Range expression with different types"
51a789c3
MG
280
281gdb_test "print (*parametrized.next.val)" \
ec8df234 282 " = simple::ParametrizedStruct<i32> {next: simple::ParametrizedEnum<\[a-z:\]*Box<simple::ParametrizedStruct<i32>>>::Empty, value: 1}"
51a789c3
MG
283gdb_test "print parametrized.next.val" \
284 " = \\(simple::ParametrizedStruct<i32> \\*\\) $hex"
285gdb_test "print parametrized" \
ec8df234 286 " = simple::ParametrizedStruct<i32> \\{next: simple::ParametrizedEnum<\[a-z:\]*Box<simple::ParametrizedStruct<i32>>>::Val\\{val: $hex\\}, value: 0\\}"
65547233 287
a037790e 288gdb_test "print u" " = simple::Union {f1: -1, f2: 255}"
65547233 289
a33ccfc7
TT
290gdb_test_sequence "ptype/o Union" "" {
291 "/\\* offset | size \\*/ type = union simple::Union {"
292 "/\\* 1 \\*/ f1: i8,"
293 "/\\* 1 \\*/ f2: u8,"
294 ""
295 " /\\* total size \\(bytes\\): 1 \\*/"
296 " }"
297}
298
299gdb_test_sequence "ptype/o SimpleLayout" "" {
300 "/\\* offset | size \\*/ type = struct simple::SimpleLayout {"
301 "/\\* 0 | 2 \\*/ f1: u16,"
302 "/\\* 2 | 2 \\*/ f2: u16,"
303 ""
304 " /\\* total size \\(bytes\\): 4 \\*/"
305 " }"
306}
307
098b2108
TT
308# PR rust/23626 - this used to crash. Note that the results are
309# fairly lax because most existing versions of Rust (those before the
310# DW_TAG_variant patches) do not emit what gdb wants here; and there
311# was little point fixing gdb to cope with these cases as the fixed
312# compilers will be available soon
313gdb_test "print empty_enum_value" \
314 " = simple::EmptyEnum.*"
315gdb_test "ptype empty_enum_value" "simple::EmptyEnum.*"
316# Just make sure these don't crash, for the same reason.
317gdb_test "print empty_enum_value.0" ""
318gdb_test "print empty_enum_value.something" ""
319
65547233
TT
320load_lib gdb-python.exp
321if {[skip_python_tests]} {
322 continue
323}
324
325gdb_test "python print(gdb.lookup_type('simple::HiBob'))" "simple::HiBob"
This page took 0.300896 seconds and 4 git commands to generate.