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