@@ -1007,79 +1007,6 @@ impl<'tcx> FieldUniquenessCheckContext<'tcx> {
1007
1007
}
1008
1008
}
1009
1009
}
1010
-
1011
- /// Check the uniqueness of fields across adt where there are
1012
- /// nested fields imported from an unnamed field.
1013
- fn check_field_in_nested_adt ( & mut self , adt_def : ty:: AdtDef < ' _ > , unnamed_field_span : Span ) {
1014
- for field in adt_def. all_fields ( ) {
1015
- if field. is_unnamed ( ) {
1016
- // Here we don't care about the generic parameters, so `instantiate_identity` is enough.
1017
- match self . tcx . type_of ( field. did ) . instantiate_identity ( ) . kind ( ) {
1018
- ty:: Adt ( adt_def, _) => {
1019
- self . check_field_in_nested_adt ( * adt_def, unnamed_field_span) ;
1020
- }
1021
- ty_kind => span_bug ! (
1022
- self . tcx. def_span( field. did) ,
1023
- "Unexpected TyKind in FieldUniquenessCheckContext::check_field_in_nested_adt(): {ty_kind:?}"
1024
- ) ,
1025
- }
1026
- } else {
1027
- self . check_field_decl (
1028
- field. ident ( self . tcx ) ,
1029
- NestedSpan {
1030
- span : unnamed_field_span,
1031
- nested_field_span : self . tcx . def_span ( field. did ) ,
1032
- }
1033
- . into ( ) ,
1034
- ) ;
1035
- }
1036
- }
1037
- }
1038
-
1039
- /// Check the uniqueness of fields in a struct variant, and recursively
1040
- /// check the nested fields if it is an unnamed field with type of an
1041
- /// anonymous adt.
1042
- fn check_field ( & mut self , field : & hir:: FieldDef < ' _ > ) {
1043
- if field. ident . name != kw:: Underscore {
1044
- self . check_field_decl ( field. ident , field. span . into ( ) ) ;
1045
- return ;
1046
- }
1047
- match & field. ty . kind {
1048
- hir:: TyKind :: AnonAdt ( item_id) => {
1049
- match & self . tcx . hir_node ( item_id. hir_id ( ) ) . expect_item ( ) . kind {
1050
- hir:: ItemKind :: Struct ( variant_data, ..)
1051
- | hir:: ItemKind :: Union ( variant_data, ..) => {
1052
- variant_data. fields ( ) . iter ( ) . for_each ( |f| self . check_field ( f) ) ;
1053
- }
1054
- item_kind => span_bug ! (
1055
- field. ty. span,
1056
- "Unexpected ItemKind in FieldUniquenessCheckContext::check_field(): {item_kind:?}"
1057
- ) ,
1058
- }
1059
- }
1060
- hir:: TyKind :: Path ( hir:: QPath :: Resolved ( _, hir:: Path { res, .. } ) ) => {
1061
- // If this is a direct path to an ADT, we can check it
1062
- // If this is a type alias or non-ADT, `check_unnamed_fields` should verify it
1063
- if let Some ( def_id) = res. opt_def_id ( )
1064
- && let Some ( local) = def_id. as_local ( )
1065
- && let Node :: Item ( item) = self . tcx . hir_node_by_def_id ( local)
1066
- && item. is_adt ( )
1067
- {
1068
- self . check_field_in_nested_adt ( self . tcx . adt_def ( def_id) , field. span ) ;
1069
- }
1070
- }
1071
- // Abort due to errors (there must be an error if an unnamed field
1072
- // has any type kind other than an anonymous adt or a named adt)
1073
- ty_kind => {
1074
- self . tcx . dcx ( ) . span_delayed_bug (
1075
- field. ty . span ,
1076
- format ! ( "Unexpected TyKind in FieldUniquenessCheckContext::check_field(): {ty_kind:?}" ) ,
1077
- ) ;
1078
- // FIXME: errors during AST validation should abort the compilation before reaching here.
1079
- self . tcx . dcx ( ) . abort_if_errors ( ) ;
1080
- }
1081
- }
1082
- }
1083
1010
}
1084
1011
1085
1012
fn lower_variant (
@@ -1090,20 +1017,13 @@ fn lower_variant(
1090
1017
def : & hir:: VariantData < ' _ > ,
1091
1018
adt_kind : ty:: AdtKind ,
1092
1019
parent_did : LocalDefId ,
1093
- is_anonymous : bool ,
1094
1020
) -> ty:: VariantDef {
1095
- let mut has_unnamed_fields = false ;
1096
1021
let mut field_uniqueness_check_ctx = FieldUniquenessCheckContext :: new ( tcx) ;
1097
1022
let fields = def
1098
1023
. fields ( )
1099
1024
. iter ( )
1100
- . inspect ( |f| {
1101
- has_unnamed_fields |= f. ident . name == kw:: Underscore ;
1102
- // We only check named ADT here because anonymous ADTs are checked inside
1103
- // the named ADT in which they are defined.
1104
- if !is_anonymous {
1105
- field_uniqueness_check_ctx. check_field ( f) ;
1106
- }
1025
+ . inspect ( |field| {
1026
+ field_uniqueness_check_ctx. check_field_decl ( field. ident , field. span . into ( ) ) ;
1107
1027
} )
1108
1028
. map ( |f| ty:: FieldDef {
1109
1029
did : f. def_id . to_def_id ( ) ,
@@ -1127,7 +1047,6 @@ fn lower_variant(
1127
1047
adt_kind == AdtKind :: Struct && tcx. has_attr ( parent_did, sym:: non_exhaustive)
1128
1048
|| variant_did
1129
1049
. is_some_and ( |variant_did| tcx. has_attr ( variant_did, sym:: non_exhaustive) ) ,
1130
- has_unnamed_fields,
1131
1050
)
1132
1051
}
1133
1052
@@ -1138,20 +1057,7 @@ fn adt_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::AdtDef<'_> {
1138
1057
bug ! ( "expected ADT to be an item" ) ;
1139
1058
} ;
1140
1059
1141
- let is_anonymous = item. ident . name == kw:: Empty ;
1142
- let repr = if is_anonymous {
1143
- let parent = tcx. local_parent ( def_id) ;
1144
- if let Node :: Item ( item) = tcx. hir_node_by_def_id ( parent)
1145
- && item. is_struct_or_union ( )
1146
- {
1147
- tcx. adt_def ( parent) . repr ( )
1148
- } else {
1149
- tcx. dcx ( ) . span_delayed_bug ( item. span , "anonymous field inside non struct/union" ) ;
1150
- ty:: ReprOptions :: default ( )
1151
- }
1152
- } else {
1153
- tcx. repr_options_of_def ( def_id)
1154
- } ;
1060
+ let repr = tcx. repr_options_of_def ( def_id) ;
1155
1061
let ( kind, variants) = match & item. kind {
1156
1062
ItemKind :: Enum ( def, _) => {
1157
1063
let mut distance_from_explicit = 0 ;
@@ -1175,7 +1081,6 @@ fn adt_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::AdtDef<'_> {
1175
1081
& v. data ,
1176
1082
AdtKind :: Enum ,
1177
1083
def_id,
1178
- is_anonymous,
1179
1084
)
1180
1085
} )
1181
1086
. collect ( ) ;
@@ -1195,15 +1100,14 @@ fn adt_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::AdtDef<'_> {
1195
1100
def,
1196
1101
adt_kind,
1197
1102
def_id,
1198
- is_anonymous,
1199
1103
) )
1200
1104
. collect ( ) ;
1201
1105
1202
1106
( adt_kind, variants)
1203
1107
}
1204
1108
_ => bug ! ( "{:?} is not an ADT" , item. owner_id. def_id) ,
1205
1109
} ;
1206
- tcx. mk_adt_def ( def_id. to_def_id ( ) , kind, variants, repr, is_anonymous )
1110
+ tcx. mk_adt_def ( def_id. to_def_id ( ) , kind, variants, repr)
1207
1111
}
1208
1112
1209
1113
fn trait_def ( tcx : TyCtxt < ' _ > , def_id : LocalDefId ) -> ty:: TraitDef {
0 commit comments