From b59ec166adec6b1348421d7b558ad434351839be Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Fri, 14 Apr 2023 06:39:16 +0000 Subject: [PATCH 1/3] allow `repr(align = x)` on inherent methods --- compiler/rustc_passes/messages.ftl | 4 ++++ compiler/rustc_passes/src/check_attr.rs | 12 +++++++++--- compiler/rustc_passes/src/errors.rs | 4 ++-- tests/codegen/align-fn.rs | 9 +++++++++ 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_passes/messages.ftl b/compiler/rustc_passes/messages.ftl index b354dca7cc44c..0d706996810f3 100644 --- a/compiler/rustc_passes/messages.ftl +++ b/compiler/rustc_passes/messages.ftl @@ -631,6 +631,10 @@ 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_inherent_method_union = + attribute should be applied to a struct, enum, function, inherent method, or union + .label = not a struct, enum, function, inherent method, or union + passes_transparent_incompatible = transparent {$target} cannot have other repr hints diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index 80a93da2b45c4..a03c991d3bee0 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -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, @@ -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(MethodKind::Inherent) => continue, _ => { self.tcx.sess.emit_err( - errors::AttrApplication::StructEnumFunctionUnion { + errors::AttrApplication::StructEnumFunctionInherentMethodUnion { hint_span: hint.span(), span, }, diff --git a/compiler/rustc_passes/src/errors.rs b/compiler/rustc_passes/src/errors.rs index 139ba8c967756..27039a2a5a21f 100644 --- a/compiler/rustc_passes/src/errors.rs +++ b/compiler/rustc_passes/src/errors.rs @@ -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_inherent_method_union, code = "E0517")] + StructEnumFunctionInherentMethodUnion { #[primary_span] hint_span: Span, #[label] diff --git a/tests/codegen/align-fn.rs b/tests/codegen/align-fn.rs index c5886cf28081a..7238e7f53c368 100644 --- a/tests/codegen/align-fn.rs +++ b/tests/codegen/align-fn.rs @@ -7,3 +7,12 @@ #[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) {} +} From dda89945b733897796250c46ca3cca8dcc6abb8a Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Sun, 16 Apr 2023 06:30:45 +0000 Subject: [PATCH 2/3] Allow all associated functions and add test --- compiler/rustc_passes/messages.ftl | 10 +++----- compiler/rustc_passes/src/check_attr.rs | 4 ++-- compiler/rustc_passes/src/errors.rs | 4 ++-- tests/codegen/align-fn.rs | 31 +++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_passes/messages.ftl b/compiler/rustc_passes/messages.ftl index 0d706996810f3..055682a1509ef 100644 --- a/compiler/rustc_passes/messages.ftl +++ b/compiler/rustc_passes/messages.ftl @@ -627,13 +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_inherent_method_union = - attribute should be applied to a struct, enum, function, inherent method, or union - .label = not a struct, enum, function, inherent method, 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 diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index a03c991d3bee0..085a28626ea00 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -1745,10 +1745,10 @@ impl CheckAttrVisitor<'_> { | Target::Union | Target::Enum | Target::Fn - | Target::Method(MethodKind::Inherent) => continue, + | Target::Method(_) => continue, _ => { self.tcx.sess.emit_err( - errors::AttrApplication::StructEnumFunctionInherentMethodUnion { + errors::AttrApplication::StructEnumFunctionMethodUnion { hint_span: hint.span(), span, }, diff --git a/compiler/rustc_passes/src/errors.rs b/compiler/rustc_passes/src/errors.rs index 27039a2a5a21f..e8603b3a2f173 100644 --- a/compiler/rustc_passes/src/errors.rs +++ b/compiler/rustc_passes/src/errors.rs @@ -1355,8 +1355,8 @@ pub enum AttrApplication { #[label] span: Span, }, - #[diag(passes_attr_application_struct_enum_function_inherent_method_union, code = "E0517")] - StructEnumFunctionInherentMethodUnion { + #[diag(passes_attr_application_struct_enum_function_method_union, code = "E0517")] + StructEnumFunctionMethodUnion { #[primary_span] hint_span: Span, #[label] diff --git a/tests/codegen/align-fn.rs b/tests/codegen/align-fn.rs index 7238e7f53c368..f3cf614e185c9 100644 --- a/tests/codegen/align-fn.rs +++ b/tests/codegen/align-fn.rs @@ -15,4 +15,35 @@ impl A { #[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(); } From 84de04155c3a1d5e9f71c53bc0d93b46e0417b25 Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Mon, 17 Apr 2023 12:42:02 +0000 Subject: [PATCH 3/3] add test for invalid places of repr align --- tests/ui/attributes/invalid-repr.rs | 5 +++++ tests/ui/attributes/invalid-repr.stderr | 12 ++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 tests/ui/attributes/invalid-repr.rs create mode 100644 tests/ui/attributes/invalid-repr.stderr diff --git a/tests/ui/attributes/invalid-repr.rs b/tests/ui/attributes/invalid-repr.rs new file mode 100644 index 0000000000000..10a487c127ec8 --- /dev/null +++ b/tests/ui/attributes/invalid-repr.rs @@ -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() {} diff --git a/tests/ui/attributes/invalid-repr.stderr b/tests/ui/attributes/invalid-repr.stderr new file mode 100644 index 0000000000000..98a6a24b3c42c --- /dev/null +++ b/tests/ui/attributes/invalid-repr.stderr @@ -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`.