Skip to content

Commit fce2a22

Browse files
pnkfelixMark-Simulacrum
authored andcommitted
Revert "Allow specifying alignment for functions"
This reverts commit 448d076 to address issue 85713 on beta.
1 parent 2c521ae commit fce2a22

File tree

18 files changed

+83
-137
lines changed

18 files changed

+83
-137
lines changed

compiler/rustc_attr/src/builtin.rs

+36-14
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,18 @@ pub fn find_repr_attrs(sess: &Session, attr: &Attribute) -> Vec<ReprAttr> {
862862
if let Some(items) = attr.meta_item_list() {
863863
sess.mark_attr_used(attr);
864864
for item in items {
865+
if !item.is_meta_item() {
866+
handle_errors(
867+
&sess.parse_sess,
868+
item.span(),
869+
AttrError::UnsupportedLiteral(
870+
"meta item in `repr` must be an identifier",
871+
false,
872+
),
873+
);
874+
continue;
875+
}
876+
865877
let mut recognised = false;
866878
if item.is_word() {
867879
let hint = match item.name_or_empty() {
@@ -878,6 +890,23 @@ pub fn find_repr_attrs(sess: &Session, attr: &Attribute) -> Vec<ReprAttr> {
878890
acc.push(h);
879891
}
880892
} else if let Some((name, value)) = item.name_value_literal() {
893+
let parse_alignment = |node: &ast::LitKind| -> Result<u32, &'static str> {
894+
if let ast::LitKind::Int(literal, ast::LitIntType::Unsuffixed) = node {
895+
if literal.is_power_of_two() {
896+
// rustc_middle::ty::layout::Align restricts align to <= 2^29
897+
if *literal <= 1 << 29 {
898+
Ok(*literal as u32)
899+
} else {
900+
Err("larger than 2^29")
901+
}
902+
} else {
903+
Err("not a power of two")
904+
}
905+
} else {
906+
Err("not an unsuffixed integer")
907+
}
908+
};
909+
881910
let mut literal_error = None;
882911
if name == sym::align {
883912
recognised = true;
@@ -937,7 +966,13 @@ pub fn find_repr_attrs(sess: &Session, attr: &Attribute) -> Vec<ReprAttr> {
937966
}
938967
if !recognised {
939968
// Not a word we recognize
940-
diagnostic.delay_span_bug(item.span(), "unrecognized representation hint");
969+
struct_span_err!(
970+
diagnostic,
971+
item.span(),
972+
E0552,
973+
"unrecognized representation hint"
974+
)
975+
.emit();
941976
}
942977
}
943978
}
@@ -1045,16 +1080,3 @@ fn allow_unstable<'a>(
10451080
name
10461081
})
10471082
}
1048-
1049-
pub fn parse_alignment(node: &ast::LitKind) -> Result<u32, &'static str> {
1050-
if let ast::LitKind::Int(literal, ast::LitIntType::Unsuffixed) = node {
1051-
if literal.is_power_of_two() {
1052-
// rustc_middle::ty::layout::Align restricts align to <= 2^29
1053-
if *literal <= 1 << 29 { Ok(*literal as u32) } else { Err("larger than 2^29") }
1054-
} else {
1055-
Err("not a power of two")
1056-
}
1057-
} else {
1058-
Err("not an unsuffixed integer")
1059-
}
1060-
}

compiler/rustc_codegen_llvm/src/attributes.rs

-3
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,6 @@ pub fn from_fn_attrs(cx: &CodegenCx<'ll, 'tcx>, llfn: &'ll Value, instance: ty::
281281
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::CMSE_NONSECURE_ENTRY) {
282282
llvm::AddFunctionAttrString(llfn, Function, cstr!("cmse_nonsecure_entry"));
283283
}
284-
if let Some(align) = codegen_fn_attrs.alignment {
285-
llvm::set_alignment(llfn, align as usize);
286-
}
287284
sanitize(cx, codegen_fn_attrs.no_sanitize, llfn);
288285

289286
// Always annotate functions with the target-cpu they are compiled for.

compiler/rustc_feature/src/active.rs

