@@ -3103,16 +3103,68 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
3103
3103
fields : & [ Ident ] ,
3104
3104
expr : & ' tcx hir:: Expr < ' tcx > ,
3105
3105
) -> Ty < ' tcx > {
3106
+ use rustc_target:: abi:: OffsetOfIdx :: * ;
3107
+
3106
3108
let container = self . to_ty ( container) . normalized ;
3107
3109
3108
3110
let mut field_indices = Vec :: with_capacity ( fields. len ( ) ) ;
3109
3111
let mut current_container = container;
3112
+ let mut fields = fields. into_iter ( ) ;
3110
3113
3111
- for & field in fields {
3114
+ while let Some ( & field) = fields. next ( ) {
3112
3115
let container = self . structurally_resolve_type ( expr. span , current_container) ;
3113
3116
3114
3117
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) => {
3116
3168
let block = self . tcx . hir ( ) . local_def_id_to_hir_id ( self . body_id ) ;
3117
3169
let ( ident, def_scope) =
3118
3170
self . tcx . adjust_ident_and_get_scope ( field, container_def. did ( ) , block) ;
@@ -3135,7 +3187,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
3135
3187
3136
3188
// Save the index of all fields regardless of their visibility in case
3137
3189
// of error recovery.
3138
- field_indices. push ( index) ;
3190
+ field_indices. push ( Field ( index) ) ;
3139
3191
current_container = field_ty;
3140
3192
3141
3193
continue ;
@@ -3149,7 +3201,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
3149
3201
self . require_type_is_sized ( ty, expr. span , traits:: MiscObligation ) ;
3150
3202
}
3151
3203
if let Some ( & field_ty) = tys. get ( index) {
3152
- field_indices. push ( index. into ( ) ) ;
3204
+ field_indices. push ( Field ( index. into ( ) ) ) ;
3153
3205
current_container = field_ty;
3154
3206
3155
3207
continue ;
0 commit comments