Skip to content

Commit eb05246

Browse files
authored
Rollup merge of #110313 - fee1-dead-contrib:repr_align_method, r=WaffleLapkin
allow `repr(align = x)` on inherent methods Discussion: #82232 (comment)
2 parents 06d12f6 + 84de041 commit eb05246

File tree

6 files changed

+71
-8
lines changed

6 files changed

+71
-8
lines changed

compiler/rustc_passes/messages.ftl

+3-3
Original file line numberDiff line numberDiff line change
@@ -627,9 +627,9 @@ passes_attr_application_struct_enum_union =
627627
attribute should be applied to a struct, enum, or union
628628
.label = not a struct, enum, or union
629629
630-
passes_attr_application_struct_enum_function_union =
631-
attribute should be applied to a struct, enum, function, or union
632-
.label = not a struct, enum, function, or union
630+
passes_attr_application_struct_enum_function_method_union =
631+
attribute should be applied to a struct, enum, function, associated function, or union
632+
.label = not a struct, enum, function, associated function, or union
633633
634634
passes_transparent_incompatible =
635635
transparent {$target} cannot have other repr hints

compiler/rustc_passes/src/check_attr.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -1728,7 +1728,9 @@ impl CheckAttrVisitor<'_> {
17281728
}
17291729
}
17301730
sym::align => {
1731-
if let (Target::Fn, false) = (target, self.tcx.features().fn_align) {
1731+
if let (Target::Fn | Target::Method(MethodKind::Inherent), false) =
1732+
(target, self.tcx.features().fn_align)
1733+
{
17321734
feature_err(
17331735
&self.tcx.sess.parse_sess,
17341736
sym::fn_align,
@@ -1739,10 +1741,14 @@ impl CheckAttrVisitor<'_> {
17391741
}
17401742

17411743
match target {
1742-
Target::Struct | Target::Union | Target::Enum | Target::Fn => continue,
1744+
Target::Struct
1745+
| Target::Union
1746+
| Target::Enum
1747+
| Target::Fn
1748+
| Target::Method(_) => continue,
17431749
_ => {
17441750
self.tcx.sess.emit_err(
1745-
errors::AttrApplication::StructEnumFunctionUnion {
1751+
errors::AttrApplication::StructEnumFunctionMethodUnion {
17461752
hint_span: hint.span(),
17471753
span,
17481754
},

compiler/rustc_passes/src/errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1355,8 +1355,8 @@ pub enum AttrApplication {
13551355
#[label]
13561356
span: Span,
13571357
},
1358-
#[diag(passes_attr_application_struct_enum_function_union, code = "E0517")]
1359-
StructEnumFunctionUnion {
1358+
#[diag(passes_attr_application_struct_enum_function_method_union, code = "E0517")]
1359+
StructEnumFunctionMethodUnion {
13601360
#[primary_span]
13611361
hint_span: Span,
13621362
#[label]

tests/codegen/align-fn.rs

+40
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,43 @@
77
#[no_mangle]
88
#[repr(align(16))]
99
pub fn fn_align() {}
10+
11+
pub struct A;
12+
13+
impl A {
14+
// CHECK: align 16
15+
#[no_mangle]
16+
#[repr(align(16))]
17+
pub fn method_align(self) {}
18+
19+
// CHECK: align 16
20+
#[no_mangle]
21+
#[repr(align(16))]
22+
pub fn associated_fn() {}
23+
}
24+
25+
trait T: Sized {
26+
fn trait_fn() {}
27+
28+
// CHECK: align 32
29+
#[repr(align(32))]
30+
fn trait_method(self) {}
31+
}
32+
33+
impl T for A {
34+
// CHECK: align 16
35+
#[no_mangle]
36+
#[repr(align(16))]
37+
fn trait_fn() {}
38+
39+
// CHECK: align 16
40+
#[no_mangle]
41+
#[repr(align(16))]
42+
fn trait_method(self) {}
43+
}
44+
45+
impl T for () {}
46+
47+
pub fn foo() {
48+
().trait_method();
49+
}

tests/ui/attributes/invalid-repr.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#[repr(align(16))]
2+
//~^ ERROR attribute should be applied to a struct, enum, function, associated function, or union
3+
pub type Foo = i32;
4+
5+
fn main() {}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0517]: attribute should be applied to a struct, enum, function, associated function, or union
2+
--> $DIR/invalid-repr.rs:1:8
3+
|
4+
LL | #[repr(align(16))]
5+
| ^^^^^^^^^
6+
LL |
7+
LL | pub type Foo = i32;
8+
| ------------------- not a struct, enum, function, associated function, or union
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0517`.

0 commit comments

Comments
 (0)