2 // [thir]compile-flags: -Z thir-unsafeck
4 #![feature(untagged_unions)]
5 use std::mem::ManuallyDrop;
6 use std::cell::RefCell;
13 a: ManuallyDrop<String>
28 union URefCell { // field that does not drop but is not `Copy`, either
29 a: (RefCell<i32>, i32),
32 fn deref_union_field(mut u: URef) {
33 // Not an assignment but an access to the union field!
34 *(u.p) = 13; //~ ERROR access to union field is unsafe
37 fn assign_noncopy_union_field(mut u: URefCell) {
38 // FIXME(thir-unsafeck)
39 u.a = (RefCell::new(0), 1); //~ ERROR assignment to union field that might need dropping
40 u.a.0 = RefCell::new(0); //~ ERROR assignment to union field that might need dropping
44 fn generic_noncopy<T: Default>() {
45 let mut u3 = U3 { a: ManuallyDrop::new(T::default()) };
46 u3.a = ManuallyDrop::new(T::default()); // OK (assignment does not drop)
47 *u3.a = T::default(); //~ ERROR access to union field is unsafe
50 fn generic_copy<T: Copy + Default>() {
51 let mut u3 = U3 { a: ManuallyDrop::new(T::default()) };
52 u3.a = ManuallyDrop::new(T::default()); // OK
53 *u3.a = T::default(); //~ ERROR access to union field is unsafe
55 let mut u4 = U4 { a: T::default() };
56 u4.a = T::default(); // OK
60 let mut u1 = U1 { a: 10 }; // OK
61 let a = u1.a; //~ ERROR access to union field is unsafe
64 let U1 { a } = u1; //~ ERROR access to union field is unsafe
65 if let U1 { a: 12 } = u1 {} //~ ERROR access to union field is unsafe
66 // let U1 { .. } = u1; // OK
68 let mut u2 = U2 { a: ManuallyDrop::new(String::from("old")) }; // OK
69 u2.a = ManuallyDrop::new(String::from("new")); // OK (assignment does not drop)
70 *u2.a = String::from("new"); //~ ERROR access to union field is unsafe
72 let mut u3 = U3 { a: ManuallyDrop::new(0) }; // OK
73 u3.a = ManuallyDrop::new(1); // OK
74 *u3.a = 1; //~ ERROR access to union field is unsafe
76 let mut u3 = U3 { a: ManuallyDrop::new(String::from("old")) }; // OK
77 u3.a = ManuallyDrop::new(String::from("new")); // OK (assignment does not drop)
78 *u3.a = String::from("new"); //~ ERROR access to union field is unsafe