@@ -282,9 +282,10 @@ impl<'tcx> HirTyLowerer<'tcx> for FnCtxt<'_, 'tcx> {
282282
283283 let mut field_indices = Vec :: with_capacity ( fields. len ( ) ) ;
284284 let mut current_container = container;
285- let mut fields = fields. into_iter ( ) ;
285+ let mut fields = fields. into_iter ( ) . peekable ( ) ;
286286
287287 while let Some ( & field) = fields. next ( ) {
288+ let last = fields. peek ( ) . is_none ( ) ;
288289 let container = self . structurally_resolve_type ( span, current_container) ;
289290
290291 match container. kind ( ) {
@@ -351,30 +352,33 @@ impl<'tcx> HirTyLowerer<'tcx> for FnCtxt<'_, 'tcx> {
351352 . emit_unless_delay ( container. references_error ( ) ) ) ;
352353 } ;
353354
354- let field_ty = self . field_ty ( span, field, args) ;
355-
356- // Enums are anyway always sized. But just to safeguard against future
357- // language extensions, let's double-check.
358- self . require_type_is_sized (
359- field_ty,
360- span,
361- ObligationCauseCode :: FieldSized {
362- adt_kind : AdtKind :: Enum ,
363- span : self . tcx . def_span ( field. did ) ,
364- last : false ,
365- } ,
366- ) ;
367-
368- if field. vis . is_accessible_from ( sub_def_scope, self . tcx ) {
369- self . tcx . check_stability ( field. did , Some ( hir_id) , span, None ) ;
370- } else {
371- self . private_field_err ( ident, container_def. did ( ) ) . emit ( ) ;
372- }
373-
374355 // Save the index of all fields regardless of their visibility in case
375356 // of error recovery.
376357 field_indices. push ( ( index, subindex) ) ;
377- current_container = field_ty;
358+
359+ if !last {
360+ let field_ty = self . field_ty ( span, field, args) ;
361+
362+ // Enums are anyway always sized. But just to safeguard against future
363+ // language extensions, let's double-check.
364+ self . require_type_is_sized (
365+ field_ty,
366+ span,
367+ ObligationCauseCode :: FieldSized {
368+ adt_kind : AdtKind :: Enum ,
369+ span : self . tcx . def_span ( field. did ) ,
370+ last : false ,
371+ } ,
372+ ) ;
373+
374+ if field. vis . is_accessible_from ( sub_def_scope, self . tcx ) {
375+ self . tcx . check_stability ( field. did , Some ( hir_id) , span, None ) ;
376+ } else {
377+ self . private_field_err ( ident, container_def. did ( ) ) . emit ( ) ;
378+ }
379+
380+ current_container = field_ty;
381+ }
378382
379383 continue ;
380384 }
@@ -388,37 +392,40 @@ impl<'tcx> HirTyLowerer<'tcx> for FnCtxt<'_, 'tcx> {
388392 . iter_enumerated ( )
389393 . find ( |( _, f) | f. ident ( self . tcx ) . normalize_to_macros_2_0 ( ) == ident)
390394 {
391- let field_ty = self . field_ty ( span, field, args) ;
395+ // Save the index of all fields regardless of their visibility in case
396+ // of error recovery.
397+ field_indices. push ( ( FIRST_VARIANT , index) ) ;
398+
399+ if !last {
400+ let field_ty = self . field_ty ( span, field, args) ;
392401
393- match field_path_kind {
394- FieldPathKind :: OffsetOf => {
395- if self . tcx . features ( ) . offset_of_slice ( ) {
396- self . require_type_has_static_alignment ( field_ty, span) ;
397- } else {
398- self . require_type_is_sized (
399- field_ty,
400- span,
401- ObligationCauseCode :: Misc ,
402- ) ;
402+ match field_path_kind {
403+ FieldPathKind :: OffsetOf => {
404+ if self . tcx . features ( ) . offset_of_slice ( ) {
405+ self . require_type_has_static_alignment ( field_ty, span) ;
406+ } else {
407+ self . require_type_is_sized (
408+ field_ty,
409+ span,
410+ ObligationCauseCode :: Misc ,
411+ ) ;
412+ }
413+ }
414+ FieldPathKind :: FieldOf => {
415+ // A field type always exists regardless of weather it is aligned or
416+ // not.
403417 }
404418 }
405- FieldPathKind :: FieldOf => {
406- // A field type always exists regardless of weather it is aligned or
407- // not.
419+
420+ if field. vis . is_accessible_from ( def_scope, self . tcx ) {
421+ self . tcx . check_stability ( field. did , Some ( hir_id) , span, None ) ;
422+ } else {
423+ self . private_field_err ( ident, container_def. did ( ) ) . emit ( ) ;
408424 }
409- }
410425
411- if field. vis . is_accessible_from ( def_scope, self . tcx ) {
412- self . tcx . check_stability ( field. did , Some ( hir_id) , span, None ) ;
413- } else {
414- self . private_field_err ( ident, container_def. did ( ) ) . emit ( ) ;
426+ current_container = field_ty;
415427 }
416428
417- // Save the index of all fields regardless of their visibility in case
418- // of error recovery.
419- field_indices. push ( ( FIRST_VARIANT , index) ) ;
420- current_container = field_ty;
421-
422429 continue ;
423430 }
424431 }
0 commit comments