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