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