]> git.lizzy.rs Git - rust.git/blob - src/test/ui/union/union-drop.rs
Rollup merge of #100462 - zohnannor:master, r=thomcc
[rust.git] / src / test / ui / union / union-drop.rs
1 // run-pass
2 // revisions: mirunsafeck thirunsafeck
3 // [thirunsafeck]compile-flags: -Z thir-unsafeck
4
5 #![allow(dead_code)]
6 #![allow(unused_variables)]
7
8 // Drop works for union itself.
9
10 #[derive(Copy, Clone)]
11 struct S;
12
13 union U {
14     a: u8
15 }
16
17 union W {
18     a: S,
19 }
20
21 union Y {
22     a: S,
23 }
24
25 impl Drop for U {
26     fn drop(&mut self) {
27         unsafe { CHECK += 1; }
28     }
29 }
30
31 impl Drop for W {
32     fn drop(&mut self) {
33         unsafe { CHECK += 1; }
34     }
35 }
36
37 static mut CHECK: u8 = 0;
38
39 fn main() {
40     unsafe {
41         assert_eq!(CHECK, 0);
42         {
43             let u = U { a: 1 };
44         }
45         assert_eq!(CHECK, 1); // 1, dtor of U is called
46         {
47             let w = W { a: S };
48         }
49         assert_eq!(CHECK, 2); // 2, dtor of W is called
50         {
51             let y = Y { a: S };
52         }
53         assert_eq!(CHECK, 2); // 2, Y has no dtor
54         {
55             let u2 = U { a: 1 };
56             std::mem::forget(u2);
57         }
58         assert_eq!(CHECK, 2); // 2, dtor of U *not* called for u2
59     }
60 }