Skip to content

Commit 39b54d6

Browse files
authored
Rollup merge of rust-lang#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 ``@rustbot`` label A-attributes F-fn_align T-compiler
2 parents 205e6c2 + 440bf29 commit 39b54d6

File tree

8 files changed

+135
-16
lines changed

8 files changed

+135
-16
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) => {
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
@@ -1888,7 +1888,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
18881888
/// Checks if the `#[align]` attributes on `item` are valid.
18891889
fn check_align(&self, span: Span, target: Target, align: Align, repr_span: Span) {
18901890
match target {
1891-
Target::Fn | Target::Method(_) => {}
1891+
Target::Fn | Target::Method(_) | Target::ForeignFn => {}
18921892
Target::Struct | Target::Union | Target::Enum => {
18931893
self.dcx().emit_err(errors::AlignShouldBeReprAlign {
18941894
span: repr_span,
@@ -1897,10 +1897,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
18971897
});
18981898
}
18991899
_ => {
1900-
self.dcx().emit_err(errors::AttrApplication::StructEnumUnion {
1901-
hint_span: repr_span,
1902-
span,
1903-
});
1900+
self.dcx().emit_err(errors::AlignAttrApplication { hint_span: repr_span, span });
19041901
}
19051902
}
19061903

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/codegen/align-fn.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//@ compile-flags: -C no-prepopulate-passes -Z mir-opt-level=0 -Clink-dead-code
2+
//@ edition: 2024
23

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

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

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

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)