@@ -3103,16 +3103,68 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
31033103 fields : & [ Ident ] ,
31043104 expr : & ' tcx hir:: Expr < ' tcx > ,
31053105 ) -> Ty < ' tcx > {
3106+ use rustc_target:: abi:: OffsetOfIdx :: * ;
3107+
31063108 let container = self . to_ty ( container) . normalized ;
31073109
31083110 let mut field_indices = Vec :: with_capacity ( fields. len ( ) ) ;
31093111 let mut current_container = container;
3112+ let mut fields = fields. into_iter ( ) ;
31103113
3111- for & field in fields {
3114+ while let Some ( & field) = fields. next ( ) {
31123115 let container = self . structurally_resolve_type ( expr. span , current_container) ;
31133116
31143117 match container. kind ( ) {
3115- ty:: Adt ( container_def, args) if !container_def. is_enum ( ) => {
3118+ ty:: Adt ( container_def, args) if container_def. is_enum ( ) => {
3119+ let block = self . tcx . hir ( ) . local_def_id_to_hir_id ( self . body_id ) ;
3120+ let ( ident, _def_scope) =
3121+ self . tcx . adjust_ident_and_get_scope ( field, container_def. did ( ) , block) ;
3122+
3123+ if let Some ( ( index, variant) ) = container_def. variants ( )
3124+ . iter_enumerated ( )
3125+ . find ( |( _, v) | v. ident ( self . tcx ) . normalize_to_macros_2_0 ( ) == ident)
3126+ {
3127+ let Some ( & subfield) = fields. next ( ) else {
3128+ let mut err = type_error_struct ! (
3129+ self . tcx( ) . sess,
3130+ ident. span,
3131+ container,
3132+ E0795 ,
3133+ "`{ident}` is an enum variant; expected field at end of `offset_of`" ,
3134+ ) ;
3135+ err. span_label ( field. span , "enum variant" ) ;
3136+ err. emit ( ) ;
3137+ break ;
3138+ } ;
3139+ let ( subident, sub_def_scope) =
3140+ self . tcx . adjust_ident_and_get_scope ( subfield, variant. def_id , block) ;
3141+
3142+ if let Some ( ( subindex, field) ) = variant. fields
3143+ . iter_enumerated ( )
3144+ . find ( |( _, f) | f. ident ( self . tcx ) . normalize_to_macros_2_0 ( ) == subident)
3145+ {
3146+ let field_ty = self . field_ty ( expr. span , field, args) ;
3147+
3148+ // FIXME: DSTs with static alignment should be allowed
3149+ self . require_type_is_sized ( field_ty, expr. span , traits:: MiscObligation ) ;
3150+
3151+ if field. vis . is_accessible_from ( sub_def_scope, self . tcx ) {
3152+ self . tcx . check_stability ( field. did , Some ( expr. hir_id ) , expr. span , None ) ;
3153+ } else {
3154+ self . private_field_err ( ident, container_def. did ( ) ) . emit ( ) ;
3155+ }
3156+
3157+ // Save the index of all fields regardless of their visibility in case
3158+ // of error recovery.
3159+ field_indices. push ( Variant ( index) ) ;
3160+ field_indices. push ( Field ( subindex) ) ;
3161+ current_container = field_ty;
3162+
3163+ continue ;
3164+ }
3165+ }
3166+ }
3167+ ty:: Adt ( container_def, args) => {
31163168 let block = self . tcx . hir ( ) . local_def_id_to_hir_id ( self . body_id ) ;
31173169 let ( ident, def_scope) =
31183170 self . tcx . adjust_ident_and_get_scope ( field, container_def. did ( ) , block) ;
@@ -3135,7 +3187,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
31353187
31363188 // Save the index of all fields regardless of their visibility in case
31373189 // of error recovery.
3138- field_indices. push ( index) ;
3190+ field_indices. push ( Field ( index) ) ;
31393191 current_container = field_ty;
31403192
31413193 continue ;
@@ -3149,7 +3201,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
31493201 self . require_type_is_sized ( ty, expr. span , traits:: MiscObligation ) ;
31503202 }
31513203 if let Some ( & field_ty) = tys. get ( index) {
3152- field_indices. push ( index. into ( ) ) ;
3204+ field_indices. push ( Field ( index. into ( ) ) ) ;
31533205 current_container = field_ty;
31543206
31553207 continue ;
0 commit comments