summaryrefslogtreecommitdiffstats
path: root/src/test/ui/union/union-overwrite.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /src/test/ui/union/union-overwrite.rs
parentInitial commit. (diff)
downloadrustc-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-overwrite.rs')
-rw-r--r--src/test/ui/union/union-overwrite.rs80
1 files changed, 80 insertions, 0 deletions
diff --git a/src/test/ui/union/union-overwrite.rs b/src/test/ui/union/union-overwrite.rs
new file mode 100644
index 000000000..0eea14d9d
--- /dev/null
+++ b/src/test/ui/union/union-overwrite.rs
@@ -0,0 +1,80 @@
+// run-pass
+// revisions: mirunsafeck thirunsafeck
+// [thirunsafeck]compile-flags: -Z thir-unsafeck
+
+#[repr(C)]
+#[derive(Copy, Clone)]
+struct Pair<T, U>(T, U);
+#[repr(C)]
+#[derive(Copy, Clone)]
+struct Triple<T>(T, T, T);
+
+#[repr(C)]
+union U<A, B>
+where
+ A: Copy, B: Copy
+{
+ a: Pair<A, A>,
+ b: B,
+}
+
+#[repr(C)]
+union W<A, B>
+where
+ A: Copy, B: Copy
+{
+ a: A,
+ b: B,
+}
+
+#[cfg(target_endian = "little")]
+unsafe fn check() {
+ let mut u = U::<u8, u16> { b: 0xDE_DE };
+ u.a.0 = 0xBE;
+ assert_eq!(u.b, 0xDE_BE);
+
+ let mut u = U::<u16, u32> { b: 0xDEAD_DEAD };
+ u.a.0 = 0xBEEF;
+ assert_eq!(u.b, 0xDEAD_BEEF);
+
+ let mut u = U::<u32, u64> { b: 0xDEADBEEF_DEADBEEF };
+ u.a.0 = 0xBAADF00D;
+ assert_eq!(u.b, 0xDEADBEEF_BAADF00D);
+
+ let mut w = W::<Pair<Triple<u8>, u8>, u32> { b: 0xDEAD_DEAD };
+ w.a.0 = Triple(0, 0, 0);
+ assert_eq!(w.b, 0xDE00_0000);
+
+ let mut w = W::<Pair<u8, Triple<u8>>, u32> { b: 0xDEAD_DEAD };
+ w.a.1 = Triple(0, 0, 0);
+ assert_eq!(w.b, 0x0000_00AD);
+}
+
+#[cfg(target_endian = "big")]
+unsafe fn check() {
+ let mut u = U::<u8, u16> { b: 0xDE_DE };
+ u.a.0 = 0xBE;
+ assert_eq!(u.b, 0xBE_DE);
+
+ let mut u = U::<u16, u32> { b: 0xDEAD_DEAD };
+ u.a.0 = 0xBEEF;
+ assert_eq!(u.b, 0xBEEF_DEAD);
+
+ let mut u = U::<u32, u64> { b: 0xDEADBEEF_DEADBEEF };
+ u.a.0 = 0xBAADF00D;
+ assert_eq!(u.b, 0xBAADF00D_DEADBEEF);
+
+ let mut w = W::<Pair<Triple<u8>, u8>, u32> { b: 0xDEAD_DEAD };
+ w.a.0 = Triple(0, 0, 0);
+ assert_eq!(w.b, 0x0000_00AD);
+
+ let mut w = W::<Pair<u8, Triple<u8>>, u32> { b: 0xDEAD_DEAD };
+ w.a.1 = Triple(0, 0, 0);
+ assert_eq!(w.b, 0xDE00_0000);
+}
+
+fn main() {
+ unsafe {
+ check();
+ }
+}