Skip to content

Commit d748e70

Browse files
authored
Rollup merge of #143206 - Jules-Bertholet:align-attr-fixes, r=workingjubilee
Align attr fixes - Remove references to the superseded `repr(align)` syntax - Allow the attribute on fn items in `extern` blocks - Test attribute in combination with `async fn` and `dyn` r? workingjubilee Tracking issue: #82232 `@rustbot` label A-attributes F-fn_align T-compiler
2 parents a84ab0c + 8f86c4a commit d748e70

File tree

12 files changed

+144
-22
lines changed

12 files changed

+144
-22
lines changed

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_abi::{Align, ExternAbi};
44
use rustc_ast::expand::autodiff_attrs::{AutoDiffAttrs, DiffActivity, DiffMode};
55
use rustc_ast::{LitKind, MetaItem, MetaItemInner, attr};
66
use rustc_attr_data_structures::{
7-
AttributeKind, InlineAttr, InstructionSetAttr, OptimizeAttr, ReprAttr, UsedBy, find_attr,
7+
AttributeKind, InlineAttr, InstructionSetAttr, OptimizeAttr, UsedBy, find_attr,
88
};
99
use rustc_hir::def::DefKind;
1010
use rustc_hir::def_id::{DefId, LOCAL_CRATE, LocalDefId};
@@ -109,14 +109,6 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
109109

