]> git.lizzy.rs Git - rust.git/blob - src/test/debuginfo/type-names.rs
Various minor/cosmetic improvements to code
[rust.git] / src / test / debuginfo / type-names.rs
1 // Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // ignore-tidy-linelength
12 // ignore-lldb
13 // ignore-gdb // Test temporarily ignored due to debuginfo tests being disabled, see PR 47155
14
15 // compile-flags:-g
16
17 // gdb-command:run
18
19 // STRUCTS
20 // gdb-command:whatis simple_struct
21 // gdbg-check:type = struct Struct1
22 // gdbr-check:type = type_names::Struct1
23
24 // gdb-command:whatis generic_struct1
25 // gdbg-check:type = struct GenericStruct<type_names::mod1::Struct2, type_names::mod1::mod2::Struct3>
26 // gdbr-check:type = type_names::GenericStruct<type_names::mod1::Struct2, type_names::mod1::mod2::Struct3>
27
28 // gdb-command:whatis generic_struct2
29 // gdbg-check:type = struct GenericStruct<type_names::Struct1, extern "fastcall" fn(isize) -> usize>
30 // gdbr-check:type = type_names::GenericStruct<type_names::Struct1, extern "fastcall" fn(isize) -> usize>
31
32 // gdb-command:whatis mod_struct
33 // gdbg-check:type = struct Struct2
34 // gdbr-check:type = type_names::mod1::Struct2
35
36
37 // ENUMS
38 // gdb-command:whatis simple_enum_1
39 // gdbg-check:type = union Enum1
40 // gdbr-check:type = type_names::Enum1
41
42 // gdb-command:whatis simple_enum_2
43 // gdbg-check:type = union Enum1
44 // gdbr-check:type = type_names::Enum1
45
46 // gdb-command:whatis simple_enum_3
47 // gdbg-check:type = union Enum2
48 // gdbr-check:type = type_names::mod1::Enum2
49
50 // gdb-command:whatis generic_enum_1
51 // gdbg-check:type = union Enum3<type_names::mod1::Struct2>
52 // gdbr-check:type = type_names::mod1::mod2::Enum3<type_names::mod1::Struct2>
53
54 // gdb-command:whatis generic_enum_2
55 // gdbg-check:type = union Enum3<type_names::Struct1>
56 // gdbr-check:type = type_names::mod1::mod2::Enum3<type_names::Struct1>
57
58
59 // TUPLES
60 // gdb-command:whatis tuple1
61 // gdbg-check:type = struct (u32, type_names::Struct1, type_names::mod1::mod2::Enum3<type_names::mod1::Struct2>)
62 // gdbr-check:type = (u32, type_names::Struct1, type_names::mod1::mod2::Enum3<type_names::mod1::Struct2>)
63
64 // gdb-command:whatis tuple2
65 // gdbg-check:type = struct ((type_names::Struct1, type_names::mod1::mod2::Struct3), type_names::mod1::Enum2, char)
66 // gdbr-check:type = ((type_names::Struct1, type_names::mod1::mod2::Struct3), type_names::mod1::Enum2, char)
67
68
69 // BOX
70 // gdb-command:whatis box1
71 // gdbg-check:type = struct (alloc::boxed::Box<f32>, i32)
72 // gdbr-check:type = (alloc::boxed::Box<f32>, i32)
73
74 // gdb-command:whatis box2
75 // gdbg-check:type = struct (alloc::boxed::Box<type_names::mod1::mod2::Enum3<f32>>, i32)
76 // gdbr-check:type = (alloc::boxed::Box<type_names::mod1::mod2::Enum3<f32>>, i32)
77
78
79 // REFERENCES
80 // gdb-command:whatis ref1
81 // gdbg-check:type = struct (&type_names::Struct1, i32)
82 // gdbr-check:type = (&type_names::Struct1, i32)
83
84 // gdb-command:whatis ref2
85 // gdbg-check:type = struct (&type_names::GenericStruct<char, type_names::Struct1>, i32)
86 // gdbr-check:type = (&type_names::GenericStruct<char, type_names::Struct1>, i32)
87
88 // gdb-command:whatis mut_ref1
89 // gdbg-check:type = struct (&mut type_names::Struct1, i32)
90 // gdbr-check:type = (&mut type_names::Struct1, i32)
91
92 // gdb-command:whatis mut_ref2
93 // gdbg-check:type = struct (&mut type_names::GenericStruct<type_names::mod1::Enum2, f64>, i32)
94 // gdbr-check:type = (&mut type_names::GenericStruct<type_names::mod1::Enum2, f64>, i32)
95
96
97 // RAW POINTERS
98 // gdb-command:whatis mut_ptr1
99 // gdbg-check:type = struct (*mut type_names::Struct1, isize)
100 // gdbr-check:type = (*mut type_names::Struct1, isize)
101
102 // gdb-command:whatis mut_ptr2
103 // gdbg-check:type = struct (*mut isize, isize)
104 // gdbr-check:type = (*mut isize, isize)
105
106 // gdb-command:whatis mut_ptr3
107 // gdbg-check:type = struct (*mut type_names::mod1::mod2::Enum3<type_names::Struct1>, isize)
108 // gdbr-check:type = (*mut type_names::mod1::mod2::Enum3<type_names::Struct1>, isize)
109
110 // gdb-command:whatis const_ptr1
111 // gdbg-check:type = struct (*const type_names::Struct1, isize)
112 // gdbr-check:type = (*const type_names::Struct1, isize)
113
114 // gdb-command:whatis const_ptr2
115 // gdbg-check:type = struct (*const isize, isize)
116 // gdbr-check:type = (*const isize, isize)
117
118 // gdb-command:whatis const_ptr3
119 // gdbg-check:type = struct (*const type_names::mod1::mod2::Enum3<type_names::Struct1>, isize)
120 // gdbr-check:type = (*const type_names::mod1::mod2::Enum3<type_names::Struct1>, isize)
121
122
123 // VECTORS
124 // gdb-command:whatis fixed_size_vec1
125 // gdbg-check:type = struct ([type_names::Struct1; 3], i16)
126 // gdbr-check:type = ([type_names::Struct1; 3], i16)
127
128 // gdb-command:whatis fixed_size_vec2
129 // gdbg-check:type = struct ([usize; 3], i16)
130 // gdbr-check:type = ([usize; 3], i16)
131
132 // gdb-command:whatis slice1
133 // gdbg-check:type = struct &[usize]
134 // gdbr-check:type = &[usize]
135
136 // gdb-command:whatis slice2
137 // gdbg-check:type = struct &[type_names::mod1::Enum2]
138 // gdbr-check:type = &[type_names::mod1::Enum2]
139
140
141 // TRAITS
142 // gdb-command:whatis box_trait
143 // gdbg-check:type = struct Box<Trait1>
144 // gdbr-check:type = type_names::Box<Trait1>
145
146 // gdb-command:whatis ref_trait
147 // gdbg-check:type = struct &Trait1
148 // gdbr-check:type = type_names::&Trait1
149
150 // gdb-command:whatis mut_ref_trait
151 // gdbg-check:type = struct &mut Trait1
152 // gdbr-check:type = type_names::&mut Trait1
153
154 // gdb-command:whatis generic_box_trait
155 // gdbg-check:type = struct Box<Trait2<i32, type_names::mod1::Struct2>>
156 // gdbr-check:type = type_names::Box<Trait2<i32, type_names::mod1::Struct2>>
157
158 // gdb-command:whatis generic_ref_trait
159 // gdbg-check:type = struct &Trait2<type_names::Struct1, type_names::Struct1>
160 // gdbr-check:type = type_names::&Trait2<type_names::Struct1, type_names::Struct1>
161
162 // gdb-command:whatis generic_mut_ref_trait
163 // gdbg-check:type = struct &mut Trait2<type_names::mod1::mod2::Struct3, type_names::GenericStruct<usize, isize>>
164 // gdbr-check:type = type_names::&mut Trait2<type_names::mod1::mod2::Struct3, type_names::GenericStruct<usize, isize>>
165
166
167 // BARE FUNCTIONS
168 // gdb-command:whatis rust_fn
169 // gdbg-check:type = struct (fn(core::option::Option<isize>, core::option::Option<&type_names::mod1::Struct2>), usize)
170 // gdbr-check:type = (fn(core::option::Option<isize>, core::option::Option<&type_names::mod1::Struct2>), usize)
171
172 // gdb-command:whatis extern_c_fn
173 // gdbg-check:type = struct (extern "C" fn(isize), usize)
174 // gdbr-check:type = (extern "C" fn(isize), usize)
175
176 // gdb-command:whatis unsafe_fn
177 // gdbg-check:type = struct (unsafe fn(core::result::Result<char, f64>), usize)
178 // gdbr-check:type = (unsafe fn(core::result::Result<char, f64>), usize)
179
180 // gdb-command:whatis extern_stdcall_fn
181 // gdbg-check:type = struct (extern "stdcall" fn(), usize)
182 // gdbr-check:type = (extern "stdcall" fn(), usize)
183
184 // gdb-command:whatis rust_fn_with_return_value
185 // gdbg-check:type = struct (fn(f64) -> usize, usize)
186 // gdbr-check:type = (fn(f64) -> usize, usize)
187
188 // gdb-command:whatis extern_c_fn_with_return_value
189 // gdbg-check:type = struct (extern "C" fn() -> type_names::Struct1, usize)
190 // gdbr-check:type = (extern "C" fn() -> type_names::Struct1, usize)
191
192 // gdb-command:whatis unsafe_fn_with_return_value
193 // gdbg-check:type = struct (unsafe fn(type_names::GenericStruct<u16, u8>) -> type_names::mod1::Struct2, usize)
194 // gdbr-check:type = (unsafe fn(type_names::GenericStruct<u16, u8>) -> type_names::mod1::Struct2, usize)
195
196 // gdb-command:whatis extern_stdcall_fn_with_return_value
197 // gdbg-check:type = struct (extern "stdcall" fn(alloc::boxed::Box<isize>) -> usize, usize)
198 // gdbr-check:type = (extern "stdcall" fn(alloc::boxed::Box<isize>) -> usize, usize)
199
200 // gdb-command:whatis generic_function_int
201 // gdbg-check:type = struct (fn(isize) -> isize, usize)
202 // gdbr-check:type = (fn(isize) -> isize, usize)
203
204 // gdb-command:whatis generic_function_struct3
205 // gdbg-check:type = struct (fn(type_names::mod1::mod2::Struct3) -> type_names::mod1::mod2::Struct3, usize)
206 // gdbr-check:type = (fn(type_names::mod1::mod2::Struct3) -> type_names::mod1::mod2::Struct3, usize)
207
208 // gdb-command:whatis variadic_function
209 // gdbg-check:type = struct (unsafe extern "C" fn(*const u8, ...) -> isize, usize)
210 // gdbr-check:type = (unsafe extern "C" fn(*const u8, ...) -> isize, usize)
211
212
213 // CLOSURES
214 // gdb-command:whatis closure1
215 // gdbg-check:type = struct (closure, usize)
216 // gdbr-check:type = (closure, usize)
217
218 // gdb-command:whatis closure2
219 // gdbg-check:type = struct (closure, usize)
220 // gdbr-check:type = (closure, usize)
221
222 #![feature(box_syntax)]
223 #![allow(unused_variables)]
224 #![feature(omit_gdb_pretty_printer_section)]
225 #![omit_gdb_pretty_printer_section]
226
227 use self::Enum1::{Variant1, Variant2};
228 use std::marker::PhantomData;
229 use std::ptr;
230
231 pub struct Struct1;
232 struct GenericStruct<T1, T2>(PhantomData<(T1,T2)>);
233
234 enum Enum1 {
235     Variant1,
236     Variant2(isize),
237 }
238
239 mod mod1 {
240     pub use self::Enum2::{Variant1, Variant2};
241     pub struct Struct2;
242
243     pub enum Enum2 {
244         Variant1,
245         Variant2(super::Struct1),
246     }
247
248     pub mod mod2 {
249         pub use self::Enum3::{Variant1, Variant2};
250         pub struct Struct3;
251
252         pub enum Enum3<T> {
253             Variant1,
254             Variant2(T),
255         }
256     }
257 }
258
259 trait Trait1 { fn dummy(&self) { } }
260 trait Trait2<T1, T2> { fn dummy(&self, _: T1, _:T2) { } }
261
262 impl Trait1 for isize {}
263 impl<T1, T2> Trait2<T1, T2> for isize {}
264
265 fn rust_fn(_: Option<isize>, _: Option<&mod1::Struct2>) {}
266 extern "C" fn extern_c_fn(_: isize) {}
267 unsafe fn unsafe_fn(_: Result<char, f64>) {}
268 extern "stdcall" fn extern_stdcall_fn() {}
269
270 fn rust_fn_with_return_value(_: f64) -> usize { 4 }
271 extern "C" fn extern_c_fn_with_return_value() -> Struct1 { Struct1 }
272 unsafe fn unsafe_fn_with_return_value(_: GenericStruct<u16, u8>) -> mod1::Struct2 { mod1::Struct2 }
273 extern "stdcall" fn extern_stdcall_fn_with_return_value(_: Box<isize>) -> usize { 0 }
274
275 fn generic_function<T>(x: T) -> T { x }
276
277 #[allow(improper_ctypes)]
278 extern {
279     fn printf(_:*const u8, ...) -> isize;
280 }
281
282 // In many of the cases below, the type that is actually under test is wrapped
283 // in a tuple, e.g., Box<T>, references, raw pointers, fixed-size vectors, ...
284 // This is because GDB will not print the type name from DWARF debuginfo for
285 // some kinds of types (pointers, arrays, functions, ...)
286 // Since tuples are structs as far as GDB is concerned, their name will be
287 // printed correctly, so the tests below just construct a tuple type that will
288 // then *contain* the type name that we want to see.
289 fn main() {
290
291     // Structs
292     let simple_struct = Struct1;
293     let generic_struct1: GenericStruct<mod1::Struct2, mod1::mod2::Struct3> =
294         GenericStruct(PhantomData);
295     let generic_struct2: GenericStruct<Struct1, extern "fastcall" fn(isize) -> usize> =
296         GenericStruct(PhantomData);
297     let mod_struct = mod1::Struct2;
298
299     // Enums
300     let simple_enum_1 = Variant1;
301     let simple_enum_2 = Variant2(0);
302     let simple_enum_3 = mod1::Variant2(Struct1);
303
304     let generic_enum_1: mod1::mod2::Enum3<mod1::Struct2> = mod1::mod2::Variant1;
305     let generic_enum_2 = mod1::mod2::Variant2(Struct1);
306
307     // Tuples
308     let tuple1 = (8u32, Struct1, mod1::mod2::Variant2(mod1::Struct2));
309     let tuple2 = ((Struct1, mod1::mod2::Struct3), mod1::Variant1, 'x');
310
311     // Box
312     let box1 = (box 1f32, 0i32);
313     let box2 = (box mod1::mod2::Variant2(1f32), 0i32);
314
315     // References
316     let ref1 = (&Struct1, 0i32);
317     let ref2 = (&GenericStruct::<char, Struct1>(PhantomData), 0i32);
318
319     let mut mut_struct1 = Struct1;
320     let mut mut_generic_struct = GenericStruct::<mod1::Enum2, f64>(PhantomData);
321     let mut_ref1 = (&mut mut_struct1, 0i32);
322     let mut_ref2 = (&mut mut_generic_struct, 0i32);
323
324     // Raw Pointers
325     let mut_ptr1: (*mut Struct1, isize) = (ptr::null_mut(), 0);
326     let mut_ptr2: (*mut isize, isize) = (ptr::null_mut(), 0);
327     let mut_ptr3: (*mut mod1::mod2::Enum3<Struct1>, isize) = (ptr::null_mut(), 0);
328
329     let const_ptr1: (*const Struct1, isize) = (ptr::null(), 0);
330     let const_ptr2: (*const isize, isize) = (ptr::null(), 0);
331     let const_ptr3: (*const mod1::mod2::Enum3<Struct1>, isize) = (ptr::null(), 0);
332
333     // Vectors
334     let fixed_size_vec1 = ([Struct1, Struct1, Struct1], 0i16);
335     let fixed_size_vec2 = ([0_usize, 1, 2], 0i16);
336
337     let vec1 = vec![0_usize, 2, 3];
338     let slice1 = &*vec1;
339     let vec2 = vec![mod1::Variant2(Struct1)];
340     let slice2 = &*vec2;
341
342     // Trait Objects
343     let box_trait = (box 0_isize) as Box<Trait1>;
344     let ref_trait = &0_isize as &Trait1;
345     let mut mut_int1 = 0_isize;
346     let mut_ref_trait = (&mut mut_int1) as &mut Trait1;
347
348     let generic_box_trait = (box 0_isize) as Box<Trait2<i32, mod1::Struct2>>;
349     let generic_ref_trait  = (&0_isize) as &Trait2<Struct1, Struct1>;
350
351     let mut generic_mut_ref_trait_impl = 0_isize;
352     let generic_mut_ref_trait = (&mut generic_mut_ref_trait_impl) as
353         &mut Trait2<mod1::mod2::Struct3, GenericStruct<usize, isize>>;
354
355     // Bare Functions
356     let rust_fn = (rust_fn, 0_usize);
357     let extern_c_fn = (extern_c_fn, 0_usize);
358     let unsafe_fn = (unsafe_fn, 0_usize);
359     let extern_stdcall_fn = (extern_stdcall_fn, 0_usize);
360
361     let rust_fn_with_return_value = (rust_fn_with_return_value, 0_usize);
362     let extern_c_fn_with_return_value = (extern_c_fn_with_return_value, 0_usize);
363     let unsafe_fn_with_return_value = (unsafe_fn_with_return_value, 0_usize);
364     let extern_stdcall_fn_with_return_value = (extern_stdcall_fn_with_return_value, 0_usize);
365
366     let generic_function_int = (generic_function::<isize>, 0_usize);
367     let generic_function_struct3 = (generic_function::<mod1::mod2::Struct3>, 0_usize);
368
369     let variadic_function = (printf, 0_usize);
370
371     // Closures
372     // I (mw) am a bit unclear about the current state of closures, their
373     // various forms (boxed, unboxed, proc, capture-by-ref, by-val, once) and
374     // how that maps to rustc's internal representation of these forms.
375     // Once closures have reached their 1.0 form, the tests below should
376     // probably be expanded.
377     let closure1 = (|x:isize| {}, 0_usize);
378     let closure2 = (|x:i8, y: f32| { (x as f32) + y }, 0_usize);
379
380     zzz(); // #break
381 }
382
383 #[inline(never)]
384 fn zzz() { () }