@@ -1008,77 +1008,11 @@ impl<'tcx> FieldUniquenessCheckContext<'tcx> {
10081008 }
10091009 }
10101010
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-
10391011 /// Check the uniqueness of fields in a struct variant, and recursively
10401012 /// check the nested fields if it is an unnamed field with type of an
10411013 /// anonymous adt.
10421014 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- }
1015+ self . check_field_decl ( field. ident , field. span . into ( ) ) ;
10821016 }
10831017}
10841018
@@ -1090,20 +1024,13 @@ fn lower_variant(
10901024 def : & hir:: VariantData < ' _ > ,
10911025 adt_kind : ty:: AdtKind ,
10921026 parent_did : LocalDefId ,
1093- is_anonymous : bool ,
10941027) -> ty:: VariantDef {
1095- let mut has_unnamed_fields = false ;
10961028 let mut field_uniqueness_check_ctx = FieldUniquenessCheckContext :: new ( tcx) ;
10971029 let fields = def
10981030 . fields ( )
10991031 . iter ( )
11001032 . 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- }
1033+ field_uniqueness_check_ctx. check_field ( f) ;
11071034 } )
11081035 . map ( |f| ty:: FieldDef {
11091036 did : f. def_id . to_def_id ( ) ,
@@ -1127,7 +1054,6 @@ fn lower_variant(
11271054 adt_kind == AdtKind :: Struct && tcx. has_attr ( parent_did, sym:: non_exhaustive)
11281055 || variant_did
11291056 . is_some_and ( |variant_did| tcx. has_attr ( variant_did, sym:: non_exhaustive) ) ,
1130- has_unnamed_fields,
11311057 )
11321058}
11331059
@@ -1138,20 +1064,7 @@ fn adt_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::AdtDef<'_> {
11381064 bug ! ( "expected ADT to be an item" ) ;
11391065 } ;
11401066
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- } ;
1067+ let repr = tcx. repr_options_of_def ( def_id) ;
11551068 let ( kind, variants) = match & item. kind {
11561069 ItemKind :: Enum ( def, _) => {
11571070 let mut distance_from_explicit = 0 ;
@@ -1175,7 +1088,6 @@ fn adt_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::AdtDef<'_> {
11751088 & v. data ,
11761089 AdtKind :: Enum ,
11771090 def_id,
1178- is_anonymous,
11791091 )
11801092 } )
11811093 . collect ( ) ;
@@ -1195,15 +1107,14 @@ fn adt_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::AdtDef<'_> {
11951107 def,
11961108 adt_kind,
11971109 def_id,
1198- is_anonymous,
11991110 ) )
12001111 . collect ( ) ;
12011112
12021113 ( adt_kind, variants)
12031114 }
12041115 _ => bug ! ( "{:?} is not an ADT" , item. owner_id. def_id) ,
12051116 } ;
1206- tcx. mk_adt_def ( def_id. to_def_id ( ) , kind, variants, repr, is_anonymous )
1117+ tcx. mk_adt_def ( def_id. to_def_id ( ) , kind, variants, repr)
12071118}
12081119
12091120fn trait_def ( tcx : TyCtxt < ' _ > , def_id : LocalDefId ) -> ty:: TraitDef {
0 commit comments