@@ -282,8 +282,6 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
282
282
283
283
let mut align = if pack. is_some ( ) { dl. i8_align } else { dl. aggregate_align } ;
284
284
285
- let mut sized = true ;
286
- let mut offsets = vec ! [ Size :: ZERO ; fields. len( ) ] ;
287
285
let mut inverse_memory_index: Vec < u32 > = ( 0 ..fields. len ( ) as u32 ) . collect ( ) ;
288
286
289
287
let mut optimize = !repr. inhibit_struct_field_reordering_opt ( ) ;
@@ -320,6 +318,8 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
320
318
// At the bottom of this function, we invert `inverse_memory_index` to
321
319
// produce `memory_index` (see `invert_mapping`).
322
320
321
+ let mut sized = true ;
322
+ let mut offsets = vec ! [ Size :: ZERO ; fields. len( ) ] ;
323
323
let mut offset = Size :: ZERO ;
324
324
let mut largest_niche = None ;
325
325
let mut largest_niche_available = 0 ;
@@ -900,18 +900,19 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
900
900
let count = ( niche_variants. end ( ) . as_u32 ( )
901
901
- niche_variants. start ( ) . as_u32 ( )
902
902
+ 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
- } ;
914
903
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
+ {
915
916
let mut align = dl. aggregate_align ;
916
917
let st = variants
917
918
. iter_enumerated ( )
0 commit comments