Fix ptype of Rust slices
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.rust / simple.exp
CommitLineData
61baf725 1# Copyright (C) 2016-2017 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
TT
66
67gdb_test "print g" " = \\(u8 \\(\\*\\)\\\[6\\\]\\) $hex b\"hi bob\""
68gdb_test "ptype g" " = u8 \\(\\*\\)\\\[6\\\]"
69
70gdb_test "print v" " = simple::Something::Three"
71gdb_test_sequence "ptype v" "" {
72 " = enum simple::Something \\{"
73 " One,"
74 " Two,"
75 " Three,"
76 "\\}"
77}
78
79gdb_test "print w" " = \\\[1, 2, 3, 4\\\]"
80gdb_test "ptype w" " = \\\[i32; 4\\\]"
81gdb_test "print w\[2\]" " = 3"
82gdb_test "print w\[2\] @ 2" " = \\\[3, 4\\\]"
42d94011 83gdb_test "print w_ptr\[2\]" " = 3"
67218854
TT
84gdb_test "print fromslice" " = 3"
85gdb_test "print slice\[0\]" " = 3"
86gdb_test "print slice as &\[i32\]\[0\]" " = 3"
87
b3e3859b
TT
88gdb_test_sequence "ptype slice" "" {
89 " = struct &\\\[i32\\\] \\{"
90 " data_ptr: i32 \\*,"
91 " length: usize,"
92 "\\}"
93}
94gdb_test_sequence "ptype &slice\[..\]" "" {
95 " = struct &\\\[i32\\\] \\{"
96 " data_ptr: i32 \\*,"
97 " length: usize,"
98 "\\}"
99}
100gdb_test_sequence "ptype &b\[..\]" "" {
101 " = struct &\\\[\\*gdb\\*\\\] \\{"
102 " data_ptr: i32 \\*,"
103 " length: usize,"
104 "\\}"
105}
106
67218854
TT
107gdb_test "print x" " = \\(23, 25\\.5\\)"
108gdb_test "ptype x" " = \\(i32, f64\\)"
109gdb_test "print x as (i32,f64)" " = \\(23, 25\\.5\\)"
110
111gdb_test "print y" " = simple::HiBob \\{field1: 7, field2: 8\\}"
112gdb_test_sequence "ptype y" "" {
113 " = struct simple::HiBob \\{"
114 " field1: i32,"
115 " field2: u64,"
116 "\\}"
117}
118gdb_test "print y.field2" " = 8"
119
120gdb_test "print z" " = simple::ByeBob \\(7, 8\\)"
121gdb_test_sequence "ptype z" "" {
122 " = struct simple::ByeBob \\("
123 " i32,"
124 " u64,"
125 "\\)"
126}
127gdb_test "print z.1" " = 8"
128
51a789c3
MG
129gdb_test "print univariant" " = simple::Univariant::Foo{a: 1}"
130gdb_test "print univariant.a" " = 1"
131gdb_test "print univariant_anon" " = simple::UnivariantAnon::Foo\\(1\\)"
132gdb_test "print univariant_anon.0" " = 1"
133
f0fd41c1
TT
134gdb_test_sequence "ptype simple::Univariant" "" {
135 "type = enum simple::Univariant \\{"
136 " Foo\\{a: u8\\},"
137 "\\}"
138}
139
140gdb_test_sequence "ptype simple::UnivariantAnon" "" {
141 "type = enum simple::UnivariantAnon \\{"
142 " Foo\\(u8\\),"
143 "\\}"
144}
145
67218854
TT
146gdb_test_sequence "ptype simple::ByeBob" "" {
147 " = struct simple::ByeBob \\("
148 " i32,"
149 " u64,"
150 "\\)"
151}
152gdb_test "print simple::ByeBob(0xff, 5)" \
153 " = simple::ByeBob \\(255, 5\\)"
154gdb_test "print simple::ByeBob\{field1: 0xff, field2:5\}" \
155 "Struct expression applied to non-struct type"
156
157gdb_test "print simple::HiBob(0xff, 5)" \
158 "Type simple::HiBob is not a tuple struct"
8880f2a9
TT
159gdb_test "print sizeof(simple::HiBob)" " = \[0-9\]+"
160gdb_test "print simple::HiBob + 5" \
161 "Found type 'simple::HiBob', which can't be evaluated in this context"
67218854
TT
162gdb_test "print nosuchsymbol" \
163 "No symbol 'nosuchsymbol' in current context"
164
165gdb_test "print e" " = simple::MoreComplicated::Two\\(73\\)"
166gdb_test "print e2" \
167 " = simple::MoreComplicated::Four\\{this: true, is: 8, a: 109 'm', struct_: 100, variant: 10\\}"
cdf5a07c 168gdb_test "print sizeof(e)" " = 24"
67218854
TT
169gdb_test_sequence "ptype e" "" {
170 " = enum simple::MoreComplicated \\{"
171 " One,"
172 " Two\\(i32\\),"
173 " Three\\(simple::HiBob\\),"
174 " Four\\{this: bool, is: u8, a: char, struct_: u64, variant: u32\\},"
175 "\\}"
176}
177
178gdb_test "print e.0" " = 73"
179gdb_test "print e.1" \
180 "Cannot access field 1 of variant simple::MoreComplicated::Two, there are only 1 fields"
181gdb_test "print e.foo" \
182 "Attempting to access named field foo of tuple variant simple::MoreComplicated::Two, which has only anonymous fields"
183
184gdb_test "print e2.variant" " = 10"
185gdb_test "print e2.notexist" \
186 "Could not find field notexist of struct variant simple::MoreComplicated::Four"
187gdb_test "print e2.0" \
188 "Variant simple::MoreComplicated::Four is not a tuple variant"
189
190gdb_test "print k" " = simple::SpaceSaver::Nothing"
191gdb_test "print l" " = simple::SpaceSaver::Thebox\\(9, $hex\\)"
192gdb_test "print *l.1" " = 1729"
193
194gdb_test "print diff2(3, 7)" " = -4"
195gdb_test "print self::diff2(8, 9)" " = -1"
196gdb_test "print ::diff2(23, -23)" " = 46"
197
198gdb_test "ptype diff2" "fn \\(i32, i32\\) -> i32"
921d8f54 199gdb_test "ptype empty" "fn \\(\\)"
67218854
TT
200
201gdb_test "print (diff2 as fn(i32, i32) -> i32)(19, -2)" " = 21"
202
203# We need the ".*" because currently we don't extract the length and
204# use it to intelligently print the string data.
205gdb_test "print \"hello rust\"" \
206 " = &str \\{data_ptr: $hex \"hello rust.*\", length: 10\\}"
207gdb_test "print \"hello" "Unexpected EOF in string"
208gdb_test "print r##\"hello \" rust\"##" \
209 " = &str \\{data_ptr: $hex \"hello \\\\\" rust.*\", length: 12\\}"
210gdb_test "print r\"hello" "Unexpected EOF in string"
211gdb_test "print r###\"###hello\"" "Unexpected EOF in string"
212gdb_test "print r###\"###hello\"##" "Unexpected EOF in string"
213gdb_test "print r###\"hello###" "Unexpected EOF in string"
214
215gdb_test "print 0..5" " = .*::ops::Range.* \\{start: 0, end: 5\\}"
216gdb_test "print ..5" " = .*::ops::RangeTo.* \\{end: 5\\}"
217gdb_test "print 5.." " = .*::ops::RangeFrom.* \\{start: 5\\}"
218gdb_test "print .." " = .*::ops::RangeFull"
219
fccb08f8 220gdb_test "print str_some" \
fdffd6f4
TT
221 " = core::option::Option<\[a-z\]+::string::String>::Some\\(\[a-z\]+::string::String .*"
222gdb_test "print str_none" " = core::option::Option<\[a-z\]+::string::String>::None"
fccb08f8
MG
223gdb_test "print int_some" " = core::option::Option::Some\\(1\\)"
224gdb_test "print int_none" " = core::option::Option::None"
ec8df234
TT
225gdb_test "print box_some" " = core::option::Option<\[a-z:\]*Box<u8>>::Some\\(.*\\)"
226gdb_test "print box_none" " = core::option::Option<\[a-z:\]*Box<u8>>::None"
fccb08f8 227gdb_test "print custom_some" \
fdffd6f4 228 " = simple::NonZeroOptimized::Value\\(\[a-z\]+::string::String .*"
fccb08f8
MG
229gdb_test "print custom_none" " = simple::NonZeroOptimized::Empty"
230
67218854
TT
231proc test_one_slice {svar length base range} {
232 global hex
233
234 set result " = &\\\[.*\\\] \\{data_ptr: $hex, length: $length\\}"
235
236 gdb_test "print $svar" $result
237 gdb_test "print &${base}\[${range}\]" $result
238}
239
240test_one_slice slice 1 w 2..3
241test_one_slice slice2 1 slice 0..1
242
243test_one_slice all1 4 w ..
244test_one_slice all2 1 slice ..
245
246test_one_slice from1 3 w 1..
247test_one_slice from2 0 slice 1..
248
249test_one_slice to1 3 w ..3
250test_one_slice to2 1 slice ..1
251
252gdb_test "print w\[2..3\]" "Can't take slice of array without '&'"
253
254
255gdb_test_sequence "complete print y.f" "" \
256 {"print y.field1" "print y.field2"}
257gdb_test_sequence "complete print y." "" \
258 {"print y.field1" "print y.field2"}
259
260# Unimplemented, but we can at least test the parser productions.
261gdb_test "print (1,2,3)" "Tuple expressions not supported yet"
262gdb_test "print (1,)" "Tuple expressions not supported yet"
263gdb_test "print (1)" " = 1"
264
265gdb_test "print 23..97.0" "Range expression with different types"
51a789c3
MG
266
267gdb_test "print (*parametrized.next.val)" \
ec8df234 268 " = simple::ParametrizedStruct<i32> {next: simple::ParametrizedEnum<\[a-z:\]*Box<simple::ParametrizedStruct<i32>>>::Empty, value: 1}"
51a789c3
MG
269gdb_test "print parametrized.next.val" \
270 " = \\(simple::ParametrizedStruct<i32> \\*\\) $hex"
271gdb_test "print parametrized" \
ec8df234 272 " = simple::ParametrizedStruct<i32> \\{next: simple::ParametrizedEnum<\[a-z:\]*Box<simple::ParametrizedStruct<i32>>>::Val\\{val: $hex\\}, value: 0\\}"
65547233
TT
273
274
275load_lib gdb-python.exp
276if {[skip_python_tests]} {
277 continue
278}
279
280gdb_test "python print(gdb.lookup_type('simple::HiBob'))" "simple::HiBob"
This page took 0.240958 seconds and 4 git commands to generate.