2021-05-13 09:42:25 -05:00
|
|
|
// revisions: mir thir
|
|
|
|
// [thir]compile-flags: -Z thir-unsafeck
|
|
|
|
|
2018-12-19 13:58:20 -06:00
|
|
|
use std::mem::ManuallyDrop;
|
2020-10-24 14:12:32 -05:00
|
|
|
use std::cell::RefCell;
|
2016-08-18 12:12:28 -05:00
|
|
|
|
2017-05-18 05:40:15 -05:00
|
|
|
union U1 {
|
2016-08-18 12:12:28 -05:00
|
|
|
a: u8
|
|
|
|
}
|
|
|
|
|
2017-05-18 05:40:15 -05:00
|
|
|
union U2 {
|
2018-12-19 13:58:20 -06:00
|
|
|
a: ManuallyDrop<String>
|
2017-05-18 05:40:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
union U3<T> {
|
2018-12-19 13:58:20 -06:00
|
|
|
a: ManuallyDrop<T>
|
2017-05-18 05:40:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
union U4<T: Copy> {
|
|
|
|
a: T
|
|
|
|
}
|
|
|
|
|
2020-10-24 14:12:32 -05:00
|
|
|
union URef {
|
|
|
|
p: &'static mut i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
union URefCell { // field that does not drop but is not `Copy`, either
|
2022-06-29 21:33:18 -05:00
|
|
|
a: (ManuallyDrop<RefCell<i32>>, i32),
|
2020-10-24 14:12:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn deref_union_field(mut u: URef) {
|
|
|
|
// Not an assignment but an access to the union field!
|
|
|
|
*(u.p) = 13; //~ ERROR access to union field is unsafe
|
|
|
|
}
|
|
|
|
|
|
|
|
fn assign_noncopy_union_field(mut u: URefCell) {
|
2021-05-13 09:42:25 -05:00
|
|
|
// FIXME(thir-unsafeck)
|
2022-06-30 07:40:06 -05:00
|
|
|
u.a = (ManuallyDrop::new(RefCell::new(0)), 1); // OK (assignment does not drop)
|
2022-06-29 21:33:18 -05:00
|
|
|
u.a.0 = ManuallyDrop::new(RefCell::new(0)); // OK (assignment does not drop)
|
2020-10-24 14:12:32 -05:00
|
|
|
u.a.1 = 1; // OK
|
|
|
|
}
|
|
|
|
|
2017-05-18 05:40:15 -05:00
|
|
|
fn generic_noncopy<T: Default>() {
|
2018-12-19 13:58:20 -06:00
|
|
|
let mut u3 = U3 { a: ManuallyDrop::new(T::default()) };
|
2020-10-18 06:53:54 -05:00
|
|
|
u3.a = ManuallyDrop::new(T::default()); // OK (assignment does not drop)
|
2018-12-19 13:58:20 -06:00
|
|
|
*u3.a = T::default(); //~ ERROR access to union field is unsafe
|
2017-05-18 05:40:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn generic_copy<T: Copy + Default>() {
|
2018-12-19 13:58:20 -06:00
|
|
|
let mut u3 = U3 { a: ManuallyDrop::new(T::default()) };
|
|
|
|
u3.a = ManuallyDrop::new(T::default()); // OK
|
|
|
|
*u3.a = T::default(); //~ ERROR access to union field is unsafe
|
|
|
|
|
2017-05-18 05:40:15 -05:00
|
|
|
let mut u4 = U4 { a: T::default() };
|
|
|
|
u4.a = T::default(); // OK
|
|
|
|
}
|
|
|
|
|
2016-08-18 12:12:28 -05:00
|
|
|
fn main() {
|
2017-05-18 05:40:15 -05:00
|
|
|
let mut u1 = U1 { a: 10 }; // OK
|
2018-07-10 03:52:05 -05:00
|
|
|
let a = u1.a; //~ ERROR access to union field is unsafe
|
2017-05-18 05:40:15 -05:00
|
|
|
u1.a = 11; // OK
|
2018-12-19 13:58:20 -06:00
|
|
|
|
2018-07-10 03:52:05 -05:00
|
|
|
let U1 { a } = u1; //~ ERROR access to union field is unsafe
|
|
|
|
if let U1 { a: 12 } = u1 {} //~ ERROR access to union field is unsafe
|
2017-05-18 05:40:15 -05:00
|
|
|
// let U1 { .. } = u1; // OK
|
|
|
|
|
2018-12-19 13:58:20 -06:00
|
|
|
let mut u2 = U2 { a: ManuallyDrop::new(String::from("old")) }; // OK
|
2020-10-18 06:53:54 -05:00
|
|
|
u2.a = ManuallyDrop::new(String::from("new")); // OK (assignment does not drop)
|
2018-12-19 13:58:20 -06:00
|
|
|
*u2.a = String::from("new"); //~ ERROR access to union field is unsafe
|
|
|
|
|
|
|
|
let mut u3 = U3 { a: ManuallyDrop::new(0) }; // OK
|
|
|
|
u3.a = ManuallyDrop::new(1); // OK
|
|
|
|
*u3.a = 1; //~ ERROR access to union field is unsafe
|
|
|
|
|
|
|
|
let mut u3 = U3 { a: ManuallyDrop::new(String::from("old")) }; // OK
|
2020-10-18 06:53:54 -05:00
|
|
|
u3.a = ManuallyDrop::new(String::from("new")); // OK (assignment does not drop)
|
2018-12-19 13:58:20 -06:00
|
|
|
*u3.a = String::from("new"); //~ ERROR access to union field is unsafe
|
2016-08-18 12:12:28 -05:00
|
|
|
}
|