Skip to content

Commit 73bf070

Browse files
committed
Add more tests for #[repr(align(x))] on enums
1 parent c6f6101 commit 73bf070

File tree

3 files changed

+56
-8
lines changed

3 files changed

+56
-8
lines changed

src/test/run-pass/structs-enums/align-enum.rs

+42-4
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,51 @@
55
use std::mem;
66

77
// Raising alignment
8-
#[repr(align(8))]
9-
enum Align8 {
8+
#[repr(align(16))]
9+
enum Align16 {
1010
Foo { foo: u32 },
1111
Bar { bar: u32 },
1212
}
1313

14+
// Raise alignment by maximum
15+
#[repr(align(1), align(16))]
16+
#[repr(align(32))]
17+
#[repr(align(4))]
18+
enum Align32 {
19+
Foo,
20+
Bar,
21+
}
22+
23+
// Not reducing alignment
24+
#[repr(align(4))]
25+
enum AlsoAlign16 {
26+
Foo { limb_with_align16: Align16 },
27+
Bar,
28+
}
29+
30+
// No niche for discriminant when used as limb
31+
#[repr(align(16))]
32+
struct NoNiche16(u64, u64);
33+
34+
// Discriminant will require extra space, but enum needs to stay compatible
35+
// with alignment 16
36+
#[repr(align(1))]
37+
enum AnotherAlign16 {
38+
Foo { limb_with_noniche16: NoNiche16 },
39+
Bar,
40+
Baz,
41+
}
42+
1443
fn main() {
15-
assert_eq!(mem::align_of::<Align8>(), 8);
16-
assert_eq!(mem::size_of::<Align8>(), 8);
44+
assert_eq!(mem::align_of::<Align16>(), 16);
45+
assert_eq!(mem::size_of::<Align16>(), 16);
46+
47+
assert_eq!(mem::align_of::<Align32>(), 32);
48+
assert_eq!(mem::size_of::<Align32>(), 32);
49+
50+
assert_eq!(mem::align_of::<AlsoAlign16>(), 16);
51+
assert_eq!(mem::size_of::<AlsoAlign16>(), 16);
52+
53+
assert_eq!(mem::align_of::<AnotherAlign16>(), 16);
54+
assert_eq!(mem::size_of::<AnotherAlign16>(), 32);
1755
}

src/test/ui/repr/repr-align.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![feature(repr_align_enum)]
12
#![allow(dead_code)]
23

34
#[repr(align(16.0))] //~ ERROR: invalid `repr(align)` attribute: not an unsuffixed integer
@@ -12,4 +13,7 @@ struct C(i32);
1213
#[repr(align(536870912))] // ok: this is the largest accepted alignment
1314
struct D(i32);
1415

16+
#[repr(align(15))] //~ ERROR: invalid `repr(align)` attribute: not a power of two
17+
enum E { Left, Right }
18+
1519
fn main() {}

src/test/ui/repr/repr-align.stderr

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
error[E0589]: invalid `repr(align)` attribute: not an unsuffixed integer
2-
--> $DIR/repr-align.rs:3:8
2+
--> $DIR/repr-align.rs:4:8
33
|
44
LL | #[repr(align(16.0))] //~ ERROR: invalid `repr(align)` attribute: not an unsuffixed integer
55
| ^^^^^^^^^^^
66

77
error[E0589]: invalid `repr(align)` attribute: not a power of two
8-
--> $DIR/repr-align.rs:6:8
8+
--> $DIR/repr-align.rs:7:8
99
|
1010
LL | #[repr(align(15))] //~ ERROR: invalid `repr(align)` attribute: not a power of two
1111
| ^^^^^^^^^
1212

1313
error[E0589]: invalid `repr(align)` attribute: larger than 2^29
14-
--> $DIR/repr-align.rs:9:8
14+
--> $DIR/repr-align.rs:10:8
1515
|
1616
LL | #[repr(align(4294967296))] //~ ERROR: invalid `repr(align)` attribute: larger than 2^29
1717
| ^^^^^^^^^^^^^^^^^
1818

19-
error: aborting due to 3 previous errors
19+
error[E0589]: invalid `repr(align)` attribute: not a power of two
20+
--> $DIR/repr-align.rs:16:8
21+
|
22+
LL | #[repr(align(15))] //~ ERROR: invalid `repr(align)` attribute: not a power of two
23+
| ^^^^^^^^^
24+
25+
error: aborting due to 4 previous errors
2026

2127
For more information about this error, try `rustc --explain E0589`.

0 commit comments

Comments
 (0)