]> git.lizzy.rs Git - rust.git/blob - src/test/debuginfo/struct-with-destructor.rs
Merge commit '1fcc74cc9e03bc91eaa80ecf92976b0b14b3aeb6' into clippyup
[rust.git] / src / test / debuginfo / struct-with-destructor.rs
1 // ignore-tidy-linelength
2
3 // min-lldb-version: 310
4
5 // compile-flags:-g
6
7 // === GDB TESTS ===================================================================================
8
9 // gdb-command:run
10 // gdb-command:print simple
11 // gdbg-check:$1 = {x = 10, y = 20}
12 // gdbr-check:$1 = struct_with_destructor::WithDestructor {x: 10, y: 20}
13
14 // gdb-command:print noDestructor
15 // gdbg-check:$2 = {a = {x = 10, y = 20}, guard = -1}
16 // gdbr-check:$2 = struct_with_destructor::NoDestructorGuarded {a: struct_with_destructor::NoDestructor {x: 10, y: 20}, guard: -1}
17
18 // gdb-command:print withDestructor
19 // gdbg-check:$3 = {a = {x = 10, y = 20}, guard = -1}
20 // gdbr-check:$3 = struct_with_destructor::WithDestructorGuarded {a: struct_with_destructor::WithDestructor {x: 10, y: 20}, guard: -1}
21
22 // gdb-command:print nested
23 // gdbg-check:$4 = {a = {a = {x = 7890, y = 9870}}}
24 // gdbr-check:$4 = struct_with_destructor::NestedOuter {a: struct_with_destructor::NestedInner {a: struct_with_destructor::WithDestructor {x: 7890, y: 9870}}}
25
26
27 // === LLDB TESTS ==================================================================================
28
29 // lldb-command:run
30 // lldb-command:print simple
31 // lldbg-check:[...]$0 = { x = 10 y = 20 }
32 // lldbr-check:(struct_with_destructor::WithDestructor) simple = { x = 10 y = 20 }
33
34 // lldb-command:print noDestructor
35 // lldbg-check:[...]$1 = { a = { x = 10 y = 20 } guard = -1 }
36 // lldbr-check:(struct_with_destructor::NoDestructorGuarded) noDestructor = { a = { x = 10 y = 20 } guard = -1 }
37
38 // lldb-command:print withDestructor
39 // lldbg-check:[...]$2 = { a = { x = 10 y = 20 } guard = -1 }
40 // lldbr-check:(struct_with_destructor::WithDestructorGuarded) withDestructor = { a = { x = 10 y = 20 } guard = -1 }
41
42 // lldb-command:print nested
43 // lldbg-check:[...]$3 = { a = { a = { x = 7890 y = 9870 } } }
44 // lldbr-check:(struct_with_destructor::NestedOuter) nested = { a = { a = { x = 7890 y = 9870 } } }
45
46 #![allow(unused_variables)]
47 #![feature(omit_gdb_pretty_printer_section)]
48 #![omit_gdb_pretty_printer_section]
49
50 struct NoDestructor {
51     x: i32,
52     y: i64
53 }
54
55 struct WithDestructor {
56     x: i32,
57     y: i64
58 }
59
60 impl Drop for WithDestructor {
61     fn drop(&mut self) {}
62 }
63
64 struct NoDestructorGuarded {
65     a: NoDestructor,
66     guard: i64
67 }
68
69 struct WithDestructorGuarded {
70     a: WithDestructor,
71     guard: i64
72 }
73
74 struct NestedInner {
75     a: WithDestructor
76 }
77
78 impl Drop for NestedInner {
79     fn drop(&mut self) {}
80 }
81
82 struct NestedOuter {
83     a: NestedInner
84 }
85
86
87 // The compiler adds a 'destructed' boolean field to structs implementing Drop. This field is used
88 // at runtime to prevent drop() to be executed more than once.
89 // This field must be incorporated by the debug info generation. Otherwise the debugger assumes a
90 // wrong size/layout for the struct.
91 fn main() {
92
93     let simple = WithDestructor { x: 10, y: 20 };
94
95     let noDestructor = NoDestructorGuarded {
96         a: NoDestructor { x: 10, y: 20 },
97         guard: -1
98     };
99
100     // If the destructor flag field is not incorporated into the debug info for 'WithDestructor'
101     // then the debugger will have an invalid offset for the field 'guard' and thus should not be
102     // able to read its value correctly (dots are padding bytes, D is the boolean destructor flag):
103     //
104     // 64 bit
105     //
106     // NoDestructorGuarded = 0000....00000000FFFFFFFF
107     //                       <--------------><------>
108     //                         NoDestructor   guard
109     //
110     //
111     // withDestructorGuarded = 0000....00000000D.......FFFFFFFF
112     //                         <--------------><------>          // How debug info says it is
113     //                          WithDestructor  guard
114     //
115     //                         <----------------------><------>  // How it actually is
116     //                              WithDestructor      guard
117     //
118     // 32 bit
119     //
120     // NoDestructorGuarded = 000000000000FFFFFFFF
121     //                       <----------><------>
122     //                       NoDestructor guard
123     //
124     //
125     // withDestructorGuarded = 000000000000D...FFFFFFFF
126     //                         <----------><------>      // How debug info says it is
127     //                      WithDestructor  guard
128     //
129     //                         <--------------><------>  // How it actually is
130     //                          WithDestructor  guard
131     //
132     let withDestructor = WithDestructorGuarded {
133         a: WithDestructor { x: 10, y: 20 },
134         guard: -1
135     };
136
137     // expected layout (64 bit) = xxxx....yyyyyyyyD.......D...
138     //                            <--WithDestructor------>
139     //                            <-------NestedInner-------->
140     //                            <-------NestedOuter-------->
141     let nested = NestedOuter { a: NestedInner { a: WithDestructor { x: 7890, y: 9870 } } };
142
143     zzz(); // #break
144 }
145
146 fn zzz() {()}