summaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.rust/simple.exp
blob: 8e84daab3a9a8a64ba35ca713e9abed0d6962ce6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# Copyright (C) 2016 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Test expression parsing and evaluation that requires Rust compiler.

load_lib rust-support.exp
if {[skip_rust_tests]} {
    continue
}

standard_testfile .rs
if {[prepare_for_testing ${testfile}.exp $testfile $srcfile {debug rust}]} {
    return -1
}

set line [gdb_get_line_number "set breakpoint here"]
if {![runto ${srcfile}:$line]} {
    untested $testfile
    return -1
}

gdb_test "print a" " = \\(\\)"
gdb_test "ptype a" " = \\(\\)"

gdb_test "print b" " = \\\[\\\]"
gdb_test "ptype b" " = \\\[i32; 0\\\]"
gdb_test "print *(&b as *const \[i32; 0\])" " = \\\[\\\]"
gdb_test "print *(&b as *const \[i32; 0_0\])" " = \\\[\\\]"

gdb_test "print c" " = 99"
gdb_test "ptype c" " = i32"

gdb_test "print c = 87" " = \\(\\)"
gdb_test "print c" " = 87"
gdb_test "print c += 3" " = \\(\\)"
gdb_test "print c" " = 90"
gdb_test "print c -= 90" " = \\(\\)"
gdb_test "print c" " = 0"
gdb_test "print *&c" " = 0"
gdb_test "print *(&c as &i32)" " = 0"
gdb_test "print *(&c as *const i32)" " = 0"
gdb_test "print *(&c as *mut i32)" " = 0"

gdb_test "print j" " = simple::Unit"
gdb_test "ptype j" " = struct simple::Unit"
gdb_test "print j2" " = simple::Unit"
gdb_test "ptype j2" " = struct simple::Unit"
gdb_test "print simple::Unit" " = simple::Unit"
gdb_test "print simple::Unit{}" " = simple::Unit"

gdb_test "print g" " = \\(u8 \\(\\*\\)\\\[6\\\]\\) $hex b\"hi bob\""
gdb_test "ptype g" " = u8 \\(\\*\\)\\\[6\\\]"

gdb_test "print v" " = simple::Something::Three"
gdb_test_sequence "ptype v" "" {
    " = enum simple::Something \\{"
    "  One,"
    "  Two,"
    "  Three,"
    "\\}"
}

gdb_test "print w" " = \\\[1, 2, 3, 4\\\]"
gdb_test "ptype w" " = \\\[i32; 4\\\]"
gdb_test "print w\[2\]" " = 3"
gdb_test "print w\[2\] @ 2" " = \\\[3, 4\\\]"
gdb_test "print w_ptr\[2\]" " = 3"
gdb_test "print fromslice" " = 3"
gdb_test "print slice\[0\]" " = 3"
gdb_test "print slice as &\[i32\]\[0\]" " = 3"

gdb_test "print x" " = \\(23, 25\\.5\\)"
gdb_test "ptype x" " = \\(i32, f64\\)"
gdb_test "print x as (i32,f64)" " = \\(23, 25\\.5\\)"

gdb_test "print y" " = simple::HiBob \\{field1: 7, field2: 8\\}"
gdb_test_sequence "ptype y" "" {
    " = struct simple::HiBob \\{"
    "  field1: i32,"
    "  field2: u64,"
    "\\}"
}
gdb_test "print y.field2" " = 8"

gdb_test "print z" " = simple::ByeBob \\(7, 8\\)"
gdb_test_sequence "ptype z" "" {
    " = struct simple::ByeBob \\("
    "  i32,"
    "  u64,"
    "\\)"
}
gdb_test "print z.1" " = 8"

gdb_test "print univariant" " = simple::Univariant::Foo{a: 1}"
gdb_test "print univariant.a" " = 1"
gdb_test "print univariant_anon" " = simple::UnivariantAnon::Foo\\(1\\)"
gdb_test "print univariant_anon.0" " = 1"

gdb_test_sequence "ptype simple::ByeBob" "" {
    " = struct simple::ByeBob \\("
    "  i32,"
    "  u64,"
    "\\)"
}
gdb_test "print simple::ByeBob(0xff, 5)" \
    " = simple::ByeBob \\(255, 5\\)"
gdb_test "print simple::ByeBob\{field1: 0xff, field2:5\}" \
    "Struct expression applied to non-struct type"

gdb_test "print simple::HiBob(0xff, 5)" \
    "Type simple::HiBob is not a tuple struct"
gdb_test "print nosuchsymbol" \
    "No symbol 'nosuchsymbol' in current context"

gdb_test "print e" " = simple::MoreComplicated::Two\\(73\\)"
gdb_test "print e2" \
    " = simple::MoreComplicated::Four\\{this: true, is: 8, a: 109 'm', struct_: 100, variant: 10\\}"

