Skip to content

Commit c09c74e

Browse files
committed
Always force repr(align) attributes for stuff with alignment >= 16
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
1 parent 25eafd7 commit c09c74e

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

src/codegen/struct_layout.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -287,18 +287,23 @@ impl<'a> StructLayoutTracker<'a> {
287287
}
288288

289289
pub fn requires_explicit_align(&self, layout: Layout) -> bool {
290+
let repr_align = self.ctx.options().rust_features().repr_align;
291+
292+
// Always force explicit repr(align) for stuff more than 16-byte aligned
293+
// to work-around https://github.com/rust-lang/rust/issues/54341.
294+
//
295+
// Worst-case this just generates redundant alignment attributes.
296+
if repr_align && self.max_field_align >= 16 {
297+
return true;
298+
}
299+
290300
if self.max_field_align >= layout.align {
291301
return false;
292302
}
293-
// At this point we require explicit alignment, but we may not be able
294-
// to generate the right bits, let's double check.
295-
if self.ctx.options().rust_features().repr_align {
296-
return true;
297-
}
298303

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

304309
fn padding_bytes(&self, layout: Layout) -> usize {

tests/expectations/tests/i128.rs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
)]
99

1010
#[repr(C)]
11+
#[repr(align(16))]
1112
#[derive(Debug, Default, Copy, Clone)]
1213
pub struct foo {
1314
pub my_signed: i128,

tests/expectations/tests/long_double.rs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
)]
99

1010
#[repr(C)]
11+
#[repr(align(16))]
1112
#[derive(Debug, Default, Copy, Clone)]
1213
pub struct foo {
1314
pub bar: u128,

0 commit comments

Comments
 (0)