Skip to content

Commit d3219ef

Browse files
authored
Unrolled build for #153189
Rollup merge of #153189 - JayanAXHF:refactor/check_attrs_reftor_1, r=JonathanBrouwer refactor: move `check_align` to `parse_alignment` Part of #153101 r? @JonathanBrouwer PS: jonathan i'm not sure about what to do with `check_align` now
2 parents a3ac2f2 + 2093158 commit d3219ef

6 files changed

Lines changed: 65 additions & 89 deletions

File tree

compiler/rustc_attr_parsing/src/attributes/repr.rs

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_abi::Align;
1+
use rustc_abi::{Align, Size};
22
use rustc_ast::{IntTy, LitIntType, LitKind, UintTy};
33
use rustc_hir::attrs::{IntType, ReprAttr};
44

@@ -229,7 +229,7 @@ fn parse_repr_align<S: Stage>(
229229
return None;
230230
};
231231

232-
match parse_alignment(&lit.kind) {
232+
match parse_alignment(&lit.kind, cx) {
233233
Ok(literal) => Some(match align_kind {
234234
AlignKind::Packed => ReprAttr::ReprPacked(literal),
235235
AlignKind::Align => ReprAttr::ReprAlign(literal),
@@ -248,23 +248,35 @@ fn parse_repr_align<S: Stage>(
248248
}
249249
}
250250

251-
fn parse_alignment(node: &LitKind) -> Result<Align, &'static str> {
252-
if let LitKind::Int(literal, LitIntType::Unsuffixed) = node {
253-
// `Align::from_bytes` accepts 0 as an input, check is_power_of_two() first
254-
if literal.get().is_power_of_two() {
255-
// Only possible error is larger than 2^29
256-
literal
257-
.get()
258-
.try_into()
259-
.ok()
260-
.and_then(|v| Align::from_bytes(v).ok())
261-
.ok_or("larger than 2^29")
262-
} else {
263-
Err("not a power of two")
264-
}
265-
} else {
266-
Err("not an unsuffixed integer")
251+
fn parse_alignment<S: Stage>(
252+
node: &LitKind,
253+
cx: &AcceptContext<'_, '_, S>,
254+
) -> Result<Align, String> {
255+
let LitKind::Int(literal, LitIntType::Unsuffixed) = node else {
256+
return Err("not an unsuffixed integer".to_string());
257+
};
258+
259+
// `Align::from_bytes` accepts 0 as a valid input,
260+
// so we check if its a power of two first
261+
if !literal.get().is_power_of_two() {
262+
return Err("not a power of two".to_string());
263+
}
264+
// lit must be < 2^29
265+
let align = literal
266+
.get()
267+
.try_into()
268+
.ok()
269+
.and_then(|a| Align::from_bytes(a).ok())
270+
.ok_or("larger than 2^29".to_string())?;
271+
272+
// alignment must not be larger than the pointer width (`isize::MAX`)
273+
let max = Size::from_bits(cx.sess.target.pointer_width).signed_int_max() as u64;
274+
if align.bytes() > max {
275+
return Err(format!(
276+
"alignment larger than `isize::MAX` bytes ({max} for the current target)"
277+
));
267278
}
279+
Ok(align)
268280
}
269281

270282
/// Parse #[align(N)].
@@ -294,7 +306,7 @@ impl RustcAlignParser {
294306
return;
295307
};
296308

297-
match parse_alignment(&lit.kind) {
309+
match parse_alignment(&lit.kind, cx) {
298310
Ok(literal) => self.0 = Ord::max(self.0, Some((literal, cx.attr_span))),
299311
Err(message) => {
300312
cx.emit_err(session_diagnostics::InvalidAlignmentValue {

compiler/rustc_attr_parsing/src/session_diagnostics.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,12 @@ pub(crate) struct InvalidReprAlignNeedArg {
227227

228228
#[derive(Diagnostic)]
229229
#[diag("invalid `repr({$repr_arg})` attribute: {$error_part}", code = E0589)]
230-
pub(crate) struct InvalidReprGeneric<'a> {
230+
pub(crate) struct InvalidReprGeneric {
231231
#[primary_span]
232232
pub span: Span,
233233

234234
pub repr_arg: String,
235-
pub error_part: &'a str,
235+
pub error_part: String,
236236
}
237237

238238
#[derive(Diagnostic)]
@@ -479,7 +479,7 @@ pub(crate) struct InvalidTarget {
479479
pub(crate) struct InvalidAlignmentValue {
480480
#[primary_span]
481481
pub span: Span,
482-
pub error_part: &'static str,
482+
pub error_part: String,
483483
}
484484

485485
#[derive(Diagnostic)]

compiler/rustc_passes/src/check_attr.rs

Lines changed: 23 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::cell::Cell;
99
use std::collections::hash_map::Entry;
1010
use std::slice;
1111

12-
use rustc_abi::{Align, ExternAbi, Size};
12+
use rustc_abi::ExternAbi;
1313
use rustc_ast::{AttrStyle, MetaItemKind, ast};
1414
use rustc_attr_parsing::{AttributeParser, Late};
1515
use rustc_data_structures::fx::FxHashMap;
@@ -190,8 +190,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
190190
&Attribute::Parsed(AttributeKind::RustcPubTransparent(attr_span)) => {
191191
self.check_rustc_pub_transparent(attr_span, span, attrs)
192192
}
193-
Attribute::Parsed(AttributeKind::RustcAlign { align, span: attr_span }) => {
194-
self.check_align(*align, *attr_span)
193+
Attribute::Parsed(AttributeKind::RustcAlign {..}) => {
194+
195195
}
196196
Attribute::Parsed(AttributeKind::Naked(..)) => {
197197
self.check_naked(hir_id, target)
@@ -1335,31 +1335,27 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
13351335
}
13361336
}
13371337
}
1338-
ReprAttr::ReprAlign(align) => {
1339-
match target {
1340-
Target::Struct | Target::Union | Target::Enum => {}
1341-
Target::Fn | Target::Method(_) if self.tcx.features().fn_align() => {
1342-
self.dcx().emit_err(errors::ReprAlignShouldBeAlign {
1343-
span: *repr_span,
1344-
item: target.plural_name(),
1345-
});
1346-
}
1347-
Target::Static if self.tcx.features().static_align() => {
1348-
self.dcx().emit_err(errors::ReprAlignShouldBeAlignStatic {
1349-
span: *repr_span,
1350-
item: target.plural_name(),
1351-
});
1352-
}
1353-
_ => {
1354-
self.dcx().emit_err(errors::AttrApplication::StructEnumUnion {
1355-
hint_span: *repr_span,
1356-
span,
1357-
});
1358-
}
1338+
ReprAttr::ReprAlign(..) => match target {
1339+
Target::Struct | Target::Union | Target::Enum => {}
1340+
Target::Fn | Target::Method(_) if self.tcx.features().fn_align() => {
1341+
self.dcx().emit_err(errors::ReprAlignShouldBeAlign {
1342+
span: *repr_span,
1343+
item: target.plural_name(),
1344+
});
13591345
}
1360-
1361-
self.check_align(*align, *repr_span);
1362-
}
1346+
Target::Static if self.tcx.features().static_align() => {
1347+
self.dcx().emit_err(errors::ReprAlignShouldBeAlignStatic {
1348+
span: *repr_span,
1349+
item: target.plural_name(),
1350+
});
1351+
}
1352+
_ => {
1353+
self.dcx().emit_err(errors::AttrApplication::StructEnumUnion {
1354+
hint_span: *repr_span,
1355+
span,
1356+
});
1357+
}
1358+
},
13631359
ReprAttr::ReprPacked(_) => {
13641360
if target != Target::Struct && target != Target::Union {
13651361
self.dcx().emit_err(errors::AttrApplication::StructUnion {
@@ -1475,25 +1471,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
14751471
}
14761472
}
14771473

1478-
fn check_align(&self, align: Align, span: Span) {
1479-
if align.bytes() > 2_u64.pow(29) {
1480-
// for values greater than 2^29, a different error will be emitted, make sure that happens
1481-
self.dcx().span_delayed_bug(
1482-
span,
1483-
"alignment greater than 2^29 should be errored on elsewhere",
1484-
);
1485-
} else {
1486-
// only do this check when <= 2^29 to prevent duplicate errors:
1487-
// alignment greater than 2^29 not supported
1488-
// alignment is too large for the current target
1489-
1490-
let max = Size::from_bits(self.tcx.sess.target.pointer_width).signed_int_max() as u64;
1491-
if align.bytes() > max {
1492-
self.dcx().emit_err(errors::InvalidReprAlignForTarget { span, size: max });
1493-
}
1494-
}
1495-
}
1496-
14971474
/// Outputs an error for attributes that can only be applied to macros, such as
14981475
/// `#[allow_internal_unsafe]` and `#[allow_internal_unstable]`.
14991476
/// (Allows proc_macro functions)

compiler/rustc_passes/src/errors.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -236,15 +236,6 @@ pub(crate) struct ReprConflicting {
236236
pub hint_spans: Vec<Span>,
237237
}
238238

239-
#[derive(Diagnostic)]
240-
#[diag("alignment must not be greater than `isize::MAX` bytes", code = E0589)]
241-
#[note("`isize::MAX` is {$size} for the current target")]
242-
pub(crate) struct InvalidReprAlignForTarget {
243-
#[primary_span]
244-
pub span: Span,
245-
pub size: u64,
246-
}
247-
248239
#[derive(Diagnostic)]
249240
#[diag("conflicting representation hints", code = E0566)]
250241
pub(crate) struct ReprConflictingLint;

tests/ui/repr/repr_align_greater_usize.msp430.stderr

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
error[E0589]: alignment must not be greater than `isize::MAX` bytes
2-
--> $DIR/repr_align_greater_usize.rs:23:8
1+
error[E0589]: invalid `repr(align)` attribute: alignment larger than `isize::MAX` bytes (32767 for the current target)
2+
--> $DIR/repr_align_greater_usize.rs:23:14
33
|
44
LL | #[repr(align(32768))]
5-
| ^^^^^^^^^^^^
6-
|
7-
= note: `isize::MAX` is 32767 for the current target
5+
| ^^^^^
86

9-
error[E0589]: alignment must not be greater than `isize::MAX` bytes
10-
--> $DIR/repr_align_greater_usize.rs:26:8
7+
error[E0589]: invalid `repr(align)` attribute: alignment larger than `isize::MAX` bytes (32767 for the current target)
8+
--> $DIR/repr_align_greater_usize.rs:26:14
119
|
1210
LL | #[repr(align(65536))]
13-
| ^^^^^^^^^^^^
14-
|
15-
= note: `isize::MAX` is 32767 for the current target
11+
| ^^^^^
1612

1713
error: aborting due to 2 previous errors
1814

tests/ui/repr/repr_align_greater_usize.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ use minicore::*;
2020
#[repr(align(16384))]
2121
struct Kitten;
2222

23-
#[repr(align(32768))] //[msp430]~ ERROR alignment must not be greater than `isize::MAX`
23+
#[repr(align(32768))] //[msp430]~ ERROR invalid `repr(align)` attribute: alignment larger than `isize::MAX` bytes (32767 for the current target) [E0589]
2424
struct Cat;
2525

26-
#[repr(align(65536))] //[msp430]~ ERROR alignment must not be greater than `isize::MAX`
26+
#[repr(align(65536))] //[msp430]~ ERROR invalid `repr(align)` attribute: alignment larger than `isize::MAX` bytes (32767 for the current target) [E0589]
2727
struct BigCat;

0 commit comments

Comments
 (0)