@@ -1007,79 +1007,6 @@ impl<'tcx> FieldUniquenessCheckContext<'tcx> {
10071007 }
10081008 }
10091009 }
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- }
10831010}
10841011
10851012fn lower_variant (
@@ -1090,20 +1017,13 @@ fn lower_variant(
10901017 def : & hir:: VariantData < ' _ > ,
10911018 adt_kind : ty:: AdtKind ,
10921019 parent_did : LocalDefId ,
1093- is_anonymous : bool ,
10941020) -> ty:: VariantDef {
1095- let mut has_unnamed_fields = false ;
10961021 let mut field_uniqueness_check_ctx = FieldUniquenessCheckContext :: new ( tcx) ;
10971022 let fields = def
10981023 . fields ( )
10991024 . 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 ( ) ) ;
11071027 } )
11081028 . map ( |f| ty:: FieldDef {
11091029 did : f. def_id . to_def_id ( ) ,
@@ -1127,7 +1047,6 @@ fn lower_variant(
11271047 adt_kind == AdtKind :: Struct && tcx. has_attr ( parent_did, sym:: non_exhaustive)
11281048 || variant_did
11291049 . is_some_and ( |variant_did| tcx. has_attr ( variant_did, sym:: non_exhaustive) ) ,
1130- has_unnamed_fields,
11311050 )
11321051}
11331052
@@ -1138,20 +1057,7 @@ fn adt_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::AdtDef<'_> {
11381057 bug ! ( "expected ADT to be an item" ) ;
11391058 } ;
11401059
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) ;
11551061 let ( kind, variants) = match & item. kind {
11561062 ItemKind :: Enum ( def, _) => {
11571063 let mut distance_from_explicit = 0 ;
@@ -1175,7 +1081,6 @@ fn adt_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::AdtDef<'_> {
11751081 & v. data ,
11761082 AdtKind :: Enum ,
11771083 def_id,
1178- is_anonymous,
11791084 )
11801085 } )
11811086 . collect ( ) ;
@@ -1195,15 +1100,14 @@ fn adt_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::AdtDef<'_> {
11951100 def,
11961101 adt_kind,
11971102 def_id,
1198- is_anonymous,
11991103 ) )
12001104 . collect ( ) ;
12011105
12021106 ( adt_kind, variants)
12031107 }
12041108 _ => bug ! ( "{:?} is not an ADT" , item. owner_id. def_id) ,
12051109 } ;
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)
12071111}
12081112
12091113fn trait_def ( tcx : TyCtxt < ' _ > , def_id : LocalDefId ) -> ty:: TraitDef {
0 commit comments