Skip to content

Commit 92a70e2

Browse files
committed
Auto merge of #125360 - RalfJung:packed-field-reorder, r=fmease
don't inhibit random field reordering on repr(packed(1)) `inhibit_struct_field_reordering_opt` being false means we exclude this type from random field shuffling. However, `packed(1)` types can still be shuffled! The logic was added in rust-lang/rust#48528 since it's pointless to reorder fields in packed(1) types (there's no padding that could be saved) -- but that shouldn't inhibit `-Zrandomize-layout` (which did not exist at the time). We could add an optimization elsewhere to not bother sorting the fields for `repr(packed)` types, but I don't think that's worth the effort. This *does* change the behavior in that we may now reorder fields of `packed(1)` structs (e.g. if there are niches, we'll try to move them to the start/end, according to `NicheBias`). We were always allowed to do that but so far we didn't. Quoting the [reference](https://doc.rust-lang.org/reference/type-layout.html): > On their own, align and packed do not provide guarantees about the order of fields in the layout of a struct or the layout of an enum variant, although they may be combined with representations (such as C) which do provide such guarantees.
2 parents f44d647 + 1d4e378 commit 92a70e2

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

tests/fail/reading_half_a_pointer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![allow(dead_code)]
22

33
// We use packed structs to get around alignment restrictions
4-
#[repr(packed)]
4+
#[repr(C, packed)]
55
struct Data {
66
pad: u8,
77
ptr: &'static i32,

tests/fail/unaligned_pointers/field_requires_parent_struct_alignment2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub struct Aligned {
77
_pad: [u8; 11],
88
packed: Packed,
99
}
10-
#[repr(packed)]
10+
#[repr(C, packed)]
1111
#[derive(Default, Copy, Clone)]
1212
pub struct Packed {
1313
_pad: [u8; 5],

0 commit comments

Comments
 (0)