]> git.lizzy.rs Git - rust.git/blob - src/test/debug-info/struct-with-destructor.rs
8994a582b58fc7cabd337939d60a6037e1a1aac7
[rust.git] / src / test / debug-info / struct-with-destructor.rs
1 // Copyright 2013 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 // compile-flags:-Z extra-debug-info
12 // debugger:rbreak zzz
13 // debugger:run
14 // debugger:finish
15 // debugger:print simple
16 // check:$1 = {x = 10, y = 20}
17
18 // debugger:print noDestructor
19 // check:$2 = {a = {x = 10, y = 20}, guard = -1}
20
21 // debugger:print withDestructor
22 // check:$3 = {a = {x = 10, y = 20}, guard = -1}
23
24 // debugger:print nested
25 // check:$4 = {a = {a = {x = 7890, y = 9870}}}
26
27 #[allow(unused_variable)];
28
29 struct NoDestructor {
30     x: i32,
31     y: i64
32 }
33
34 struct WithDestructor {
35     x: i32,
36     y: i64
37 }
38
39 impl Drop for WithDestructor {
40     fn drop(&self) {}
41 }
42
43 struct NoDestructorGuarded {
44     a: NoDestructor,
45     guard: i64
46 }
47
48 struct WithDestructorGuarded {
49     a: WithDestructor,
50     guard: i64
51 }
52
53 struct NestedInner {
54     a: WithDestructor
55 }
56
57 impl Drop for NestedInner {
58     fn drop(&self) {}
59 }
60
61 struct NestedOuter {
62     a: NestedInner
63 }
64
65
66 // The compiler adds a 'destructed' boolean field to structs implementing Drop. This field is used
67 // at runtime to prevent drop() to be executed more than once (see middle::trans::adt).
68 // This field must be incorporated by the debug info generation. Otherwise the debugger assumes a
69 // wrong size/layout for the struct.
70 fn main() {
71
72     let simple = WithDestructor { x: 10, y: 20 };
73
74     let noDestructor = NoDestructorGuarded {
75         a: NoDestructor { x: 10, y: 20 },
76         guard: -1
77     };
78
79     // If the destructor flag field is not incorporated into the debug info for 'WithDestructor'
80     // then the debugger will have an invalid offset for the field 'guard' and thus should not be
81     // able to read its value correctly (dots are padding bytes, D is the boolean destructor flag):
82     //
83     // 64 bit
84     //
85     // NoDestructorGuarded = 0000....00000000FFFFFFFF
86     //                       <--------------><------>
87     //                         NoDestructor   guard
88     //
89     //
90     // withDestructorGuarded = 0000....00000000D.......FFFFFFFF
91     //                         <--------------><------>          // How debug info says it is
92     //                          WithDestructor  guard
93     //
94     //                         <----------------------><------>  // How it actually is
95     //                              WithDestructor      guard
96     //
97     // 32 bit
98     //
99     // NoDestructorGuarded = 000000000000FFFFFFFF
100     //                       <----------><------>
101     //                       NoDestructor guard
102     //
103     //
104     // withDestructorGuarded = 000000000000D...FFFFFFFF
105     //                         <----------><------>      // How debug info says it is
106     //                      WithDestructor  guard
107     //
108     //                         <--------------><------>  // How it actually is
109     //                          WithDestructor  guard
110     //
111     let withDestructor = WithDestructorGuarded {
112         a: WithDestructor { x: 10, y: 20 },
113         guard: -1
114     };
115
116     // expected layout (64 bit) = xxxx....yyyyyyyyD.......D...
117     //                            <--WithDestructor------>
118     //                            <-------NestedInner-------->
119     //                            <-------NestedOuter-------->
120     let nested = NestedOuter { a: NestedInner { a: WithDestructor { x: 7890, y: 9870 } } };
121
122     zzz();
123 }
124
125 fn zzz() {()}