Skip to content

Commit

Permalink
Lower alignment limit down to 2^31 - 1 (from LLVM)
Browse files Browse the repository at this point in the history
  • Loading branch information
PlasmaPower committed Jul 8, 2017
1 parent a10213f commit afed75a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
10 changes: 6 additions & 4 deletions src/librustc/ty/layout.rs
Expand Up @@ -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,
Expand All @@ -298,7 +300,7 @@ impl Align {
}

pub fn from_bytes(abi: u64, pref: u64) -> Result<Align, String> {
let pack = |align: u64| {
let log2 = |align: u64| {
// Treat an alignment of 0 bytes like 1-byte alignment.
if align == 0 {
return Ok(0);
Expand All @@ -318,8 +320,8 @@ impl Align {
};

Ok(Align {
abi: pack(abi)?,
pref: pack(pref)?,
abi: log2(abi)?,
pref: log2(pref)?,
})
}

Expand Down
6 changes: 3 additions & 3 deletions src/libsyntax/attr.rs
Expand Up @@ -974,11 +974,11 @@ pub fn find_repr_attrs(diagnostic: &Handler, attr: &Attribute) -> Vec<ReprAttr>
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");
Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/repr-align.rs
Expand Up @@ -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() {}

0 comments on commit afed75a

Please sign in to comment.