]> git.lizzy.rs Git - rust.git/commitdiff
Add more tests for #[repr(align(x))] on enums
authorNiklas Fiekas <niklas.fiekas@backscattering.de>
Thu, 31 Jan 2019 13:18:31 +0000 (14:18 +0100)
committerNiklas Fiekas <niklas.fiekas@backscattering.de>
Thu, 31 Jan 2019 13:18:51 +0000 (14:18 +0100)
src/test/run-pass/structs-enums/align-enum.rs
src/test/ui/repr/repr-align.rs
src/test/ui/repr/repr-align.stderr

index 265bae89b80c95136538945cb530006089d4c158..8d72b1f6f0d24b99b1307706e6307a623b6e7499 100644 (file)
@@ -5,13 +5,51 @@
 use std::mem;
 
 // Raising alignment
-#[repr(align(8))]
-enum Align8 {
+#[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::<Align8>(), 8);
-    assert_eq!(mem::size_of::<Align8>(), 8);
+    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);
 }
index 78ed6c7e1c4177c68ce8cdc7640a9846275e0753..9ce89e82ca2256c4ac5fc54a92551f05ce57d2aa 100644 (file)
@@ -1,3 +1,4 @@
+#![feature(repr_align_enum)]
 #![allow(dead_code)]
 
 #[repr(align(16.0))] //~ ERROR: invalid `repr(align)` attribute: not an unsuffixed integer
@@ -12,4 +13,7 @@
 #[repr(align(536870912))] // ok: this is the largest accepted alignment
 struct D(i32);
 
+#[repr(align(15))] //~ ERROR: invalid `repr(align)` attribute: not a power of two
+enum E { Left, Right }
+
 fn main() {}
index e8dbf74232bfafb18927a3a4e6344948cf05c303..f1a5d88ace1fd15a55dac7bdbdf01a7d67dd9c83 100644 (file)
@@ -1,21 +1,27 @@
 error[E0589]: invalid `repr(align)` attribute: not an unsuffixed integer
-  --> $DIR/repr-align.rs:3:8
+  --> $DIR/repr-align.rs:4:8
    |
 LL | #[repr(align(16.0))] //~ ERROR: invalid `repr(align)` attribute: not an unsuffixed integer
    |        ^^^^^^^^^^^
 
 error[E0589]: invalid `repr(align)` attribute: not a power of two
-  --> $DIR/repr-align.rs:6:8
+  --> $DIR/repr-align.rs:7:8
    |
 LL | #[repr(align(15))] //~ ERROR: invalid `repr(align)` attribute: not a power of two
    |        ^^^^^^^^^
 
 error[E0589]: invalid `repr(align)` attribute: larger than 2^29
-  --> $DIR/repr-align.rs:9:8
+  --> $DIR/repr-align.rs:10:8
    |
 LL | #[repr(align(4294967296))] //~ ERROR: invalid `repr(align)` attribute: larger than 2^29
    |        ^^^^^^^^^^^^^^^^^
 
-error: aborting due to 3 previous errors
+error[E0589]: invalid `repr(align)` attribute: not a power of two
+  --> $DIR/repr-align.rs:16:8
+   |
+LL | #[repr(align(15))] //~ ERROR: invalid `repr(align)` attribute: not a power of two
+   |        ^^^^^^^^^
+
+error: aborting due to 4 previous errors
 
 For more information about this error, try `rustc --explain E0589`.