From afed75a2d9b7e049dc26be7af3cb72297274d343 Mon Sep 17 00:00:00 2001 From: Lee Bousfield Date: Sat, 8 Jul 2017 10:28:20 -0400 Subject: [PATCH] Lower alignment limit down to 2^31 - 1 (from LLVM) --- src/librustc/ty/layout.rs | 10 ++++++---- src/libsyntax/attr.rs | 6 +++--- src/test/compile-fail/repr-align.rs | 2 +- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/librustc/ty/layout.rs b/src/librustc/ty/layout.rs index 90468d92a50ad..6fb26f0dd6dbf 100644 --- a/src/librustc/ty/layout.rs +++ b/src/librustc/ty/layout.rs @@ -285,7 +285,9 @@ impl Size { } /// Alignment of a type in bytes, both ABI-mandated and preferred. -/// Each field is a power of two. +/// Each field is a power of two, giving the alignment a maximum +/// value of 2^(2^8 - 1), which is limited by LLVM to a i32, with +/// a maximum capacity of 2^31 - 1 or 2147483647. #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)] pub struct Align { abi: u8, @@ -298,7 +300,7 @@ impl Align { } pub fn from_bytes(abi: u64, pref: u64) -> Result { - let pack = |align: u64| { + let log2 = |align: u64| { // Treat an alignment of 0 bytes like 1-byte alignment. if align == 0 { return Ok(0); @@ -318,8 +320,8 @@ impl Align { }; Ok(Align { - abi: pack(abi)?, - pref: pack(pref)?, + abi: log2(abi)?, + pref: log2(pref)?, }) } diff --git a/src/libsyntax/attr.rs b/src/libsyntax/attr.rs index 45c106f2a7f05..a247fe7f8a56a 100644 --- a/src/libsyntax/attr.rs +++ b/src/libsyntax/attr.rs @@ -974,11 +974,11 @@ pub fn find_repr_attrs(diagnostic: &Handler, attr: &Attribute) -> Vec let mut align_error = None; if let ast::LitKind::Int(align, ast::LitIntType::Unsuffixed) = value.node { if align.is_power_of_two() { - // rustc::ty::layout::Align restricts align to <= 2147483648 - if align <= 2147483648 { + // rustc::ty::layout::Align restricts align to <= 2147483647 + if align <= 2147483647 { acc.push(ReprAlign(align as u32)); } else { - align_error = Some("larger than 2147483648"); + align_error = Some("larger than 2147483647"); } } else { align_error = Some("not a power of two"); diff --git a/src/test/compile-fail/repr-align.rs b/src/test/compile-fail/repr-align.rs index 433abe943f58a..bc9cf065e5a0a 100644 --- a/src/test/compile-fail/repr-align.rs +++ b/src/test/compile-fail/repr-align.rs @@ -17,7 +17,7 @@ struct A(i32); #[repr(align(15))] //~ ERROR: invalid `repr(align)` attribute: not a power of two struct B(i32); -#[repr(align(4294967296))] //~ ERROR: invalid `repr(align)` attribute: larger than 2147483648 +#[repr(align(4294967296))] //~ ERROR: invalid `repr(align)` attribute: larger than 2147483647 struct C(i32); fn main() {}