]>
Commit | Line | Data |
---|---|---|
b7449926 | 1 | // run-pass |
0bf4aa26 XL |
2 | #![allow(dead_code)] |
3 | #![allow(unused_variables)] | |
b7449926 | 4 | |
9e0c209e SL |
5 | // Drop works for union itself. |
6 | ||
29967ef6 | 7 | #[derive(Copy, Clone)] |
9e0c209e SL |
8 | struct S; |
9 | ||
10 | union U { | |
11 | a: u8 | |
12 | } | |
13 | ||
14 | union W { | |
15 | a: S, | |
16 | } | |
17 | ||
18 | union Y { | |
19 | a: S, | |
20 | } | |
21 | ||
9e0c209e SL |
22 | impl Drop for U { |
23 | fn drop(&mut self) { | |
24 | unsafe { CHECK += 1; } | |
25 | } | |
26 | } | |
27 | ||
28 | impl Drop for W { | |
29 | fn drop(&mut self) { | |
30 | unsafe { CHECK += 1; } | |
31 | } | |
32 | } | |
33 | ||
34 | static mut CHECK: u8 = 0; | |
35 | ||
36 | fn main() { | |
37 | unsafe { | |
38 | assert_eq!(CHECK, 0); | |
39 | { | |
40 | let u = U { a: 1 }; | |
41 | } | |
42 | assert_eq!(CHECK, 1); // 1, dtor of U is called | |
43 | { | |
44 | let w = W { a: S }; | |
45 | } | |
e74abb32 | 46 | assert_eq!(CHECK, 2); // 2, dtor of W is called |
9e0c209e SL |
47 | { |
48 | let y = Y { a: S }; | |
49 | } | |
1b1a35ee XL |
50 | assert_eq!(CHECK, 2); // 2, Y has no dtor |
51 | { | |
52 | let u2 = U { a: 1 }; | |
53 | std::mem::forget(u2); | |
54 | } | |
55 | assert_eq!(CHECK, 2); // 2, dtor of U *not* called for u2 | |
9e0c209e SL |
56 | } |
57 | } |