-3
Original file line numberDiff line numberDiff line change
@@ -637,9 +637,6 @@ declare_features! (
637637
/// Allows `extern "C-unwind" fn` to enable unwinding across ABI boundaries.
638638
(active, c_unwind, "1.52.0", Some(74990), None),
639639

640-
/// Allows using `#[repr(align(...))]` on function items
641-
(active, fn_align, "1.53.0", Some(82232), None),
642-
643640
/// Allows `extern "wasm" fn`
644641
(active, wasm_abi, "1.53.0", Some(83788), None),
645642

compiler/rustc_middle/src/middle/codegen_fn_attrs.rs

-4
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ pub struct CodegenFnAttrs {
3838
/// be generated against a specific instruction set. Only usable on architectures which allow
3939
/// switching between multiple instruction sets.
4040
pub instruction_set: Option<InstructionSetAttr>,
41-
/// The `#[repr(align(...))]` attribute. Indicates the value of which the function should be
42-
/// aligned to.
43-
pub alignment: Option<u32>,
4441
}
4542

4643
bitflags! {
@@ -110,7 +107,6 @@ impl CodegenFnAttrs {
110107
link_section: None,
111108
no_sanitize: SanitizerSet::empty(),
112109
instruction_set: None,
113-
alignment: None,
114110
}
115111
}
116112

compiler/rustc_passes/src/check_attr.rs

+6-40
Original file line numberDiff line numberDiff line change
@@ -1120,41 +1120,17 @@ impl CheckAttrVisitor<'tcx> {
11201120
let mut is_transparent = false;
11211121

11221122
for hint in &hints {
1123-
if !hint.is_meta_item() {
1124-
struct_span_err!(
1125-
self.tcx.sess,
1126-
hint.span(),
1127-
E0565,
1128-
"meta item in `repr` must be an identifier"
1129-
)
1130-
.emit();
1131-
continue;
1132-
}
1133-
11341123
let (article, allowed_targets) = match hint.name_or_empty() {
1135-
sym::C => {
1136-
is_c = true;
1124+
_ if !matches!(target, Target::Struct | Target::Enum | Target::Union) => {
1125+
("a", "struct, enum, or union")
1126+
}
1127+
name @ sym::C | name @ sym::align => {
1128+
is_c |= name == sym::C;
11371129
match target {
11381130
Target::Struct | Target::Union | Target::Enum => continue,
11391131
_ => ("a", "struct, enum, or union"),
11401132
}
11411133
}
1142-
sym::align => {
1143-
if let (Target::Fn, true) = (target, !self.tcx.features().fn_align) {
1144-
feature_err(
1145-
&self.tcx.sess.parse_sess,
1146-
sym::fn_align,
1147-
hint.span(),
1148-
"`repr(align)` attributes on functions are unstable",
1149-
)
1150-
.emit();
1151-
}
1152-
1153-
match target {
1154-
Target::Struct | Target::Union | Target::Enum | Target::Fn => continue,
1155-
_ => ("a", "struct, enum, function, or union"),
1156-
}
1157-
}
11581134
sym::packed => {
11591135
if target != Target::Struct && target != Target::Union {
11601136
("a", "struct or union")
@@ -1211,17 +1187,7 @@ impl CheckAttrVisitor<'tcx> {
12111187
continue;
12121188
}
12131189
}
1214-
_ => {
1215-
struct_span_err!(
1216-
self.tcx.sess,
1217-
hint.span(),
1218-
E0552,
1219-
"unrecognized representation hint"
1220-
)
1221-
.emit();
1222-
1223-
continue;
1224-
}
1190+
_ => continue,
12251191
};
12261192

12271193
struct_span_err!(

compiler/rustc_span/src/symbol.rs

-1
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,6 @@ symbols! {
563563
fmt,
564564
fmt_internals,
565565
fmul_fast,
566-
fn_align,
567566
fn_must_use,
568567
fn_mut,
569568
fn_once,

compiler/rustc_typeck/src/collect.rs

-32
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
//! At present, however, we do run collection across all items in the
1616
//! crate as a kind of pass. This should eventually be factored away.
1717
18-
// ignore-tidy-filelength
19-
2018
use crate::astconv::{AstConv, SizedByDefault};
2119
use crate::bounds::Bounds;
2220
use crate::check::intrinsic::intrinsic_operation_unsafety;
@@ -2903,36 +2901,6 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
29032901
None
29042902
}
29052903
};
2906-
} else if tcx.sess.check_name(attr, sym::repr) {
2907-
codegen_fn_attrs.alignment = match attr.meta_item_list() {
2908-
Some(items) => match items.as_slice() {
2909-
[item] => match item.name_value_literal() {
2910-
Some((sym::align, literal)) => {
2911-
let alignment = rustc_attr::parse_alignment(&literal.kind);
2912-
2913-
match alignment {
2914-
Ok(align) => Some(align),
2915-
Err(msg) => {
2916-
struct_span_err!(
2917-
tcx.sess.diagnostic(),
2918-
attr.span,
2919-
E0589,
2920-
"invalid `repr(align)` attribute: {}",
2921-
msg
2922-
)
2923-
.emit();
2924-
2925-
None
2926-
}
2927-
}
2928-
}
2929-
_ => None,
2930-
},
2931-
[] => None,
2932-
_ => None,
2933-
},
2934-
None => None,
2935-
};
29362904
}
29372905
}
29382906

src/test/codegen/align-fn.rs

-9
This file was deleted.

src/test/ui/attributes/nonterminal-expansion.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
macro_rules! pass_nonterminal {
44
($n:expr) => {
55
#[repr(align($n))] //~ ERROR expected unsuffixed literal or identifier, found `n!()`
6+
//~| ERROR unrecognized representation hint
67
struct S;
78
};
89
}

src/test/ui/attributes/nonterminal-expansion.stderr

+13-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,17 @@ LL | pass_nonterminal!(n!());
99
|
1010
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
1111

12-
error: aborting due to previous error
12+
error[E0552]: unrecognized representation hint
13+
--> $DIR/nonterminal-expansion.rs:5:16
14+
|
15+
LL | #[repr(align($n))]
16+
| ^^^^^^^^^
17+
...
18+
LL | pass_nonterminal!(n!());
19+
| ------------------------ in this macro invocation
20+
|
21+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
22+
23+
error: aborting due to 2 previous errors
1324

25+
For more information about this error, try `rustc --explain E0552`.

src/test/ui/error-codes/E0565.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// repr currently doesn't support literals
22
#[repr("C")] //~ ERROR E0565
3-
struct A {}
3+
//~| ERROR E0565
4+
struct A { }
45

5-
fn main() {}
6+
fn main() { }

src/test/ui/error-codes/E0565.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ error[E0565]: meta item in `repr` must be an identifier
44
LL | #[repr("C")]
55
| ^^^
66

7-
error: aborting due to previous error
7+
error[E0565]: meta item in `repr` must be an identifier
8+
--> $DIR/E0565.rs:2:8
9+
|
10+
LL | #[repr("C")]
11+
| ^^^
12+
13+
error: aborting due to 2 previous errors
814

915
For more information about this error, try `rustc --explain E0565`.

src/test/ui/feature-gates/feature-gate-fn_align.rs

-4
This file was deleted.

src/test/ui/feature-gates/feature-gate-fn_align.stderr

-12
This file was deleted.

src/test/ui/issues/issue-43988.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ fn main() {
1313

1414
#[repr(nothing)]
1515
let _x = 0;
16-
//~^^ ERROR E0552
16+
//~^^ ERROR attribute should be applied to a struct, enum, or union
1717

1818
#[repr(something_not_real)]
1919
loop {
2020
()
2121
};
22-
//~^^^^ ERROR E0552
22+
//~^^^^ ERROR attribute should be applied to a struct, enum, or union
2323

2424
#[repr]
2525
let _y = "123";

src/test/ui/issues/issue-43988.stderr

+12-6
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,23 @@ LL | #[inline(XYZ)]
2626
LL | let _b = 4;
2727
| ----------- not a function or closure
2828

29-
error[E0552]: unrecognized representation hint
29+
error[E0517]: attribute should be applied to a struct, enum, or union
3030
--> $DIR/issue-43988.rs:14:12
3131
|
3232
LL | #[repr(nothing)]
3333
| ^^^^^^^
34+
LL | let _x = 0;
35+
| ----------- not a struct, enum, or union
3436

35-
error[E0552]: unrecognized representation hint
37+
error[E0517]: attribute should be applied to a struct, enum, or union
3638
--> $DIR/issue-43988.rs:18:12
3739
|
38-
LL | #[repr(something_not_real)]
39-
| ^^^^^^^^^^^^^^^^^^
40+
LL | #[repr(something_not_real)]
41+
| ^^^^^^^^^^^^^^^^^^
42+
LL | / loop {
43+
LL | | ()
44+
LL | | };
45+
| |_____- not a struct, enum, or union
4046

4147
error[E0518]: attribute should be applied to function or closure
4248
--> $DIR/issue-43988.rs:30:5
@@ -48,5 +54,5 @@ LL | foo();
4854

4955
error: aborting due to 7 previous errors
5056

51-
Some errors have detailed explanations: E0518, E0552.
52-
For more information about an error, try `rustc --explain E0518`.
57+
Some errors have detailed explanations: E0517, E0518.
58+
For more information about an error, try `rustc --explain E0517`.

src/test/ui/repr/repr-disallow-on-variant.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ struct Test;
22

33
enum Foo {
44
#[repr(u8)]
5-
//~^ ERROR attribute should be applied to an enum
5+
//~^ ERROR attribute should be applied to a struct, enum, or union
66
Variant,
77
}
88

src/test/ui/repr/repr-disallow-on-variant.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
error[E0517]: attribute should be applied to an enum
1+
error[E0517]: attribute should be applied to a struct, enum, or union
22
--> $DIR/repr-disallow-on-variant.rs:4:12
33
|
44
LL | #[repr(u8)]
55
| ^^
66
LL |
77
LL | Variant,
8-
| ------- not an enum
8+
| ------- not a struct, enum, or union
99

1010
error: aborting due to previous error
1111

0 commit comments

Comments
 (0)