Skip to content

Allow #[repr(align(x))] on enums (#57996) #57998

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 7, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/doc/unstable-book/src/language-features/repr-align-enum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# `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);
}
```
12 changes: 2 additions & 10 deletions src/librustc/hir/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ impl<'a, 'tcx> CheckAttrVisitor<'a, 'tcx> {
};

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 {
Expand All @@ -213,14 +213,6 @@ impl<'a, 'tcx> CheckAttrVisitor<'a, 'tcx> {
continue
}
}
"align" => {
if target != Target::Struct &&
target != Target::Union {
("a", "struct or union")
} else {
continue
}
}
"transparent" => {
is_transparent = true;
if target != Target::Struct {
Expand Down
14 changes: 14 additions & 0 deletions src/libsyntax/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,9 @@ declare_features! (

// #[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! (
Expand Down Expand Up @@ -1697,6 +1700,17 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
}
}

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,
Expand Down
17 changes: 17 additions & 0 deletions src/test/run-pass/structs-enums/align-enum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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);
}
3 changes: 2 additions & 1 deletion src/test/ui/attr-usage-repr.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![feature(repr_simd)]
#![feature(repr_align_enum)]

#[repr(C)] //~ ERROR: attribute should be applied to struct, enum or union
fn f() {}
Expand All @@ -18,7 +19,7 @@ struct SInt(f64, f64);
#[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
Expand Down
18 changes: 5 additions & 13 deletions src/test/ui/attr-usage-repr.stderr
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`.
10 changes: 10 additions & 0 deletions src/test/ui/feature-gates/feature-gate-repr_align_enum.rs
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 src/test/ui/feature-gates/feature-gate-repr_align_enum.stderr
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`.