Skip to content

Commit 69dcce6

Browse files
authored
Unrolled build for rust-lang#131633
Rollup merge of rust-lang#131633 - asquared31415:align_isize_max, r=jieyouxu error on alignments greater than `isize::MAX` Fixes rust-lang#131122 On zulip someone had a concern that it was legal to create a type with alignment `isize::MAX + 1`, but I do not believe this should be allowed for `repr(align)`, as `repr(align)` also increases the *size* of the type, and that would be larger than the maximum allowed size of objects. (cc `@workingjubilee` rust-lang#131276)
2 parents 6929a48 + 6fc7ce4 commit 69dcce6

File tree

5 files changed

+100
-1
lines changed

5 files changed

+100
-1
lines changed

Diff for: compiler/rustc_passes/messages.ftl

+4
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,10 @@ passes_remove_fields =
622622
passes_repr_align_function =
623623
`repr(align)` attributes on functions are unstable
624624
625+
passes_repr_align_greater_than_target_max =
626+
alignment must not be greater than `isize::MAX` bytes
627+
.note = `isize::MAX` is {$size} for the current target
628+
625629
passes_repr_conflicting =
626630
conflicting representation hints
627631

Diff for: compiler/rustc_passes/src/check_attr.rs

+43-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use rustc_session::lint::builtin::{
3434
use rustc_session::parse::feature_err;
3535
use rustc_span::symbol::{Symbol, kw, sym};
3636
use rustc_span::{BytePos, DUMMY_SP, Span};
37+
use rustc_target::abi::Size;
3738
use rustc_target::spec::abi::Abi;
3839
use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
3940
use rustc_trait_selection::infer::{TyCtxtInferExt, ValuePairs};
@@ -1785,7 +1786,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
17851786
| Target::Union
17861787
| Target::Enum
17871788
| Target::Fn
1788-
| Target::Method(_) => continue,
1789+
| Target::Method(_) => {}
17891790
_ => {
17901791
self.dcx().emit_err(
17911792
errors::AttrApplication::StructEnumFunctionMethodUnion {
@@ -1795,6 +1796,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
17951796
);
17961797
}
17971798
}
1799+
1800+
self.check_align_value(hint);
17981801
}
17991802
sym::packed => {
18001803
if target != Target::Struct && target != Target::Union {
@@ -1892,6 +1895,45 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
18921895
}
18931896
}
18941897

1898+
fn check_align_value(&self, item: &MetaItemInner) {
1899+
match item.singleton_lit_list() {
1900+
Some((
1901+
_,
1902+
MetaItemLit {
1903+
kind: ast::LitKind::Int(literal, ast::LitIntType::Unsuffixed), ..
1904+
},
1905+
)) => {
1906+
let val = literal.get() as u64;
1907+
if val > 2_u64.pow(29) {
1908+
// for values greater than 2^29, a different error will be emitted, make sure that happens
1909+
self.dcx().span_delayed_bug(
1910+
item.span(),
1911+
"alignment greater than 2^29 should be errored on elsewhere",
1912+
);
1913+
} else {
1914+
// only do this check when <= 2^29 to prevent duplicate errors:
1915+
// alignment greater than 2^29 not supported
1916+
// alignment is too large for the current target
1917+
1918+
let max =
1919+
Size::from_bits(self.tcx.sess.target.pointer_width).signed_int_max() as u64;
1920+
if val > max {
1921+
self.dcx().emit_err(errors::InvalidReprAlignForTarget {
1922+
span: item.span(),
1923+
size: max,
1924+
});
1925+
}
1926+
}
1927+
}
1928+
1929+
// if the attribute is malformed, singleton_lit_list may not be of the expected type or may be None
1930+
// but an error will have already been emitted, so this code should just skip such attributes
1931+
Some((_, _)) | None => {
1932+
self.dcx().span_delayed_bug(item.span(), "malformed repr(align(N))");
1933+
}
1934+
}
1935+
}
1936+
18951937
fn check_used(&self, attrs: &[Attribute], target: Target, target_span: Span) {
18961938
let mut used_linker_span = None;
18971939
let mut used_compiler_span = None;

Diff for: compiler/rustc_passes/src/errors.rs

+9
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,15 @@ pub(crate) struct ReprConflicting {
567567
pub hint_spans: Vec<Span>,
568568
}
569569

570+
#[derive(Diagnostic)]
571+
#[diag(passes_repr_align_greater_than_target_max, code = E0589)]
572+
#[note]
573+
pub(crate) struct InvalidReprAlignForTarget {
574+
#[primary_span]
575+
pub span: Span,
576+
pub size: u64,
577+
}
578+
570579
#[derive(LintDiagnostic)]
571580
#[diag(passes_repr_conflicting, code = E0566)]
572581
pub(crate) struct ReprConflictingLint;

Diff for: tests/ui/repr/repr_align_greater_usize.msp430.stderr

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0589]: alignment must not be greater than `isize::MAX` bytes
2+
--> $DIR/repr_align_greater_usize.rs:21:8
3+
|
4+
LL | #[repr(align(32768))]
5+
| ^^^^^^^^^^^^
6+
|
7+
= note: `isize::MAX` is 32767 for the current target
8+
9+
error[E0589]: alignment must not be greater than `isize::MAX` bytes
10+
--> $DIR/repr_align_greater_usize.rs:24:8
11+
|
12+
LL | #[repr(align(65536))]
13+
| ^^^^^^^^^^^^
14+
|
15+
= note: `isize::MAX` is 32767 for the current target
16+
17+
error: aborting due to 2 previous errors
18+
19+
For more information about this error, try `rustc --explain E0589`.

Diff for: tests/ui/repr/repr_align_greater_usize.rs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//@ revisions: msp430 aarch32
2+
//@[msp430] needs-llvm-components: msp430
3+
//@[msp430] compile-flags: --target=msp430-none-elf
4+
//@[aarch32] build-pass
5+
//@[aarch32] needs-llvm-components: arm
6+
//@[aarch32] compile-flags: --target=thumbv7m-none-eabi
7+
8+
// We should fail to compute alignment for types aligned higher than usize::MAX.
9+
// We can't handle alignments that require all 32 bits, so this only affects 16-bit.
10+
11+
#![feature(lang_items, no_core)]
12+
#![no_core]
13+
#![crate_type = "lib"]
14+
15+
#[lang = "sized"]
16+
trait Sized {}
17+
18+
#[repr(align(16384))]
19+
struct Kitten;
20+
21+
#[repr(align(32768))] //[msp430]~ ERROR alignment must not be greater than `isize::MAX`
22+
struct Cat;
23+
24+
#[repr(align(65536))] //[msp430]~ ERROR alignment must not be greater than `isize::MAX`
25+
struct BigCat;

0 commit comments

Comments
 (0)