]> git.lizzy.rs Git - rust.git/blob - src/test/debuginfo/struct-with-destructor.rs
rollup merge of #20350: fhahn/issue-20340-rustdoc-version
[rust.git] / src / test / debuginfo / struct-with-destructor.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-android: FIXME(#10381)
12 // min-lldb-version: 310
13
14 // compile-flags:-g
15
16 // === GDB TESTS ===================================================================================
17
18 // gdb-command:run
19 // gdb-command:print simple
20 // gdb-check:$1 = {x = 10, y = 20}
21
22 // gdb-command:print noDestructor
23 // gdb-check:$2 = {a = {x = 10, y = 20}, guard = -1}
24
25 // gdb-command:print withDestructor
26 // gdb-check:$3 = {a = {x = 10, y = 20}, guard = -1}
27
28 // gdb-command:print nested
29 // gdb-check:$4 = {a = {a = {x = 7890, y = 9870}}}
30
31
32 // === LLDB TESTS ==================================================================================
33
34 // lldb-command:run
35 // lldb-command:print simple
36 // lldb-check:[...]$0 = WithDestructor { x: 10, y: 20 }
37
38 // lldb-command:print noDestructor
39 // lldb-check:[...]$1 = NoDestructorGuarded { a: NoDestructor { x: 10, y: 20 }, guard: -1 }
40
41 // lldb-command:print withDestructor
42 // lldb-check:[...]$2 = WithDestructorGuarded { a: WithDestructor { x: 10, y: 20 }, guard: -1 }
43
44 // lldb-command:print nested
45 // lldb-check:[...]$3 = NestedOuter { a: NestedInner { a: WithDestructor { x: 7890, y: 9870 } } }
46
47 #![allow(unused_variables)]
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 (see middle::trans::adt).
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() {()}