Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
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();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add a check for trait methods and associated functions, just to be sure? i.e.

Suggested change
}
// CHECK: align 16
#[no_mangle]
#[repr(align(16))]
pub fn associated_fn() {}
}
trait T {
fn trait_fn() {}
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) {}
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should those be allowed? The current change doesn't allow those, but I can change it to include trait impls as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think yes, they should? Don't really see a reason to not allow these (btw maybe a test for default bodies should be added too, although I'm not sure if it's easy to do...).