Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3d85cf8

Browse files
committedJun 14, 2025··
add #[align] attribute
Right now it's used for functions with `fn_align`, in the future it will get more uses (statics, struct fields, etc.)
1 parent 8da6239 commit 3d85cf8

File tree

26 files changed

+269
-112
lines changed

26 files changed

+269
-112
lines changed
 

‎compiler/rustc_attr_data_structures/src/attributes.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ impl Deprecation {
152152
#[derive(Clone, Debug, HashStable_Generic, Encodable, Decodable, PrintAttribute)]
153153
pub enum AttributeKind {
154154
// tidy-alphabetical-start
155+
/// Represents `#[align(N)]`.
156+
Align { align: Align, span: Span },
157+
155158
/// Represents `#[rustc_allow_const_fn_unstable]`.
156159
AllowConstFnUnstable(ThinVec<Symbol>),
157160

‎compiler/rustc_attr_parsing/messages.ftl‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ attr_parsing_incorrect_repr_format_packed_expect_integer =
4242
attr_parsing_incorrect_repr_format_packed_one_or_zero_arg =
4343
incorrect `repr(packed)` attribute format: `packed` takes exactly one parenthesized argument, or no parentheses at all
4444
45+
attr_parsing_invalid_alignment_value =
46+
invalid alignment value: {$error_part}
47+
4548
attr_parsing_invalid_issue_string =
4649
`issue` must be a non-zero numeric string or "none"
4750
.must_not_be_zero = `issue` must not be "0", use "none" instead

‎compiler/rustc_attr_parsing/src/attributes/repr.rs‎

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_ast::{IntTy, LitIntType, LitKind, UintTy};
33
use rustc_attr_data_structures::{AttributeKind, IntType, ReprAttr};
44
use rustc_span::{DUMMY_SP, Span, Symbol, sym};
55

6-
use super::{CombineAttributeParser, ConvertFn};
6+
use super::{AcceptMapping, AttributeParser, CombineAttributeParser, ConvertFn, FinalizeContext};
77
use crate::context::{AcceptContext, Stage};
88
use crate::parser::{ArgParser, MetaItemListParser, MetaItemParser};
99
use crate::session_diagnostics;
@@ -262,3 +262,54 @@ fn parse_alignment(node: &LitKind) -> Result<Align, &'static str> {
262262
Err("not an unsuffixed integer")
263263
}
264264
}
265+
266+
/// Parse #[align(<integer>)].
267+
#[derive(Default)]
268+
pub(crate) struct AlignParser(Option<(Align, Span)>);
269+
270+
impl AlignParser {
271+
const PATH: &'static [Symbol] = &[sym::align];
272+
273+
fn parse<'c, S: Stage>(
274+
&mut self,
275+
cx: &'c mut AcceptContext<'_, '_, S>,
276+
args: &'c ArgParser<'_>,
277+
) {
278+
// The builtin attributes parser already emits an error in this case.
279+
let Some(list) = args.list() else { return };
280+
281+
let Some(align) = list.single() else {
282+
cx.dcx()
283+
.emit_err(session_diagnostics::IncorrectReprFormatAlignOneArg { span: list.span });
284+
285+
return;
286+
};
287+
288+
let Some(lit) = align.lit() else {
289+
cx.emit_err(session_diagnostics::IncorrectReprFormatExpectInteger {
290+
span: align.span(),
291+
});
292+
293+
return;
294+
};
295+
296+
match parse_alignment(&lit.kind) {
297+
Ok(literal) => self.0 = Ord::max(self.0, Some((literal, cx.attr_span))),
298+
Err(message) => {
299+
cx.emit_err(session_diagnostics::InvalidAlignmentValue {
300+
span: lit.span,
301+
error_part: message,
302+
});
303+
}
304+
}
305+
}
306+
}
307+
308+
impl<S: Stage> AttributeParser<S> for AlignParser {
309+
const ATTRIBUTES: AcceptMapping<Self, S> = &[(Self::PATH, Self::parse)];
310+
311+
fn finalize(self, _cx: &FinalizeContext<'_, '_, S>) -> Option<AttributeKind> {
312+
let (align, span) = self.0?;
313+
Some(AttributeKind::Align { align, span })
314+
}
315+
}

