Skip to content

Commit f9d1378

Browse files
authored
Rollup merge of #70411 - ogoffart:fix-62691, r=eddyb
Fix for #62691: use the largest niche across all fields fixes #62691 (The second commit is a small optimization but it makes the code less pretty and i don't know if it is worth it.)
2 parents 20771ae + 0b00c20 commit f9d1378

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

src/librustc/ty/layout.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,6 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
282282

283283
let mut align = if pack.is_some() { dl.i8_align } else { dl.aggregate_align };
284284

285-
let mut sized = true;
286-
let mut offsets = vec![Size::ZERO; fields.len()];
287285
let mut inverse_memory_index: Vec<u32> = (0..fields.len() as u32).collect();
288286

289287
let mut optimize = !repr.inhibit_struct_field_reordering_opt();
@@ -320,6 +318,8 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
320318
// At the bottom of this function, we invert `inverse_memory_index` to
321319
// produce `memory_index` (see `invert_mapping`).
322320

321+
let mut sized = true;
322+
let mut offsets = vec![Size::ZERO; fields.len()];
323323
let mut offset = Size::ZERO;
324324
let mut largest_niche = None;
325325
let mut largest_niche_available = 0;
@@ -900,18 +900,19 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
900900
let count = (niche_variants.end().as_u32()
901901
- niche_variants.start().as_u32()
902902
+ 1) as u128;
903-
// FIXME(#62691) use the largest niche across all fields,
904-
// not just the first one.
905-
for (field_index, &field) in variants[i].iter().enumerate() {
906-
let niche = match &field.largest_niche {
907-
Some(niche) => niche,
908-
_ => continue,
909-
};
910-
let (niche_start, niche_scalar) = match niche.reserve(self, count) {
911-
Some(pair) => pair,
912-
None => continue,
913-
};
914903

904+
// Find the field with the largest niche
905+
let niche_candidate = variants[i]
906+
.iter()
907+
.enumerate()
908+
.filter_map(|(j, &field)| Some((j, field.largest_niche.as_ref()?)))
909+
.max_by_key(|(_, niche)| niche.available(dl));
910+
911+
if let Some((field_index, niche, (niche_start, niche_scalar))) =
912+
niche_candidate.and_then(|(field_index, niche)| {
913+
Some((field_index, niche, niche.reserve(self, count)?))
914+
})
915+
{
915916
let mut align = dl.aggregate_align;
916917
let st = variants
917918
.iter_enumerated()

src/test/ui/type-sizes.rs

+7
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ enum NicheFilledEnumWithAbsentVariant {
7474
C,
7575
}
7676

77+
enum Option2<A, B> {
78+
Some(A, B),
79+
None
80+
}
81+
7782
pub fn main() {
7883
assert_eq!(size_of::<u8>(), 1 as usize);
7984
assert_eq!(size_of::<u32>(), 4 as usize);
@@ -113,4 +118,6 @@ pub fn main() {
113118

114119
assert_eq!(size_of::<Option<Option<(bool, &())>>>(), size_of::<(bool, &())>());
115120
assert_eq!(size_of::<Option<Option<(&(), bool)>>>(), size_of::<(bool, &())>());
121+
assert_eq!(size_of::<Option<Option2<bool, &()>>>(), size_of::<(bool, &())>());
122+
assert_eq!(size_of::<Option<Option2<&(), bool>>>(), size_of::<(bool, &())>());
116123
}

0 commit comments

Comments
 (0)