Skip to content

Commit 059b19a

Browse files
committed
Fix the re-ordering of the field to put the niche first
1 parent 23d815c commit 059b19a

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/librustc_middle/ty/layout.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,8 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
330330
optimizing.sort_by_key(|&x| field_align(&fields[x as usize]));
331331
}
332332
}
333-
// Rotate index array to put the largest niche first.
334-
// Since it is already the first amongst the types with the same alignement,
333+
// Rotate index array to put the largest niche first. Then reverse the ones with larger
334+
// alignment. Since it is already the first amongst the types with the same alignment,
335335
// this will just move some of the potential padding within the structure.
336336
if let (Some(niche_index), StructKind::AlwaysSized) = (largest_niche_index, kind) {
337337
// ZSTs are always first, and the largest niche is not one, so we can unwrap
@@ -342,6 +342,9 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
342342
let non_zsts = &mut inverse_memory_index[first_non_zst..];
343343
let pivot = non_zsts.iter().position(|&x| x == niche_index).unwrap();
344344
non_zsts.rotate_left(pivot);
345+
let pivot = non_zsts.len() - pivot;
346+
non_zsts[pivot..].reverse();
347+
debug_assert_eq![non_zsts[0], niche_index];
345348
}
346349
}
347350

src/test/ui/type-sizes.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,8 @@ pub fn main() {
145145
assert_eq!(size_of::<Option<Option<(&(), bool)>>>(), size_of::<(bool, &())>());
146146
assert_eq!(size_of::<Option<Option2<bool, &()>>>(), size_of::<(bool, &())>());
147147
assert_eq!(size_of::<Option<Option2<&(), bool>>>(), size_of::<(bool, &())>());
148+
149+
struct S1{ a: u16, b: std::num::NonZeroU16, c: u16, d: u8, e: u32, f: u64, g:[u8;2] }
150+
assert_eq!(size_of::<S1>(), 24);
151+
assert_eq!(size_of::<Option<S1>>(), 24);
148152
}

0 commit comments

Comments
 (0)