Add more tests for #[repr(align(x))] on enums

This commit is contained in:
Niklas Fiekas 2019-01-31 14:18:31 +01:00
parent c6f6101180
commit 73bf0703a7
3 changed files with 57 additions and 9 deletions

View 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 },
}
fn main() {
assert_eq!(mem::align_of::<Align8>(), 8);
assert_eq!(mem::size_of::<Align8>(), 8);
// 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);
}

View 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 @@ struct C(i32);
#[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() {}

View 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`.