about summary refs log tree commit diff
path: root/tests/ui/structs-enums/align-enum.rs
diff options
context:
space:
mode:
authorAlbert Larsan <74931857+albertlarsan68@users.noreply.github.com>2023-01-05 09:13:28 +0100
committerAlbert Larsan <74931857+albertlarsan68@users.noreply.github.com>2023-01-11 09:32:08 +0000
commitcf2dff2b1e3fa55fa5415d524200070d0d7aacfe (patch)
tree40a88d9a46aaf3e8870676eb2538378b75a263eb /tests/ui/structs-enums/align-enum.rs
parentca855e6e42787ecd062d81d53336fe6788ef51a9 (diff)
downloadrust-cf2dff2b1e3fa55fa5415d524200070d0d7aacfe.tar.gz
rust-cf2dff2b1e3fa55fa5415d524200070d0d7aacfe.zip
Move /src/test to /tests
Diffstat (limited to 'tests/ui/structs-enums/align-enum.rs')
-rw-r--r--tests/ui/structs-enums/align-enum.rs54
1 files changed, 54 insertions, 0 deletions
diff --git a/tests/ui/structs-enums/align-enum.rs b/tests/ui/structs-enums/align-enum.rs
new file mode 100644
index 00000000000..fa872caa3b4
--- /dev/null
+++ b/tests/ui/structs-enums/align-enum.rs
@@ -0,0 +1,54 @@
+// run-pass
+#![allow(dead_code)]
+
+use std::mem;
+
+// Raising alignment
+#[repr(align(16))]
+enum Align16 {
+    Foo { foo: u32 },
+    Bar { bar: u32 },
+}
+
+// Raise alignment by maximum
+#[repr(align(1), align(16))]
+#[repr(align(32))]
+#[repr(align(4))]
+enum Align32 {
+    Foo,
+    Bar,
+}
+
+// Not reducing alignment
+#[repr(align(4))]
+enum AlsoAlign16 {
+    Foo { limb_with_align16: Align16 },
+    Bar,
+}
+
+// No niche for discriminant when used as limb
+#[repr(align(16))]
+struct NoNiche16(u64, u64);
+
+// Discriminant will require extra space, but enum needs to stay compatible
+// with alignment 16
+#[repr(align(1))]
+enum AnotherAlign16 {
+    Foo { limb_with_noniche16: NoNiche16 },
+    Bar,
+    Baz,
+}
+
+fn main() {
+    assert_eq!(mem::align_of::<Align16>(), 16);
+    assert_eq!(mem::size_of::<Align16>(), 16);
+
+    assert_eq!(mem::align_of::<Align32>(), 32);
+    assert_eq!(mem::size_of::<Align32>(), 32);
+
+    assert_eq!(mem::align_of::<AlsoAlign16>(), 16);
+    assert_eq!(mem::size_of::<AlsoAlign16>(), 16);
+
+    assert_eq!(mem::align_of::<AnotherAlign16>(), 16);
+    assert_eq!(mem::size_of::<AnotherAlign16>(), 32);
+}