--- /dev/null
+# `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);
+}
+```
};
let (article, allowed_targets) = match &*name.as_str() {
- "C" => {
- is_c = true;
+ "C" | "align" => {
+ is_c |= name == "C";
if target != Target::Struct &&
target != Target::Union &&
target != Target::Enum {
continue
}
}
- "align" => {
- if target != Target::Struct &&
- target != Target::Union {
- ("a", "struct or union")
- } else {
- continue
- }
- }
"transparent" => {
is_transparent = true;
if target != Target::Struct {
// #[optimize(X)]
(active, optimize_attribute, "1.34.0", Some(54882), None),
+
+ // #[repr(align(X))] on enums
+ (active, repr_align_enum, "1.34.0", Some(57996), None),
);
declare_features! (
}
}
+ ast::ItemKind::Enum(..) => {
+ for attr in attr::filter_by_name(&i.attrs[..], "repr") {
+ for item in attr.meta_item_list().unwrap_or_else(Vec::new) {
+ if item.check_name("align") {
+ gate_feature_post!(&self, repr_align_enum, attr.span,
+ "`#[repr(align(x))]` on enums is experimental");
+ }
+ }
+ }
+ }
+
ast::ItemKind::Impl(_, polarity, defaultness, _, _, _, _) => {
if polarity == ast::ImplPolarity::Negative {
gate_feature_post!(&self, optin_builtin_traits,
--- /dev/null
+// run-pass
+#![allow(dead_code)]
+#![feature(repr_align_enum)]
+
+use std::mem;
+
+// Raising alignment
+#[repr(align(8))]
+enum Align8 {
+ Foo { foo: u32 },
+ Bar { bar: u32 },
+}
+
+fn main() {
+ assert_eq!(mem::align_of::<Align8>(), 8);
+ assert_eq!(mem::size_of::<Align8>(), 8);
+}
#![feature(repr_simd)]
+#![feature(repr_align_enum)]
#[repr(C)] //~ ERROR: attribute should be applied to struct, enum or union
fn f() {}
#[repr(C)]
enum EExtern { A, B }
-#[repr(align(8))] //~ ERROR: attribute should be applied to struct
+#[repr(align(8))]
enum EAlign { A, B }
#[repr(packed)] //~ ERROR: attribute should be applied to struct
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
| ^
| --------- 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
| ^^
| ---------------------- 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
| ^^^^^^
| --------------------- 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`.
--- /dev/null
+#[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() { }
--- /dev/null
+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`.