-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #57998 - niklasf:align-enum, r=nagisa
Allow #[repr(align(x))] on enums (#57996) Tracking issue: #57996 Implements an extension of [RFC 1358](https://github.com/rust-lang/rfcs/blob/master/text/1358-repr-align.md) behind a feature flag (`repr_align_enum`). Originally introduced here for structs: #39999. It seems like only HIR-level changes are required, since enums are already aware of their alignment (due to alignment of their limbs). cc @bitshifter
- Loading branch information
Showing
11 changed files
with
191 additions
and
28 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
src/doc/unstable-book/src/language-features/repr-align-enum.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# `repr_align_enum` | ||
|
||
The tracking issue for this feature is: [#57996] | ||
|
||
[#57996]: https://github.com/rust-lang/rust/issues/57996 | ||
|
||
------------------------ | ||
|
||
The `repr_align_enum` feature allows using the `#[repr(align(x))]` attribute | ||
on enums, similarly to structs. | ||
|
||
# Examples | ||
|
||
```rust | ||
#![feature(repr_align_enum)] | ||
|
||
#[repr(align(8))] | ||
enum Aligned { | ||
Foo, | ||
Bar { value: u32 }, | ||
} | ||
|
||
fn main() { | ||
assert_eq!(std::mem::align_of::<Aligned>(), 8); | ||
} | ||
``` | ||
|
||
This is equivalent to using an aligned wrapper struct everywhere: | ||
|
||
```rust | ||
#[repr(align(8))] | ||
struct Aligned(Unaligned); | ||
|
||
enum Unaligned { | ||
Foo, | ||
Bar { value: u32 }, | ||
} | ||
|
||
fn main() { | ||
assert_eq!(std::mem::align_of::<Aligned>(), 8); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// compile-flags: -C no-prepopulate-passes | ||
// ignore-tidy-linelength | ||
// min-llvm-version 7.0 | ||
|
||
#![crate_type = "lib"] | ||
#![feature(repr_align_enum)] | ||
|
||
#[repr(align(64))] | ||
pub enum Align64 { | ||
A(u32), | ||
B(u32), | ||
} | ||
// CHECK: %Align64 = type { [0 x i32], i32, [15 x i32] } | ||
|
||
pub struct Nested64 { | ||
a: u8, | ||
b: Align64, | ||
c: u16, | ||
} | ||
|
||
// CHECK-LABEL: @align64 | ||
#[no_mangle] | ||
pub fn align64(a: u32) -> Align64 { | ||
// CHECK: %a64 = alloca %Align64, align 64 | ||
// CHECK: call void @llvm.memcpy.{{.*}}(i8* align 64 %{{.*}}, i8* align 64 %{{.*}}, i{{[0-9]+}} 64, i1 false) | ||
let a64 = Align64::A(a); | ||
a64 | ||
} | ||
|
||
// CHECK-LABEL: @nested64 | ||
#[no_mangle] | ||
pub fn nested64(a: u8, b: u32, c: u16) -> Nested64 { | ||
// CHECK: %n64 = alloca %Nested64, align 64 | ||
let n64 = Nested64 { a, b: Align64::B(b), c }; | ||
n64 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// run-pass | ||
#![allow(dead_code)] | ||
#![feature(repr_align_enum)] | ||
|
||
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,35 @@ | ||
error[E0517]: attribute should be applied to struct, enum or union | ||
--> $DIR/attr-usage-repr.rs:3:8 | ||
--> $DIR/attr-usage-repr.rs:4:8 | ||
| | ||
LL | #[repr(C)] //~ ERROR: attribute should be applied to struct, enum or union | ||
| ^ | ||
LL | fn f() {} | ||
| --------- not a struct, enum or union | ||
|
||
error[E0517]: attribute should be applied to enum | ||
--> $DIR/attr-usage-repr.rs:15:8 | ||
--> $DIR/attr-usage-repr.rs:16:8 | ||
| | ||
LL | #[repr(i8)] //~ ERROR: attribute should be applied to enum | ||
| ^^ | ||
LL | struct SInt(f64, f64); | ||
| ---------------------- not an enum | ||
|
||
error[E0517]: attribute should be applied to struct or union | ||
--> $DIR/attr-usage-repr.rs:21:8 | ||
| | ||
LL | #[repr(align(8))] //~ ERROR: attribute should be applied to struct | ||
| ^^^^^^^^ | ||
LL | enum EAlign { A, B } | ||
| -------------------- not a struct or union | ||
|
||
error[E0517]: attribute should be applied to struct or union | ||
--> $DIR/attr-usage-repr.rs:24:8 | ||
--> $DIR/attr-usage-repr.rs:25:8 | ||
| | ||
LL | #[repr(packed)] //~ ERROR: attribute should be applied to struct | ||
| ^^^^^^ | ||
LL | enum EPacked { A, B } | ||
| --------------------- not a struct or union | ||
|
||
error[E0517]: attribute should be applied to struct | ||
--> $DIR/attr-usage-repr.rs:27:8 | ||
--> $DIR/attr-usage-repr.rs:28:8 | ||
| | ||
LL | #[repr(simd)] //~ ERROR: attribute should be applied to struct | ||
| ^^^^ | ||
LL | enum ESimd { A, B } | ||
| ------------------- not a struct | ||
|
||
error: aborting due to 5 previous errors | ||
error: aborting due to 4 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0517`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#[repr(align(16))] | ||
struct Foo(u64); | ||
|
||
#[repr(align(8))] //~ ERROR `#[repr(align(x))]` on enums is experimental (see issue #57996) | ||
enum Bar { | ||
Foo { foo: Foo }, | ||
Baz, | ||
} | ||
|
||
fn main() { } |
11 changes: 11 additions & 0 deletions
11
src/test/ui/feature-gates/feature-gate-repr_align_enum.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
error[E0658]: `#[repr(align(x))]` on enums is experimental (see issue #57996) | ||
--> $DIR/feature-gate-repr_align_enum.rs:4:1 | ||
| | ||
LL | #[repr(align(8))] //~ ERROR `#[repr(align(x))]` on enums is experimental (see issue #57996) | ||
| ^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: add #![feature(repr_align_enum)] to the crate attributes to enable | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0658`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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`. |