‎compiler/rustc_attr_parsing/src/context.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span, Symbol, sym};
1818
use crate::attributes::allow_unstable::{AllowConstFnUnstableParser, AllowInternalUnstableParser};
1919
use crate::attributes::confusables::ConfusablesParser;
2020
use crate::attributes::deprecation::DeprecationParser;
21-
use crate::attributes::repr::ReprParser;
21+
use crate::attributes::repr::{AlignParser, ReprParser};
2222
use crate::attributes::stability::{
2323
BodyStabilityParser, ConstStabilityIndirectParser, ConstStabilityParser, StabilityParser,
2424
};
@@ -89,6 +89,7 @@ macro_rules! attribute_parsers {
8989
attribute_parsers!(
9090
pub(crate) static ATTRIBUTE_PARSERS = [
9191
// tidy-alphabetical-start
92+
AlignParser,
9293
BodyStabilityParser,
9394
ConfusablesParser,
9495
ConstStabilityParser,

‎compiler/rustc_attr_parsing/src/session_diagnostics.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,3 +490,11 @@ pub(crate) struct UnrecognizedReprHint {
490490
#[primary_span]
491491
pub span: Span,
492492
}
493+
494+
#[derive(Diagnostic)]
495+
#[diag(attr_parsing_invalid_alignment_value, code = E0589)]
496+
pub(crate) struct InvalidAlignmentValue {
497+
#[primary_span]
498+
pub span: Span,
499+
pub error_part: &'static str,
500+
}

‎compiler/rustc_codegen_ssa/src/codegen_attrs.rs‎

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::str::FromStr;
33
use rustc_abi::ExternAbi;
44
use rustc_ast::expand::autodiff_attrs::{AutoDiffAttrs, DiffActivity, DiffMode};
55
use rustc_ast::{LitKind, MetaItem, MetaItemInner, attr};
6-
use rustc_attr_data_structures::ReprAttr::ReprAlign;
76
use rustc_attr_data_structures::{AttributeKind, InlineAttr, InstructionSetAttr, OptimizeAttr};
87
use rustc_data_structures::fx::FxHashMap;
98
use rustc_hir::def::DefKind;
@@ -110,17 +109,8 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
110109
}
111110
};
112111

113-
if let hir::Attribute::Parsed(p) = attr {
114-
match p {
115-
AttributeKind::Repr(reprs) => {
116-
codegen_fn_attrs.alignment = reprs
117-
.iter()
118-
.filter_map(|(r, _)| if let ReprAlign(x) = r { Some(*x) } else { None })
119-
.max();
120-
}
121-
122-
_ => {}
123-
}
112+
if let hir::Attribute::Parsed(AttributeKind::Align { align, .. }) = attr {
113+
codegen_fn_attrs.alignment = Ord::max(codegen_fn_attrs.alignment, Some(*align));
124114
}
125115

126116
let Some(Ident { name, .. }) = attr.ident() else {

‎compiler/rustc_feature/src/builtin_attrs.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
473473
),
474474
ungated!(no_link, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::No),
475475
ungated!(repr, Normal, template!(List: "C"), DuplicatesOk, EncodeCrossCrate::No),
476+
gated!(align, Normal, template!(List: "alignment"), DuplicatesOk, EncodeCrossCrate::No, fn_align, experimental!(align)),
476477
ungated!(unsafe(Edition2024) export_name, Normal, template!(NameValueStr: "name"), FutureWarnPreceding, EncodeCrossCrate::No),
477478
ungated!(unsafe(Edition2024) link_section, Normal, template!(NameValueStr: "name"), FutureWarnPreceding, EncodeCrossCrate::No),
478479
ungated!(unsafe(Edition2024) no_mangle, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::No),

‎compiler/rustc_middle/src/middle/codegen_fn_attrs.rs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ pub struct CodegenFnAttrs {
4747
/// be generated against a specific instruction set. Only usable on architectures which allow
4848
/// switching between multiple instruction sets.
4949
pub instruction_set: Option<InstructionSetAttr>,
50-
/// The `#[repr(align(...))]` attribute. Indicates the value of which the function should be
51-
/// aligned to.
50+
/// The `#[align(...)]` attribute. Determines the alignment of the function body.
5251
pub alignment: Option<Align>,
5352
/// The `#[patchable_function_entry(...)]` attribute. Indicates how many nops should be around
5453
/// the function entry.

‎compiler/rustc_passes/messages.ftl‎

Lines changed: 8 additions & 7 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_should_be_repr_align =
17+
`#[align(...)]` is not supported on {$item} items
18+
.suggestion = use `#[repr(align(...))]` instead
19+
1620
passes_allow_incoherent_impl =
1721
`rustc_allow_incoherent_impl` attribute should be applied to impl items
1822
.label = the only currently supported targets are inherent methods
@@ -29,10 +33,6 @@ passes_attr_application_struct =
2933
attribute should be applied to a struct
3034
.label = not a struct
3135
32-
passes_attr_application_struct_enum_function_method_union =
33-
attribute should be applied to a struct, enum, function, associated function, or union
34-
.label = not a struct, enum, function, associated function, or union
35-
3636
passes_attr_application_struct_enum_union =
3737
attribute should be applied to a struct, enum, or union
3838
.label = not a struct, enum, or union
@@ -583,13 +583,14 @@ passes_remove_fields =
583583
*[other] fields
584584
}
585585
586-
passes_repr_align_function =
587-
`repr(align)` attributes on functions are unstable
588-
589586
passes_repr_align_greater_than_target_max =
590587
alignment must not be greater than `isize::MAX` bytes
591588
.note = `isize::MAX` is {$size} for the current target
592589
590+
passes_repr_align_should_be_align =
591+
`#[repr(align(...))]` is not supported on {$item} items
592+
.help = use `#[align(...)]` instead
593+
593594
passes_repr_conflicting =
594595
conflicting representation hints
595596

‎compiler/rustc_passes/src/check_attr.rs‎

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,27 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
142142
}
143143
Attribute::Parsed(AttributeKind::Repr(_)) => { /* handled below this loop and elsewhere */
144144
}
145+
Attribute::Parsed(AttributeKind::Align { align, span: repr_span }) => {
146+
match target {
147+
Target::Fn | Target::Method(_) => {}
148+
Target::Struct | Target::Union | Target::Enum => {
149+
self.dcx().emit_err(errors::AlignShouldBeReprAlign {
150+
span: *repr_span,
151+
item: target.name(),
152+
align_bytes: align.bytes(),
153+
});
154+
}
155+
_ => {
156+
self.dcx().emit_err(errors::AttrApplication::StructEnumUnion {
157+
hint_span: *repr_span,
158+
span,
159+
});
160+
}
161+
}
162+
163+
self.check_align_value(*align, *repr_span);
164+
}
165+
145166
Attribute::Parsed(
146167
AttributeKind::BodyStability { .. }
147168
| AttributeKind::ConstStabilityIndirect
@@ -636,6 +657,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
636657
sym::naked,
637658
sym::instruction_set,
638659
sym::repr,
660+
sym::align,
639661
sym::rustc_std_internal_symbol,
640662
// code generation
641663
sym::cold,
@@ -672,7 +694,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
672694
// this check can be part of the parser and be removed here
673695
match other_attr {
674696
Attribute::Parsed(
675-
AttributeKind::Deprecation { .. } | AttributeKind::Repr { .. },
697+
AttributeKind::Deprecation { .. }
698+
| AttributeKind::Repr { .. }
699+
| AttributeKind::Align { .. },
676700
) => {
677701
continue;
678702
}
@@ -1999,23 +2023,16 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
19992023
match target {
20002024
Target::Struct | Target::Union | Target::Enum => {}
20012025
Target::Fn | Target::Method(_) => {
2002-
if !self.tcx.features().fn_align() {
2003-
feature_err(
2004-
&self.tcx.sess,
2005-
sym::fn_align,
2006-
*repr_span,
2007-
fluent::passes_repr_align_function,
2008-
)
2009-
.emit();
2010-
}
2026+
self.dcx().emit_err(errors::ReprAlignShouldBeAlign {
2027+
span: *repr_span,
2028+
item: target.name(),
2029+
});
20112030
}
20122031
_ => {
2013-
self.dcx().emit_err(
2014-
errors::AttrApplication::StructEnumFunctionMethodUnion {
2015-
hint_span: *repr_span,
2016-
span,
2017-
},
2018-
);
2032+
self.dcx().emit_err(errors::AttrApplication::StructEnumUnion {
2033+
hint_span: *repr_span,
2034+
span,
2035+
});
20192036
}
20202037
}
20212038

@@ -2073,21 +2090,16 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
20732090
match target {
20742091
Target::Struct | Target::Union | Target::Enum => continue,
20752092
Target::Fn | Target::Method(_) => {
2076-
feature_err(
2077-
&self.tcx.sess,
2078-
sym::fn_align,
2079-
*repr_span,
2080-
fluent::passes_repr_align_function,
2081-
)
2082-
.emit();
2093+
self.dcx().emit_err(errors::ReprAlignShouldBeAlign {
2094+
span: *repr_span,
2095+
item: target.name(),
2096+
});
20832097
}
20842098
_ => {
2085-
self.dcx().emit_err(
2086-
errors::AttrApplication::StructEnumFunctionMethodUnion {
2087-
hint_span: *repr_span,
2088-
span,
2089-
},
2090-
);
2099+
self.dcx().emit_err(errors::AttrApplication::StructEnumUnion {
2100+
hint_span: *repr_span,
2101+
span,
2102+
});
20912103
}
20922104
}
20932105
}

‎compiler/rustc_passes/src/errors.rs‎

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,13 +1308,6 @@ pub(crate) enum AttrApplication {
13081308
#[label]
13091309
span: Span,
13101310
},
1311-
#[diag(passes_attr_application_struct_enum_function_method_union, code = E0517)]
1312-
StructEnumFunctionMethodUnion {
1313-
#[primary_span]
1314-
hint_span: Span,
1315-
#[label]
1316-
span: Span,
1317-
},
13181311
}
13191312

13201313
#[derive(Diagnostic)]
@@ -1816,3 +1809,26 @@ pub(crate) enum UnexportableItem<'a> {
18161809
field_name: &'a str,
18171810
},
18181811
}
1812+
1813+
#[derive(Diagnostic)]
1814+
#[diag(passes_repr_align_should_be_align)]
1815+
pub(crate) struct ReprAlignShouldBeAlign {
1816+
#[primary_span]
1817+
#[help]
1818+
pub span: Span,
1819+
pub item: &'static str,
1820+
}
1821+
1822+
#[derive(Diagnostic)]
1823+
#[diag(passes_align_should_be_repr_align)]
1824+
pub(crate) struct AlignShouldBeReprAlign {
1825+
#[primary_span]
1826+
#[suggestion(
1827+
style = "verbose",
1828+
applicability = "machine-applicable",
1829+
code = "#[repr(align({align_bytes}))]"
1830+
)]
1831+
pub span: Span,
1832+
pub item: &'static str,
1833+
pub align_bytes: u64,
1834+
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
//@compile-flags: -Zmin-function-alignment=8
22
#![feature(fn_align)]
33

