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