]> git.lizzy.rs Git - rust.git/blob - src/test/ui/structs-enums/field-destruction-order.rs
Rollup merge of #89468 - FabianWolff:issue-89358, r=jackh726
[rust.git] / src / test / ui / structs-enums / field-destruction-order.rs
1 // run-pass
2 #![allow(dead_code)]
3 #![allow(non_upper_case_globals)]
4
5 // In theory, it doesn't matter what order destructors are run in for rust
6 // because we have explicit ownership of values meaning that there's no need to
7 // run one before another. With unsafe code, however, there may be a safe
8 // interface which relies on fields having their destructors run in a particular
9 // order. At the time of this writing, std::rt::sched::Scheduler is an example
10 // of a structure which contains unsafe handles to FFI-like types, and the
11 // destruction order of the fields matters in the sense that some handles need
12 // to get destroyed before others.
13 //
14 // In C++, destruction order happens bottom-to-top in order of field
15 // declarations, but we currently run them top-to-bottom. I don't think the
16 // order really matters that much as long as we define what it is.
17
18
19 struct A;
20 struct B;
21 struct C {
22     a: A,
23     b: B,
24 }
25
26 static mut hit: bool = false;
27
28 impl Drop for A {
29     fn drop(&mut self) {
30         unsafe {
31             assert!(!hit);
32             hit = true;
33         }
34     }
35 }
36
37 impl Drop for B {
38     fn drop(&mut self) {
39         unsafe {
40             assert!(hit);
41         }
42     }
43 }
44
45 pub fn main() {
46     let _c = C { a: A, b: B };
47 }