4-
// When a function uses `repr(align(N))`, the function address should be a multiple of `N`.
4+
// When a function uses `align(N)`, the function address should be a multiple of `N`.
55

6-
#[repr(align(256))]
6+
#[align(256)]
77
fn foo() {}
88

9-
#[repr(align(16))]
9+
#[align(16)]
1010
fn bar() {}
1111

12-
#[repr(align(4))]
12+
#[align(4)]
1313
fn baz() {}
1414

1515
fn main() {
1616
assert!((foo as usize).is_multiple_of(256));
1717
assert!((bar as usize).is_multiple_of(16));
1818

19-
// The maximum of `repr(align(N))` and `-Zmin-function-alignment=N` is used.
19+
// The maximum of `align(N)` and `-Zmin-function-alignment=N` is used.
2020
assert!((baz as usize).is_multiple_of(8));
2121
}

‎tests/codegen/align-fn.rs‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,40 @@
55

66
// CHECK: align 16
77
#[no_mangle]
8-
#[repr(align(16))]
8+
#[align(16)]
99
pub fn fn_align() {}
1010

1111
pub struct A;
1212

1313
impl A {
1414
// CHECK: align 16
1515
#[no_mangle]
16-
#[repr(align(16))]
16+
#[align(16)]
1717
pub fn method_align(self) {}
1818

1919
// CHECK: align 16
2020
#[no_mangle]
21-
#[repr(align(16))]
21+
#[align(16)]
2222
pub fn associated_fn() {}
2323
}
2424

