2018-09-06 07:41:12 -05:00
|
|
|
// run-pass
|
2018-09-25 16:51:35 -05:00
|
|
|
#![allow(unused_assignments)]
|
2018-09-06 07:41:12 -05:00
|
|
|
|
2016-08-26 08:54:58 -05:00
|
|
|
// Drop works for union itself.
|
|
|
|
|
2019-07-03 05:35:02 -05:00
|
|
|
use std::mem::ManuallyDrop;
|
|
|
|
|
2016-08-26 08:54:58 -05:00
|
|
|
struct S;
|
|
|
|
|
|
|
|
union U {
|
2019-07-03 05:35:02 -05:00
|
|
|
a: ManuallyDrop<S>
|
2016-08-26 08:54:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for S {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe { CHECK += 10; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for U {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe { CHECK += 1; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static mut CHECK: u8 = 0;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
unsafe {
|
2019-07-03 05:35:02 -05:00
|
|
|
let mut u = U { a: ManuallyDrop::new(S) };
|
2016-08-26 08:54:58 -05:00
|
|
|
assert_eq!(CHECK, 0);
|
2019-07-03 05:35:02 -05:00
|
|
|
u = U { a: ManuallyDrop::new(S) };
|
2016-08-26 08:54:58 -05:00
|
|
|
assert_eq!(CHECK, 1); // union itself is assigned, union is dropped, field is not dropped
|
2019-07-03 05:35:02 -05:00
|
|
|
*u.a = S;
|
2016-08-26 08:54:58 -05:00
|
|
|
assert_eq!(CHECK, 11); // union field is assigned, field is dropped
|
|
|
|
}
|
|
|
|
}
|