summaryrefslogtreecommitdiffstats
path: root/tests/ui/union/union-align.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:18:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:18:58 +0000
commita4b7ed7a42c716ab9f05e351f003d589124fd55d (patch)
treeb620cd3f223850b28716e474e80c58059dca5dd4 /tests/ui/union/union-align.rs
parentAdding upstream version 1.67.1+dfsg1. (diff)
downloadrustc-a4b7ed7a42c716ab9f05e351f003d589124fd55d.tar.xz
rustc-a4b7ed7a42c716ab9f05e351f003d589124fd55d.zip
Adding upstream version 1.68.2+dfsg1.upstream/1.68.2+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/ui/union/union-align.rs')
-rw-r--r--tests/ui/union/union-align.rs65
1 files changed, 65 insertions, 0 deletions
diff --git a/tests/ui/union/union-align.rs b/tests/ui/union/union-align.rs
new file mode 100644
index 000000000..6a44f27db
--- /dev/null
+++ b/tests/ui/union/union-align.rs
@@ -0,0 +1,65 @@
+// run-pass
+// revisions: mirunsafeck thirunsafeck
+// [thirunsafeck]compile-flags: -Z thir-unsafeck
+
+#![allow(dead_code)]
+
+use std::mem::{size_of, size_of_val, align_of, align_of_val};
+
+#[repr(align(16))]
+pub union U16 {
+ a: u8,
+ b: u32
+}
+
+fn main() {
+ assert_eq!(align_of::<U16>(), 16);
+ assert_eq!(size_of::<U16>(), 16);
+ let u = U16 { a: 10 };
+ unsafe {
+ assert_eq!(align_of_val(&u.a), 1);
+ assert_eq!(size_of_val(&u.a), 1);
+ assert_eq!(u.a, 10);
+ }
+
+ let u = U16 { b: 11 };
+ unsafe {
+ assert_eq!(align_of_val(&u.b), 4);
+ assert_eq!(size_of_val(&u.b), 4);
+ assert_eq!(u.b, 11);
+ }
+
+ hybrid::check_hybrid();
+}
+
+mod hybrid {
+ use std::mem::{size_of, align_of};
+
+ #[repr(align(16))]
+ #[derive(Copy, Clone)]
+ struct S1 {
+ a: u16,
+ b: u8,
+ }
+
+ #[repr(align(32))]
+ union U {
+ s: S1,
+ c: u16,
+ }
+
+ #[repr(align(64))]
+ struct S2 {
+ d: u8,
+ u: U,
+ }
+
+ pub fn check_hybrid() {
+ assert_eq!(align_of::<S1>(), 16);
+ assert_eq!(size_of::<S1>(), 16);
+ assert_eq!(align_of::<U>(), 32);
+ assert_eq!(size_of::<U>(), 32);
+ assert_eq!(align_of::<S2>(), 64);
+ assert_eq!(size_of::<S2>(), 64);
+ }
+}