Skip to content

Commit

Permalink
Rollup merge of #110313 - fee1-dead-contrib:repr_align_method, r=Waff…
Browse files Browse the repository at this point in the history
…leLapkin

allow `repr(align = x)` on inherent methods

Discussion: #82232 (comment)
  • Loading branch information
matthiaskrgr authored Apr 17, 2023
2 parents 06d12f6 + 84de041 commit eb05246
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 8 deletions.
6 changes: 3 additions & 3 deletions compiler/rustc_passes/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -627,9 +627,9 @@ passes_attr_application_struct_enum_union =
attribute should be applied to a struct, enum, or union
.label = not a struct, enum, or union
passes_attr_application_struct_enum_function_union =
attribute should be applied to a struct, enum, function, or union
.label = not a struct, enum, function, or union
passes_attr_application_struct_enum_function_method_union =
attribute should be applied to a struct, enum, function, associated function, or union
.label = not a struct, enum, function, associated function, or union
passes_transparent_incompatible =
transparent {$target} cannot have other repr hints
Expand Down
12 changes: 9 additions & 3 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1728,7 +1728,9 @@ impl CheckAttrVisitor<'_> {
}
}
sym::align => {
if let (Target::Fn, false) = (target, self.tcx.features().fn_align) {
if let (Target::Fn | Target::Method(MethodKind::Inherent), false) =
(target, self.tcx.features().fn_align)
{
feature_err(
&self.tcx.sess.parse_sess,
sym::fn_align,
Expand All @@ -1739,10 +1741,14 @@ impl CheckAttrVisitor<'_> {
}

match target {
Target::Struct | Target::Union | Target::Enum | Target::Fn => continue,
Target::Struct
| Target::Union
| Target::Enum
| Target::Fn
| Target::Method(_) => continue,
_ => {
self.tcx.sess.emit_err(
errors::AttrApplication::StructEnumFunctionUnion {
errors::AttrApplication::StructEnumFunctionMethodUnion {
hint_span: hint.span(),
span,
},
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_passes/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1355,8 +1355,8 @@ pub enum AttrApplication {
#[label]
span: Span,
},
#[diag(passes_attr_application_struct_enum_function_union, code = "E0517")]
StructEnumFunctionUnion {
#[diag(passes_attr_application_struct_enum_function_method_union, code = "E0517")]
StructEnumFunctionMethodUnion {
#[primary_span]
hint_span: Span,
#[label]
Expand Down
40 changes: 40 additions & 0 deletions tests/codegen/align-fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,43 @@
#[no_mangle]
#[repr(align(16))]
pub fn fn_align() {}

pub struct A;

impl A {
// CHECK: align 16
#[no_mangle]
#[repr(align(16))]
pub fn method_align(self) {}

// CHECK: align 16
#[no_mangle]
#[repr(align(16))]
pub fn associated_fn() {}
}

trait T: Sized {
fn trait_fn() {}

// CHECK: align 32
#[repr(align(32))]
fn trait_method(self) {}
}

impl T for A {
// CHECK: align 16
#[no_mangle]
#[repr(align(16))]
fn trait_fn() {}

// CHECK: align 16
#[no_mangle]
#[repr(align(16))]
fn trait_method(self) {}
}

impl T for () {}

pub fn foo() {
().trait_method();
}
5 changes: 5 additions & 0 deletions tests/ui/attributes/invalid-repr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[repr(align(16))]
//~^ ERROR attribute should be applied to a struct, enum, function, associated function, or union
pub type Foo = i32;

fn main() {}
12 changes: 12 additions & 0 deletions tests/ui/attributes/invalid-repr.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error[E0517]: attribute should be applied to a struct, enum, function, associated function, or union
--> $DIR/invalid-repr.rs:1:8
|
LL | #[repr(align(16))]
| ^^^^^^^^^
LL |
LL | pub type Foo = i32;
| ------------------- not a struct, enum, function, associated function, or union

error: aborting due to previous error

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

0 comments on commit eb05246

Please sign in to comment.