]> git.lizzy.rs Git - rust.git/blob - src/test/ui/union/union-drop-assign.rs
Rollup merge of #100462 - zohnannor:master, r=thomcc
[rust.git] / src / test / ui / union / union-drop-assign.rs
1 // run-pass
2 #![allow(unused_assignments)]
3
4 // Drop works for union itself.
5
6 use std::mem::ManuallyDrop;
7
8 struct S;
9
10 union U {
11     a: ManuallyDrop<S>
12 }
13
14 impl Drop for S {
15     fn drop(&mut self) {
16         unsafe { CHECK += 10; }
17     }
18 }
19
20 impl Drop for U {
21     fn drop(&mut self) {
22         unsafe { CHECK += 1; }
23     }
24 }
25
26 static mut CHECK: u8 = 0;
27
28 fn main() {
29     unsafe {
30         let mut u = U { a: ManuallyDrop::new(S) };
31         assert_eq!(CHECK, 0);
32         u = U { a: ManuallyDrop::new(S) };
33         assert_eq!(CHECK, 1); // union itself is assigned, union is dropped, field is not dropped
34         *u.a = S;
35         assert_eq!(CHECK, 11); // union field is assigned, field is dropped
36     }
37 }