Skip to content

Commit aead2c5

Browse files
committed
error on alignments greater than isize::MAX
1 parent 3678036 commit aead2c5

File tree

5 files changed

+86
-1
lines changed

5 files changed

+86
-1
lines changed

compiler/rustc_passes/messages.ftl

+4
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,10 @@ passes_remove_fields =
601601
passes_repr_align_function =
602602
`repr(align)` attributes on functions are unstable
603603
604+
passes_repr_align_greater_than_target_max =
605+
alignment must not be greater than `isize::MAX` bytes
606+
.note = `isize::MAX` is {$size} for the current target
607+
604608
passes_repr_conflicting =
605609
conflicting representation hints
606610

compiler/rustc_passes/src/check_attr.rs

+28-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};
@@ -1783,7 +1784,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
17831784
| Target::Union
17841785
| Target::Enum
17851786
| Target::Fn
1786-
| Target::Method(_) => continue,
1787+
| Target::Method(_) => {}
17871788
_ => {
17881789
self.dcx().emit_err(
17891790
errors::AttrApplication::StructEnumFunctionMethodUnion {
@@ -1793,6 +1794,32 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
17931794
);
17941795
}
17951796
}
1797+
1798+
// UNWRAP: parsing the attribute already ensured that it had a name and integer value
1799+
// that is a power of 2 and less than 2^29
1800+
1801+
// if the attribute is malformed, it may return None from this
1802+
// but an error will have already been emitted, so this code should just skip such attributes
1803+
1804+
if let Some((
1805+
_,
1806+
MetaItemLit {
1807+
kind: ast::LitKind::Int(literal, ast::LitIntType::Unsuffixed),
1808+
..
1809+
},
1810+
)) = hint.singleton_lit_list()
1811+
{
1812+
{
1813+
let max = Size::from_bits(self.tcx.sess.target.pointer_width)
1814+
.signed_int_max() as u64;
1815+
if literal.get() as u64 > max {
1816+
self.dcx().emit_err(errors::InvalidReprAlignForTarget {
1817+
span: hint.span(),
1818+
size: max,
1819+
});
1820+
}
1821+
}
1822+
}
17961823
}
17971824
sym::packed => {
17981825
if target != Target::Struct && target != Target::Union {

compiler/rustc_passes/src/errors.rs

+9
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,15 @@ pub(crate) struct ReprConflicting {
532532
pub hint_spans: Vec<Span>,
533533
}
534534

535+
#[derive(Diagnostic)]
536+
#[diag(passes_repr_align_greater_than_target_max, code = E0589)]
537+
#[note]
538+
pub(crate) struct InvalidReprAlignForTarget {
539+
#[primary_span]
540+
pub span: Span,
541+
pub size: u64,
542+
}
543+
535544
#[derive(LintDiagnostic)]
536545
#[diag(passes_repr_conflicting, code = E0566)]
537546
pub(crate) struct ReprConflictingLint;
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:22: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:25: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`.
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//@ revisions: msp430 aarch32
2+
//@[msp430] check-fail
3+
//@[msp430] needs-llvm-components: msp430
4+
//@[msp430] compile-flags: --target=msp430-none-elf
5+
//@[aarch32] build-pass
6+
//@[aarch32] needs-llvm-components: arm
7+
//@[aarch32] compile-flags: --target=thumbv7m-none-eabi
8+
9+
// We should fail to compute alignment for types aligned higher than usize::MAX.
10+
// We can't handle alignments that require all 32 bits, so this only affects 16-bit.
11+
12+
#![feature(lang_items, no_core)]
13+
#![no_core]
14+
#![crate_type = "lib"]
15+
16+
#[lang = "sized"]
17+
trait Sized {}
18+
19+
#[repr(align(16384))]
20+
struct Kitten;
21+
22+
#[repr(align(32768))] //[msp430]~ ERROR alignment must not be greater than `isize::MAX`
23+
struct Cat;
24+
25+
#[repr(align(65536))] //[msp430]~ ERROR alignment must not be greater than `isize::MAX`
26+
struct BigCat;

0 commit comments

Comments
 (0)