Skip to content

Commit a53f3ed

Browse files
committed
don't inhibit random field reordering on repr(packed(1))
1 parent e875391 commit a53f3ed

File tree

3 files changed

+6
-13
lines changed

3 files changed

+6
-13
lines changed

compiler/rustc_abi/src/layout.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -970,7 +970,7 @@ fn univariant<
970970
let mut align = if pack.is_some() { dl.i8_align } else { dl.aggregate_align };
971971
let mut max_repr_align = repr.align;
972972
let mut inverse_memory_index: IndexVec<u32, FieldIdx> = fields.indices().collect();
973-
let optimize = !repr.inhibit_struct_field_reordering_opt();
973+
let optimize = !repr.inhibit_struct_field_reordering();
974974
if optimize && fields.len() > 1 {
975975
let end = if let StructKind::MaybeUnsized = kind { fields.len() - 1 } else { fields.len() };
976976
let optimizing = &mut inverse_memory_index.raw[..end];

compiler/rustc_abi/src/lib.rs

+4-11
Original file line numberDiff line numberDiff line change
@@ -137,23 +137,16 @@ impl ReprOptions {
137137
self.c() || self.int.is_some()
138138
}
139139

140-
/// Returns `true` if this `#[repr()]` should inhibit struct field reordering
141-
/// optimizations, such as with `repr(C)`, `repr(packed(1))`, or `repr(<int>)`.
142-
pub fn inhibit_struct_field_reordering_opt(&self) -> bool {
143-
if let Some(pack) = self.pack {
144-
if pack.bytes() == 1 {
145-
return true;
146-
}
147-
}
148-
140+
/// Returns `true` if this `#[repr()]` guarantees a fixed field order,
141+
/// e.g. `repr(C)` or `repr(<int>)`.
142+
pub fn inhibit_struct_field_reordering(&self) -> bool {
149143
self.flags.intersects(ReprFlags::IS_UNOPTIMISABLE) || self.int.is_some()
150144
}
151145

152146
/// Returns `true` if this type is valid for reordering and `-Z randomize-layout`
153147
/// was enabled for its declaration crate.
154148
pub fn can_randomize_type_layout(&self) -> bool {
155-
!self.inhibit_struct_field_reordering_opt()
156-
&& self.flags.contains(ReprFlags::RANDOMIZE_LAYOUT)
149+
!self.inhibit_struct_field_reordering() && self.flags.contains(ReprFlags::RANDOMIZE_LAYOUT)
157150
}
158151

159152
/// Returns `true` if this `#[repr()]` should inhibit union ABI optimisations.

src/tools/clippy/clippy_lints/src/transmute/transmute_undefined_repr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ fn reduce_ty<'tcx>(cx: &LateContext<'tcx>, mut ty: Ty<'tcx>) -> ReducedTy<'tcx>
278278
ty = sized_ty;
279279
continue;
280280
}
281-
if def.repr().inhibit_struct_field_reordering_opt() {
281+
if def.repr().inhibit_struct_field_reordering() {
282282
ReducedTy::OrderedFields(Some(sized_ty))
283283
} else {
284284
ReducedTy::UnorderedFields(ty)

0 commit comments

Comments
 (0)