2525
trait T: Sized {
2626
fn trait_fn() {}
2727

2828
// CHECK: align 32
29-
#[repr(align(32))]
29+
#[align(32)]
3030
fn trait_method(self) {}
3131
}
3232

3333
impl T for A {
3434
// CHECK: align 16
3535
#[no_mangle]
36-
#[repr(align(16))]
36+
#[align(16)]
3737
fn trait_fn() {}
3838

3939
// CHECK: align 16
4040
#[no_mangle]
41-
#[repr(align(16))]
41+
#[align(16)]
4242
fn trait_method(self) {}
4343
}
4444

@@ -51,18 +51,20 @@ pub fn foo() {
5151
// CHECK-LABEL: align_specified_twice_1
5252
// CHECK-SAME: align 64
5353
#[no_mangle]
54-
#[repr(align(32), align(64))]
54+
#[align(32)]
55+
#[align(64)]
5556
pub fn align_specified_twice_1() {}
5657

5758
// CHECK-LABEL: align_specified_twice_2
5859
// CHECK-SAME: align 128
5960
#[no_mangle]
60-
#[repr(align(128), align(32))]
61+
#[align(128)]
62+
#[align(32)]
6163
pub fn align_specified_twice_2() {}
6264

6365
// CHECK-LABEL: align_specified_twice_3
6466
// CHECK-SAME: align 256
6567
#[no_mangle]
66-
#[repr(align(32))]
67-
#[repr(align(256))]
68+
#[align(32)]
69+
#[align(256)]
6870
pub fn align_specified_twice_3() {}

‎tests/codegen/min-function-alignment.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ pub fn no_explicit_align() {}
1818
// align16: align 16
1919
// align1024: align 1024
2020
#[no_mangle]
21-
#[repr(align(8))]
21+
#[align(8)]
2222
pub fn lower_align() {}
2323

24-
// the higher value of min-function-alignment and repr(align) wins out
24+
// the higher value of min-function-alignment and the align attribute wins out
2525
//
2626
// CHECK-LABEL: @higher_align
2727
// align16: align 32
2828
// align1024: align 1024
2929
#[no_mangle]
30-
#[repr(align(32))]
30+
#[align(32)]
3131
pub fn higher_align() {}
3232

3333
// cold functions follow the same rules as other functions

‎tests/codegen/naked-fn/aligned.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::arch::naked_asm;
88

99
// CHECK: .balign 16
1010
// CHECK-LABEL: naked_empty:
11-
#[repr(align(16))]
11+
#[align(16)]
1212
#[no_mangle]
1313
#[unsafe(naked)]
1414
pub extern "C" fn naked_empty() {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ pub extern "C" fn naked_no_explicit_align() {
1616

1717
// CHECK: .balign 16
1818
#[no_mangle]
19-
#[repr(align(8))]
19+
#[align(8)]
2020
#[unsafe(naked)]
2121
pub extern "C" fn naked_lower_align() {
2222
core::arch::naked_asm!("ret")
2323
}
2424

2525
// CHECK: .balign 32
2626
#[no_mangle]
27-
#[repr(align(32))]
27+
#[align(32)]
2828
#[unsafe(naked)]
2929
pub extern "C" fn naked_higher_align() {
3030
core::arch::naked_asm!("ret")

‎tests/ui/asm/naked-with-invalid-repr-attr.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ extern "C" fn example2() {
1919
naked_asm!("")
2020
}
2121

22-
#[repr(align(16), C)]
22+
#[repr(C)]
2323
//~^ ERROR attribute should be applied to a struct, enum, or union [E0517]
24+
#[align(16)]
2425
#[unsafe(naked)]
2526
extern "C" fn example3() {
2627
//~^ NOTE not a struct, enum, or union

‎tests/ui/asm/naked-with-invalid-repr-attr.stderr‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ LL | | }
2323
| |_- not a struct, enum, or union
2424

2525
error[E0517]: attribute should be applied to a struct, enum, or union
26-
--> $DIR/naked-with-invalid-repr-attr.rs:22:19
26+
--> $DIR/naked-with-invalid-repr-attr.rs:22:8
2727
|
28-
LL | #[repr(align(16), C)]
29-
| ^
28+
LL | #[repr(C)]
29+
| ^
3030
...
3131
LL | / extern "C" fn example3() {
3232
LL | |
@@ -35,7 +35,7 @@ LL | | }
3535
| |_- not a struct, enum, or union
3636

3737
error[E0517]: attribute should be applied to a struct, enum, or union
38-
--> $DIR/naked-with-invalid-repr-attr.rs:31:8
38+
--> $DIR/naked-with-invalid-repr-attr.rs:32:8
3939
|
4040
LL | #[repr(C, packed)]
4141
| ^
@@ -48,7 +48,7 @@ LL | | }
4848
| |_- not a struct, enum, or union
4949

5050
error[E0517]: attribute should be applied to a struct or union
51-
--> $DIR/naked-with-invalid-repr-attr.rs:31:11
51+
--> $DIR/naked-with-invalid-repr-attr.rs:32:11
5252
|
5353
LL | #[repr(C, packed)]
5454
| ^^^^^^
@@ -61,7 +61,7 @@ LL | | }
6161
| |_- not a struct or union
6262

6363
error[E0517]: attribute should be applied to an enum
64-
--> $DIR/naked-with-invalid-repr-attr.rs:41:8
64+
--> $DIR/naked-with-invalid-repr-attr.rs:42:8
6565
|
6666
LL | #[repr(u8)]
6767
| ^^
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#[repr(align(16))]
2-
//~^ ERROR attribute should be applied to a struct, enum, function, associated function, or union
2+
//~^ ERROR attribute should be applied to a struct, enum, or union
33
pub type Foo = i32;
44

55
fn main() {}

‎tests/ui/attributes/invalid-repr.stderr‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
error[E0517]: attribute should be applied to a struct, enum, function, associated function, or union
1+
error[E0517]: attribute should be applied to a struct, enum, or union
22
--> $DIR/invalid-repr.rs:1:8
33
|
44
LL | #[repr(align(16))]
55
| ^^^^^^^^^
66
LL |
77
LL | pub type Foo = i32;
8-
| ------------------- not a struct, enum, function, associated function, or union
8+
| ------------------- not a struct, enum, or union
99

1010
error: aborting due to 1 previous error
1111

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,21 @@
22
#![crate_type = "lib"]
33

44
trait MyTrait {
5-
#[repr(align)] //~ ERROR invalid `repr(align)` attribute: `align` needs an argument
5+
#[align] //~ ERROR malformed `align` attribute input
66
fn myfun();
77
}
8+
9+
#[align = 16] //~ ERROR malformed `align` attribute input
10+
fn f1() {}
11+
12+
#[align("hello")] //~ ERROR invalid alignment value: not an unsuffixed integer
13+
fn f2() {}
14+
15+
#[align(0)] //~ ERROR invalid alignment value: not a power of two
16+
fn f3() {}
17+
18+
#[repr(align(16))] //~ ERROR `#[repr(align(...))]` is not supported on function items
19+
fn f4() {}
20+
21+
#[align(16)] //~ ERROR `#[align(...)]` is not supported on struct items
22+
struct S1;
Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,51 @@
1-
error[E0589]: invalid `repr(align)` attribute: `align` needs an argument
2-
--> $DIR/malformed-fn-align.rs:5:12
1+
error: malformed `align` attribute input
2+
--> $DIR/malformed-fn-align.rs:5:5
33
|
4-
LL | #[repr(align)]
5-
| ^^^^^ help: supply an argument here: `align(...)`
4+
LL | #[align]
5+
| ^^^^^^^^ help: must be of the form: `#[align(alignment)]`
66

7-
error: aborting due to 1 previous error
7+
error: malformed `align` attribute input
8+
--> $DIR/malformed-fn-align.rs:9:1
9+
|
10+
LL | #[align = 16]
11+
| ^^^^^^^^^^^^^ help: must be of the form: `#[align(alignment)]`
12+
13+
error[E0589]: invalid alignment value: not an unsuffixed integer
14+
--> $DIR/malformed-fn-align.rs:12:9
15+
|
16+
LL | #[align("hello")]
17+
| ^^^^^^^
18+
19+
error[E0589]: invalid alignment value: not a power of two
20+
--> $DIR/malformed-fn-align.rs:15:9
21+
|
22+
LL | #[align(0)]
23+
| ^
24+
25+
error: `#[repr(align(...))]` is not supported on function items
26+
--> $DIR/malformed-fn-align.rs:18:8
27+
|
28+
LL | #[repr(align(16))]
29+
| ^^^^^^^^^
30+
|
31+
help: use `#[align(...)]` instead
32+
--> $DIR/malformed-fn-align.rs:18:8
33+
|
34+
LL | #[repr(align(16))]
35+
| ^^^^^^^^^
36+
37+
error: `#[align(...)]` is not supported on struct items
38+
--> $DIR/malformed-fn-align.rs:21:1
39+
|
40+
LL | #[align(16)]
41+
| ^^^^^^^^^^^^
42+
|
43+
help: use `#[repr(align(...))]` instead
44+
|
45+
LL - #[align(16)]
46+
LL + #[repr(align(16))]
47+
|
48+
49+
error: aborting due to 6 previous errors
850

951
For more information about this error, try `rustc --explain E0589`.
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
#![crate_type = "lib"]
22

3-
#[repr(align(16))] //~ ERROR `repr(align)` attributes on functions are unstable
3+
#[align(16)]
4+
//~^ ERROR the `#[align]` attribute is an experimental feature
45
fn requires_alignment() {}
56

67
trait MyTrait {
7-
#[repr(align)] //~ ERROR invalid `repr(align)` attribute: `align` needs an argument
8+
#[align]
9+
//~^ ERROR the `#[align]` attribute is an experimental feature
10+
//~| ERROR malformed `align` attribute input
811
fn myfun();
912
}
Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
1-
error[E0589]: invalid `repr(align)` attribute: `align` needs an argument
2-
--> $DIR/feature-gate-fn_align.rs:7:12
1+
error: malformed `align` attribute input
2+
--> $DIR/feature-gate-fn_align.rs:8:5
33
|
4-
LL | #[repr(align)]
5-
| ^^^^^ help: supply an argument here: `align(...)`
4+
LL | #[align]
5+
| ^^^^^^^^ help: must be of the form: `#[align(alignment)]`
66

7-
error[E0658]: `repr(align)` attributes on functions are unstable
8-
--> $DIR/feature-gate-fn_align.rs:3:8
7+
error[E0658]: the `#[align]` attribute is an experimental feature
8+
--> $DIR/feature-gate-fn_align.rs:3:1
99
|
10-
LL | #[repr(align(16))]
11-
| ^^^^^^^^^
10+
LL | #[align(16)]
11+
| ^^^^^^^^^^^^
1212
|
1313
= note: see issue #82232 <https://github.com/rust-lang/rust/issues/82232> for more information
1414
= help: add `#![feature(fn_align)]` to the crate attributes to enable
1515
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
1616

17-
error: aborting due to 2 previous errors
17+
error[E0658]: the `#[align]` attribute is an experimental feature
18+
--> $DIR/feature-gate-fn_align.rs:8:5
19+
|
20+
LL | #[align]
21+
| ^^^^^^^^
22+
|
23+
= note: see issue #82232 <https://github.com/rust-lang/rust/issues/82232> for more information
24+
= help: add `#![feature(fn_align)]` to the crate attributes to enable
25+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
26+
27+
error: aborting due to 3 previous errors
1828

19-
Some errors have detailed explanations: E0589, E0658.
20-
For more information about an error, try `rustc --explain E0589`.
29+
For more information about this error, try `rustc --explain E0658`.

‎tests/ui/repr/attr-usage-repr.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ enum EInt {
4545
B,
4646
}
4747

48-
#[repr()] //~ ERROR attribute should be applied to a struct, enum, function, associated function, or union [E0517]
48+
#[repr()] //~ ERROR attribute should be applied to a struct, enum, or union [E0517]
4949
type SirThisIsAType = i32;
5050

5151
#[repr()]

‎tests/ui/repr/attr-usage-repr.stderr‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ LL | | B,
3636
LL | | }
3737
| |_- not a struct
3838

39-
error[E0517]: attribute should be applied to a struct, enum, function, associated function, or union
39+
error[E0517]: attribute should be applied to a struct, enum, or union
4040
--> $DIR/attr-usage-repr.rs:48:1
4141
|
4242
LL | #[repr()]
4343
| ^^^^^^^^^
4444
LL | type SirThisIsAType = i32;
45-
| -------------------------- not a struct, enum, function, associated function, or union
45+
| -------------------------- not a struct, enum, or union
4646

4747
error: aborting due to 5 previous errors
4848

0 commit comments

Comments
 (0)
Please sign in to comment.