Skip to content

Commit d49221f

Browse files
authored
Rollup merge of rust-lang#70748 - ogoffart:enum-layout-optim2, r=eddyb
Do not disable field reordering on enums with big discriminant The field are always re-ordered to minimize padding, regardless of the alignment of the discriminant (spinoff from rust-lang#70477)
2 parents 54bbe07 + 6b6cb7b commit d49221f

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

src/librustc_middle/ty/layout.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
285285

286286
let mut inverse_memory_index: Vec<u32> = (0..fields.len() as u32).collect();
287287

288-
let mut optimize = !repr.inhibit_struct_field_reordering_opt();
289-
if let StructKind::Prefixed(_, align) = kind {
290-
optimize &= align.bytes() == 1;
291-
}
292-
288+
let optimize = !repr.inhibit_struct_field_reordering_opt();
293289
if optimize {
294290
let end =
295291
if let StructKind::MaybeUnsized = kind { fields.len() - 1 } else { fields.len() };
@@ -307,6 +303,8 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
307303
});
308304
}
309305
StructKind::Prefixed(..) => {
306+
// Sort in ascending alignment so that the layout stay optimal
307+
// regardless of the prefix
310308
optimizing.sort_by_key(|&x| field_align(&fields[x as usize]));
311309
}
312310
}

src/test/ui/type-sizes.rs

+25
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,29 @@ enum ReorderedEnum {
3737
B(u8, u16, u8),
3838
}
3939

40+
enum ReorderedEnum2 {
41+
A(u8, u32, u8),
42+
B(u16, u8, u16, u8),
43+
44+
// 0x100 niche variants.
45+
_00, _01, _02, _03, _04, _05, _06, _07, _08, _09, _0A, _0B, _0C, _0D, _0E, _0F,
46+
_10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _1A, _1B, _1C, _1D, _1E, _1F,
47+
_20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _2A, _2B, _2C, _2D, _2E, _2F,
48+
_30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _3A, _3B, _3C, _3D, _3E, _3F,
49+
_40, _41, _42, _43, _44, _45, _46, _47, _48, _49, _4A, _4B, _4C, _4D, _4E, _4F,
50+
_50, _51, _52, _53, _54, _55, _56, _57, _58, _59, _5A, _5B, _5C, _5D, _5E, _5F,
51+
_60, _61, _62, _63, _64, _65, _66, _67, _68, _69, _6A, _6B, _6C, _6D, _6E, _6F,
52+
_70, _71, _72, _73, _74, _75, _76, _77, _78, _79, _7A, _7B, _7C, _7D, _7E, _7F,
53+
_80, _81, _82, _83, _84, _85, _86, _87, _88, _89, _8A, _8B, _8C, _8D, _8E, _8F,
54+
_90, _91, _92, _93, _94, _95, _96, _97, _98, _99, _9A, _9B, _9C, _9D, _9E, _9F,
55+
_A0, _A1, _A2, _A3, _A4, _A5, _A6, _A7, _A8, _A9, _AA, _AB, _AC, _AD, _AE, _AF,
56+
_B0, _B1, _B2, _B3, _B4, _B5, _B6, _B7, _B8, _B9, _BA, _BB, _BC, _BD, _BE, _BF,
57+
_C0, _C1, _C2, _C3, _C4, _C5, _C6, _C7, _C8, _C9, _CA, _CB, _CC, _CD, _CE, _CF,
58+
_D0, _D1, _D2, _D3, _D4, _D5, _D6, _D7, _D8, _D9, _DA, _DB, _DC, _DD, _DE, _DF,
59+
_E0, _E1, _E2, _E3, _E4, _E5, _E6, _E7, _E8, _E9, _EA, _EB, _EC, _ED, _EE, _EF,
60+
_F0, _F1, _F2, _F3, _F4, _F5, _F6, _F7, _F8, _F9, _FA, _FB, _FC, _FD, _FE, _FF,
61+
}
62+
4063
enum EnumEmpty {}
4164

4265
enum EnumSingle1 {
@@ -104,6 +127,8 @@ pub fn main() {
104127
assert_eq!(size_of::<e3>(), 4 as usize);
105128
assert_eq!(size_of::<ReorderedStruct>(), 4);
106129
assert_eq!(size_of::<ReorderedEnum>(), 6);
130+
assert_eq!(size_of::<ReorderedEnum2>(), 8);
131+
107132

108133
assert_eq!(size_of::<EnumEmpty>(), 0);
109134
assert_eq!(size_of::<EnumSingle1>(), 0);

0 commit comments

Comments
 (0)