diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
commit | 698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch) | |
tree | 173a775858bd501c378080a10dca74132f05bc50 /src/test/ui/union/union-unsafe.rs | |
parent | Initial commit. (diff) | |
download | rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip |
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/test/ui/union/union-unsafe.rs')
-rw-r--r-- | src/test/ui/union/union-unsafe.rs | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/src/test/ui/union/union-unsafe.rs b/src/test/ui/union/union-unsafe.rs new file mode 100644 index 000000000..5e1837a90 --- /dev/null +++ b/src/test/ui/union/union-unsafe.rs @@ -0,0 +1,78 @@ +// revisions: mir thir +// [thir]compile-flags: -Z thir-unsafeck + +use std::mem::ManuallyDrop; +use std::cell::RefCell; + +union U1 { + a: u8 +} + +union U2 { + a: ManuallyDrop<String> +} + +union U3<T> { + a: ManuallyDrop<T> +} + +union U4<T: Copy> { + a: T +} + +union URef { + p: &'static mut i32, +} + +union URefCell { // field that does not drop but is not `Copy`, either + a: (ManuallyDrop<RefCell<i32>>, i32), +} + +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) { + // FIXME(thir-unsafeck) + u.a = (ManuallyDrop::new(RefCell::new(0)), 1); // OK (assignment does not drop) + u.a.0 = ManuallyDrop::new(RefCell::new(0)); // OK (assignment does not drop) + u.a.1 = 1; // OK +} + +fn generic_noncopy<T: Default>() { + let mut u3 = U3 { a: ManuallyDrop::new(T::default()) }; + u3.a = ManuallyDrop::new(T::default()); // OK (assignment does not drop) + *u3.a = T::default(); //~ ERROR access to union field is unsafe +} + +fn generic_copy<T: Copy + Default>() { + 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 + + let mut u4 = U4 { a: T::default() }; + u4.a = T::default(); // OK +} + +fn main() { + let mut u1 = U1 { a: 10 }; // OK + let a = u1.a; //~ ERROR access to union field is unsafe + u1.a = 11; // OK + + let U1 { a } = u1; //~ ERROR access to union field is unsafe + if let U1 { a: 12 } = u1 {} //~ ERROR access to union field is unsafe + // let U1 { .. } = u1; // OK + + let mut u2 = U2 { a: ManuallyDrop::new(String::from("old")) }; // OK + u2.a = ManuallyDrop::new(String::from("new")); // OK (assignment does not drop) + *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 + u3.a = ManuallyDrop::new(String::from("new")); // OK (assignment does not drop) + *u3.a = String::from("new"); //~ ERROR access to union field is unsafe +} |