110110
if let hir::Attribute::Parsed(p) = attr {
111111
match p {
112-
AttributeKind::Repr { reprs, first_span: _ } => {
113-
codegen_fn_attrs.alignment = reprs
114-
.iter()
115-
.filter_map(
116-
|(r, _)| if let ReprAttr::ReprAlign(x) = r { Some(*x) } else { None },
117-
)
118-
.max();
119-
}
120112
AttributeKind::Cold(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::COLD,
121113
AttributeKind::ExportName { name, .. } => {
122114
codegen_fn_attrs.export_name = Some(*name);

compiler/rustc_passes/messages.ftl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ passes_abi_ne =
1313
passes_abi_of =
1414
fn_abi_of({$fn_name}) = {$fn_abi}
1515
16+
passes_align_attr_application =
17+
`#[align(...)]` should be applied to a function item
18+
.label = not a function item
19+
1620
passes_align_should_be_repr_align =
1721
`#[align(...)]` is not supported on {$item} items
1822
.suggestion = use `#[repr(align(...))]` instead

compiler/rustc_passes/src/check_attr.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1915,7 +1915,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
19151915
/// Checks if the `#[align]` attributes on `item` are valid.
19161916
fn check_align(&self, span: Span, target: Target, align: Align, repr_span: Span) {
19171917
match target {
1918-
Target::Fn | Target::Method(_) => {}
1918+
Target::Fn | Target::Method(_) | Target::ForeignFn => {}
19191919
Target::Struct | Target::Union | Target::Enum => {
19201920
self.dcx().emit_err(errors::AlignShouldBeReprAlign {
19211921
span: repr_span,
@@ -1924,10 +1924,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
19241924
});
19251925
}
19261926
_ => {
1927-
self.dcx().emit_err(errors::AttrApplication::StructEnumUnion {
1928-
hint_span: repr_span,
1929-
span,
1930-
});
1927+
self.dcx().emit_err(errors::AlignAttrApplication { hint_span: repr_span, span });
19311928
}
19321929
}
19331930

compiler/rustc_passes/src/errors.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1838,3 +1838,12 @@ pub(crate) struct AlignShouldBeReprAlign {
18381838
pub item: &'static str,
18391839
pub align_bytes: u64,
18401840
}
1841+
1842+
#[derive(Diagnostic)]
1843+
#[diag(passes_align_attr_application)]
1844+
pub(crate) struct AlignAttrApplication {
1845+
#[primary_span]
1846+
pub hint_span: Span,
1847+
#[label]
1848+
pub span: Span,
1849+
}

tests/assembly/naked-functions/wasm32.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,16 @@ extern "C" fn nop() {
2727
naked_asm!("nop")
2828
}
2929

30-
// CHECK: .section .text.weak_aligned_nop,"",@
31-
// CHECK: .weak weak_aligned_nop
30+
// CHECK: .section .text.weak_nop,"",@
31+
// CHECK: .weak weak_nop
3232
// CHECK-LABEL: nop:
33-
// CHECK: .functype weak_aligned_nop () -> ()
33+
// CHECK: .functype weak_nop () -> ()
3434
// CHECK-NOT: .size
3535
// CHECK: end_function
3636
#[no_mangle]
3737
#[unsafe(naked)]
3838
#[linkage = "weak"]
39-
// wasm functions cannot be aligned, so this has no effect
40-
#[align(32)]
41-
extern "C" fn weak_aligned_nop() {
39+
extern "C" fn weak_nop() {
4240
naked_asm!("nop")
4341
}
4442

tests/codegen/align-fn.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
//@ compile-flags: -C no-prepopulate-passes -Z mir-opt-level=0 -Clink-dead-code
2+
//@ edition: 2024
3+
//@ ignore-wasm32 aligning functions is not currently supported on wasm (#143368)
24

35
#![crate_type = "lib"]
46
#![feature(fn_align)]
@@ -116,3 +118,24 @@ pub fn align_specified_twice_2() {}
116118
#[align(32)]
117119
#[align(256)]
118120
pub fn align_specified_twice_3() {}
121+
122+
const _: () = {
123+
// CHECK-LABEL: align_unmangled
124+
// CHECK-SAME: align 256
125+
#[unsafe(no_mangle)]
126+
#[align(32)]
127+
#[align(256)]
128+
extern "C" fn align_unmangled() {}
129+
};
130+
131+
unsafe extern "C" {
132+
#[align(256)]
133+
fn align_unmangled();
134+
}
135+
136+
// FIXME also check `gen` et al
137+
// CHECK-LABEL: async_align
138+
// CHECK-SAME: align 64
139+
#[unsafe(no_mangle)]
140+
#[align(64)]
141+
pub async fn async_align() {}

tests/codegen/min-function-alignment.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//@ compile-flags: -C no-prepopulate-passes -Z mir-opt-level=0 -Clink-dead-code
33
//@ [align16] compile-flags: -Zmin-function-alignment=16
44
//@ [align1024] compile-flags: -Zmin-function-alignment=1024
5+
//@ ignore-wasm32 aligning functions is not currently supported on wasm (#143368)
56

67
#![crate_type = "lib"]
78
#![feature(fn_align)]

tests/codegen/naked-fn/aligned.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//@ compile-flags: -C no-prepopulate-passes -Copt-level=0
22
//@ needs-asm-support
33
//@ ignore-arm no "ret" mnemonic
4+
//@ ignore-wasm32 aligning functions is not currently supported on wasm (#143368)
45

56
#![crate_type = "lib"]
67
#![feature(fn_align)]

tests/codegen/naked-fn/min-function-alignment.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//@ compile-flags: -C no-prepopulate-passes -Copt-level=0 -Zmin-function-alignment=16
22
//@ needs-asm-support
33
//@ ignore-arm no "ret" mnemonic
4+
//@ ignore-wasm32 aligning functions is not currently supported on wasm (#143368)
45

56
#![feature(fn_align)]
67
#![crate_type = "lib"]

tests/ui/attributes/fn-align-dyn.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//@ run-pass
2+
//@ ignore-wasm32 aligning functions is not currently supported on wasm (#143368)
3+
#![feature(fn_align)]
4+
5+
trait Test {
6+
#[align(4096)]
7+
fn foo(&self);
8+
9+
#[align(4096)]
10+
fn foo1(&self);
11+
}
12+
13+
fn main() {
14+
assert_eq!((<dyn Test>::foo as fn(_) as usize & !1) % 4096, 0);
15+
assert_eq!((<dyn Test>::foo1 as fn(_) as usize & !1) % 4096, 0);
16+
}

tests/ui/attributes/malformed-fn-align.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,29 @@ fn f3() {}
2121
#[repr(align(16))] //~ ERROR `#[repr(align(...))]` is not supported on function items
2222
fn f4() {}
2323

24+
#[align(-1)] //~ ERROR expected unsuffixed literal, found `-`
25+
fn f5() {}
26+
27+
#[align(3)] //~ ERROR invalid alignment value: not a power of two
28+
fn f6() {}
29+
30+
#[align(4usize)] //~ ERROR invalid alignment value: not an unsuffixed integer [E0589]
31+
//~^ ERROR suffixed literals are not allowed in attributes
32+
fn f7() {}
33+
34+
#[align(16)]
35+
#[align(3)] //~ ERROR invalid alignment value: not a power of two
36+
#[align(16)]
37+
fn f8() {}
38+
2439
#[align(16)] //~ ERROR `#[align(...)]` is not supported on struct items
2540
struct S1;
41+
42+
#[align(32)] //~ ERROR `#[align(...)]` should be applied to a function item
43+
const FOO: i32 = 42;
44+
45+
#[align(32)] //~ ERROR `#[align(...)]` should be applied to a function item
46+
mod test {}
47+
48+
#[align(32)] //~ ERROR `#[align(...)]` should be applied to a function item
49+
use ::std::iter;

tests/ui/attributes/malformed-fn-align.stderr

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
error: expected unsuffixed literal, found `-`
2+
--> $DIR/malformed-fn-align.rs:24:9
3+
|
4+
LL | #[align(-1)]
5+
| ^
6+
7+
error: suffixed literals are not allowed in attributes
8+
--> $DIR/malformed-fn-align.rs:30:9
9+
|
10+
LL | #[align(4usize)]
11+
| ^^^^^^
12+
|
13+
= help: instead of using a suffixed literal (`1u8`, `1.0f32`, etc.), use an unsuffixed version (`1`, `1.0`, etc.)
14+
115
error[E0539]: malformed `align` attribute input
216
--> $DIR/malformed-fn-align.rs:5:5
317
|
@@ -37,6 +51,24 @@ error[E0589]: invalid alignment value: not a power of two
3751
LL | #[align(0)]
3852
| ^
3953

54+
error[E0589]: invalid alignment value: not a power of two
55+
--> $DIR/malformed-fn-align.rs:27:9
56+
|
57+
LL | #[align(3)]
58+
| ^
59+
60+
error[E0589]: invalid alignment value: not an unsuffixed integer
61+
--> $DIR/malformed-fn-align.rs:30:9
62+
|
63+
LL | #[align(4usize)]
64+
| ^^^^^^
65+
66+
error[E0589]: invalid alignment value: not a power of two
67+
--> $DIR/malformed-fn-align.rs:35:9
68+
|
69+
LL | #[align(3)]
70+
| ^
71+
4072
error: `#[repr(align(...))]` is not supported on function items
4173
--> $DIR/malformed-fn-align.rs:21:8
4274
|
@@ -50,7 +82,7 @@ LL | #[repr(align(16))]
5082
| ^^^^^^^^^
5183

5284
error: `#[align(...)]` is not supported on struct items
53-
--> $DIR/malformed-fn-align.rs:24:1
85+
--> $DIR/malformed-fn-align.rs:39:1
5486
|
5587
LL | #[align(16)]
5688
| ^^^^^^^^^^^^
@@ -61,7 +93,31 @@ LL - #[align(16)]
6193
LL + #[repr(align(16))]
6294
|
6395

64-
error: aborting due to 7 previous errors
96+
error: `#[align(...)]` should be applied to a function item
97+
--> $DIR/malformed-fn-align.rs:42:1
98+
|
99+
LL | #[align(32)]
100+
| ^^^^^^^^^^^^
101+
LL | const FOO: i32 = 42;
102+
| -------------------- not a function item
103+
104+
error: `#[align(...)]` should be applied to a function item
105+
--> $DIR/malformed-fn-align.rs:45:1
106+
|
107+
LL | #[align(32)]
108+
| ^^^^^^^^^^^^
109+
LL | mod test {}
110+
| ----------- not a function item
111+
112+
error: `#[align(...)]` should be applied to a function item
113+
--> $DIR/malformed-fn-align.rs:48:1
114+
|
115+
LL | #[align(32)]
116+
| ^^^^^^^^^^^^
117+
LL | use ::std::iter;
118+
| ---------------- not a function item
119+
120+
error: aborting due to 15 previous errors
65121

66122
Some errors have detailed explanations: E0539, E0589, E0805.
67123
For more information about an error, try `rustc --explain E0539`.

0 commit comments

Comments
 (0)