2018-09-06 12:41:12 +00:00
|
|
|
//@ run-pass
|
2021-08-03 19:11:04 +00:00
|
|
|
|
2018-09-25 21:51:35 +00:00
|
|
|
#![allow(dead_code)]
|
|
|
|
#![allow(unused_variables)]
|
2024-08-24 03:49:09 +00:00
|
|
|
// FIXME(static_mut_refs): this could use an atomic
|
|
|
|
#![allow(static_mut_refs)]
|
2018-09-06 12:41:12 +00:00
|
|
|
|
2016-08-18 15:31:47 +00:00
|
|
|
// Drop works for union itself.
|
|
|
|
|
2020-10-04 20:24:14 +00:00
|
|
|
#[derive(Copy, Clone)]
|
2016-08-19 16:20:30 +00:00
|
|
|
struct S;
|
|
|
|
|
2016-08-18 15:31:47 +00:00
|
|
|
union U {
|
|
|
|
a: u8
|
|
|
|
}
|
|
|
|
|
2016-08-19 16:20:30 +00:00
|
|
|
union W {
|
|
|
|
a: S,
|
|
|
|
}
|
|
|
|
|
|
|
|
union Y {
|
|
|
|
a: S,
|
|
|
|
}
|
|
|
|
|
2016-08-18 15:31:47 +00:00
|
|
|
impl Drop for U {
|
2016-08-19 16:20:30 +00:00
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe { CHECK += 1; }
|
|
|
|
}
|
2016-08-18 15:31:47 +00:00
|
|
|
}
|
|
|
|
|
2016-08-19 16:20:30 +00:00
|
|
|
impl Drop for W {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe { CHECK += 1; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static mut CHECK: u8 = 0;
|
|
|
|
|
2016-08-18 15:31:47 +00:00
|
|
|
fn main() {
|
2016-08-19 16:20:30 +00:00
|
|
|
unsafe {
|
|
|
|
assert_eq!(CHECK, 0);
|
|
|
|
{
|
|
|
|
let u = U { a: 1 };
|
|
|
|
}
|
|
|
|
assert_eq!(CHECK, 1); // 1, dtor of U is called
|
|
|
|
{
|
|
|
|
let w = W { a: S };
|
|
|
|
}
|
2019-07-03 10:35:02 +00:00
|
|
|
assert_eq!(CHECK, 2); // 2, dtor of W is called
|
2016-08-19 16:20:30 +00:00
|
|
|
{
|
|
|
|
let y = Y { a: S };
|
|
|
|
}
|
2020-08-30 16:59:57 +00:00
|
|
|
assert_eq!(CHECK, 2); // 2, Y has no dtor
|
2020-08-15 11:04:32 +00:00
|
|
|
{
|
2020-08-30 16:59:57 +00:00
|
|
|
let u2 = U { a: 1 };
|
|
|
|
std::mem::forget(u2);
|
2020-08-15 11:04:32 +00:00
|
|
|
}
|
2020-08-30 16:59:57 +00:00
|
|
|
assert_eq!(CHECK, 2); // 2, dtor of U *not* called for u2
|
2016-08-19 16:20:30 +00:00
|
|
|
}
|
2016-08-18 15:31:47 +00:00
|
|
|
}
|