gdb_test_sequence "ptype e" "" {
    " = enum simple::MoreComplicated \\{"
    "  One,"
    "  Two\\(i32\\),"
    "  Three\\(simple::HiBob\\),"
    "  Four\\{this: bool, is: u8, a: char, struct_: u64, variant: u32\\},"
    "\\}"
}

gdb_test "print e.0" " = 73"
gdb_test "print e.1" \
    "Cannot access field 1 of variant simple::MoreComplicated::Two, there are only 1 fields"
gdb_test "print e.foo" \
    "Attempting to access named field foo of tuple variant simple::MoreComplicated::Two, which has only anonymous fields"

gdb_test "print e2.variant" " = 10"
gdb_test "print e2.notexist" \
    "Could not find field notexist of struct variant simple::MoreComplicated::Four"
gdb_test "print e2.0" \
    "Variant simple::MoreComplicated::Four is not a tuple variant"

gdb_test "print k" " = simple::SpaceSaver::Nothing"
gdb_test "print l" " = simple::SpaceSaver::Thebox\\(9, $hex\\)"
gdb_test "print *l.1" " = 1729"

gdb_test "print diff2(3, 7)" " = -4"
gdb_test "print self::diff2(8, 9)" " = -1"
gdb_test "print ::diff2(23, -23)" " = 46"

gdb_test "ptype diff2" "fn \\(i32, i32\\) -> i32"
gdb_test "ptype empty" "fn \\(\\)"

gdb_test "print (diff2 as fn(i32, i32) -> i32)(19, -2)" " = 21"

# We need the ".*" because currently we don't extract the length and
# use it to intelligently print the string data.
gdb_test "print \"hello rust\"" \
    " = &str \\{data_ptr: $hex \"hello rust.*\", length: 10\\}"
gdb_test "print \"hello" "Unexpected EOF in string"
gdb_test "print r##\"hello \" rust\"##" \
    " = &str \\{data_ptr: $hex \"hello \\\\\" rust.*\", length: 12\\}"
gdb_test "print r\"hello" "Unexpected EOF in string"
gdb_test "print r###\"###hello\"" "Unexpected EOF in string"
gdb_test "print r###\"###hello\"##" "Unexpected EOF in string"
gdb_test "print r###\"hello###" "Unexpected EOF in string"

gdb_test "print 0..5" " = .*::ops::Range.* \\{start: 0, end: 5\\}"
gdb_test "print ..5" " = .*::ops::RangeTo.* \\{end: 5\\}"
gdb_test "print 5.." " = .*::ops::RangeFrom.* \\{start: 5\\}"
gdb_test "print .." " = .*::ops::RangeFull"

gdb_test "print str_some" \
    " = core::option::Option<collections::string::String>::Some\\(collections::string::String .*"
gdb_test "print str_none" " = core::option::Option<collections::string::String>::None"
gdb_test "print int_some" " = core::option::Option::Some\\(1\\)"
gdb_test "print int_none" " = core::option::Option::None"
gdb_test "print box_some" " = core::option::Option<Box<u8>>::Some\\(.*\\)"
gdb_test "print box_none" " = core::option::Option<Box<u8>>::None"
gdb_test "print custom_some" \
    " = simple::NonZeroOptimized::Value\\(collections::string::String .*"
gdb_test "print custom_none" " = simple::NonZeroOptimized::Empty"

proc test_one_slice {svar length base range} {
    global hex

    set result " = &\\\[.*\\\] \\{data_ptr: $hex, length: $length\\}"

    gdb_test "print $svar" $result
    gdb_test "print &${base}\[${range}\]" $result
}

test_one_slice slice 1 w 2..3
test_one_slice slice2 1 slice 0..1

test_one_slice all1 4 w ..
test_one_slice all2 1 slice ..

test_one_slice from1 3 w 1..
test_one_slice from2 0 slice 1..

test_one_slice to1 3 w ..3
test_one_slice to2 1 slice ..1

gdb_test "print w\[2..3\]" "Can't take slice of array without '&'"


gdb_test_sequence "complete print y.f" "" \
    {"print y.field1" "print y.field2"}
gdb_test_sequence "complete print y." "" \
    {"print y.field1" "print y.field2"}

# Unimplemented, but we can at least test the parser productions.
gdb_test "print (1,2,3)" "Tuple expressions not supported yet"
gdb_test "print (1,)" "Tuple expressions not supported yet"
gdb_test "print (1)" " = 1"

gdb_test "print 23..97.0" "Range expression with different types"

gdb_test "print (*parametrized.next.val)" \
    " = simple::ParametrizedStruct<i32> {next: simple::ParametrizedEnum<Box<simple::ParametrizedStruct<i32>>>::Empty, value: 1}"
gdb_test "print parametrized.next.val" \
    " = \\(simple::ParametrizedStruct<i32> \\*\\) $hex"
gdb_test "print parametrized" \
    " = simple::ParametrizedStruct<i32> \\{next: simple::ParametrizedEnum<Box<simple::ParametrizedStruct<i32>>>::Val\\{val: $hex\\}, value: 0\\}"