]> git.lizzy.rs Git - rust.git/blob - src/test/ui/union/union-drop.rs
Re-use std::sealed::Sealed in os/linux/process.
[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 #[derive(Copy, Clone)]
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
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         }
46         assert_eq!(CHECK, 2); // 2, dtor of W is called
47         {
48             let y = Y { a: S };
49         }
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
56     }
57 }