Skip to content

Commit

Permalink
Always force repr(align) attributes for stuff with alignment >= 16
Browse files Browse the repository at this point in the history
To work-around some cases of rust-lang/rust#54341.

Other cases where u128 and u64 are mixed in fields might not behave correctly,
but the field offset assertions would catch them.

Fixes rust-lang#1370
  • Loading branch information
emilio committed Sep 19, 2018
1 parent 25eafd7 commit c09c74e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/codegen/struct_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,18 +287,23 @@ impl<'a> StructLayoutTracker<'a> {
}

pub fn requires_explicit_align(&self, layout: Layout) -> bool {
let repr_align = self.ctx.options().rust_features().repr_align;

// Always force explicit repr(align) for stuff more than 16-byte aligned
// to work-around https://github.com/rust-lang/rust/issues/54341.
//
// Worst-case this just generates redundant alignment attributes.
if repr_align && self.max_field_align >= 16 {
return true;
}

if self.max_field_align >= layout.align {
return false;
}
// At this point we require explicit alignment, but we may not be able
// to generate the right bits, let's double check.
if self.ctx.options().rust_features().repr_align {
return true;
}

// We can only generate up-to a word of alignment unless we support
// repr(align).
layout.align <= self.ctx.target_pointer_size()
repr_align || layout.align <= self.ctx.target_pointer_size()
}

fn padding_bytes(&self, layout: Layout) -> usize {
Expand Down
1 change: 1 addition & 0 deletions tests/expectations/tests/i128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
)]

#[repr(C)]
#[repr(align(16))]
#[derive(Debug, Default, Copy, Clone)]
pub struct foo {
pub my_signed: i128,
Expand Down
1 change: 1 addition & 0 deletions tests/expectations/tests/long_double.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
)]

#[repr(C)]
#[repr(align(16))]
#[derive(Debug, Default, Copy, Clone)]
pub struct foo {
pub bar: u128,
Expand Down

0 comments on commit c09c74e

Please sign in to comment.