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+
}

0 commit comments

Comments
 (0)