@@ -19,9 +19,8 @@ use rustc_middle::query::Providers;
1919use rustc_middle:: traits:: solve:: NoSolution ;
2020use rustc_middle:: ty:: trait_def:: TraitSpecializationKind ;
2121use rustc_middle:: ty:: {
22- self , AdtKind , GenericArgKind , GenericArgs , GenericParamDefKind , Ty , TyCtxt , TypeFlags ,
23- TypeFoldable , TypeSuperVisitable , TypeVisitable , TypeVisitableExt , TypeVisitor , TypingMode ,
24- Upcast ,
22+ self , GenericArgKind , GenericArgs , GenericParamDefKind , Ty , TyCtxt , TypeFlags , TypeFoldable ,
23+ TypeSuperVisitable , TypeVisitable , TypeVisitableExt , TypeVisitor , TypingMode , Upcast ,
2524} ;
2625use rustc_middle:: { bug, span_bug} ;
2726use rustc_session:: parse:: feature_err;
@@ -289,12 +288,8 @@ pub(super) fn check_item<'tcx>(
289288 }
290289 hir:: ItemKind :: Fn { sig, .. } => check_item_fn ( tcx, def_id, sig. decl ) ,
291290 hir:: ItemKind :: Const ( _, _, ty, _) => check_const_item ( tcx, def_id, ty. span ) ,
292- hir:: ItemKind :: Struct ( ..) => check_type_defn ( tcx, item, false ) ,
293- hir:: ItemKind :: Union ( ..) => check_type_defn ( tcx, item, true ) ,
294- hir:: ItemKind :: Enum ( ..) => check_type_defn ( tcx, item, true ) ,
295- hir:: ItemKind :: Trait ( ..) => check_trait ( tcx, item) ,
296- hir:: ItemKind :: TraitAlias ( ..) => check_trait ( tcx, item) ,
297- _ => Ok ( ( ) ) ,
291+ // Note: do not add new entries to this match. Instead add all new logic in `check_item_type`
292+ _ => span_bug ! ( item. span, "should have been handled by the type based wf check: {item:?}" ) ,
298293 }
299294}
300295
@@ -344,7 +339,7 @@ pub(crate) fn check_trait_item<'tcx>(
344339/// fn into_iter<'a>(&'a self) -> Self::Iter<'a>;
345340/// }
346341/// ```
347- fn check_gat_where_clauses ( tcx : TyCtxt < ' _ > , trait_def_id : LocalDefId ) {
342+ pub ( crate ) fn check_gat_where_clauses ( tcx : TyCtxt < ' _ > , trait_def_id : LocalDefId ) {
348343 // Associates every GAT's def_id to a list of possibly missing bounds detected by this lint.
349344 let mut required_bounds_by_item = FxIndexMap :: default ( ) ;
350345 let associated_items = tcx. associated_items ( trait_def_id) ;
@@ -990,15 +985,15 @@ pub(crate) fn check_associated_item(
990985}
991986
992987/// In a type definition, we check that to ensure that the types of the fields are well-formed.
993- fn check_type_defn < ' tcx > (
988+ pub ( crate ) fn check_type_defn < ' tcx > (
994989 tcx : TyCtxt < ' tcx > ,
995- item : & hir :: Item < ' tcx > ,
990+ item : LocalDefId ,
996991 all_sized : bool ,
997992) -> Result < ( ) , ErrorGuaranteed > {
998- let _ = tcx. representability ( item. owner_id . def_id ) ;
999- let adt_def = tcx. adt_def ( item. owner_id ) ;
993+ let _ = tcx. representability ( item) ;
994+ let adt_def = tcx. adt_def ( item) ;
1000995
1001- enter_wf_checking_ctxt ( tcx, item. owner_id . def_id , |wfcx| {
996+ enter_wf_checking_ctxt ( tcx, item, |wfcx| {
1002997 let variants = adt_def. variants ( ) ;
1003998 let packed = adt_def. repr ( ) . packed ( ) ;
1004999
@@ -1056,35 +1051,21 @@ fn check_type_defn<'tcx>(
10561051 variant. fields . raw [ ..variant. fields . len ( ) - unsized_len] . iter ( ) . enumerate ( )
10571052 {
10581053 let last = idx == variant. fields . len ( ) - 1 ;
1059- let field_id = field. did . expect_local ( ) ;
1060- let hir:: FieldDef { ty : hir_ty, .. } =
1061- tcx. hir_node_by_def_id ( field_id) . expect_field ( ) ;
1062- let ty = wfcx. normalize (
1063- hir_ty. span ,
1064- None ,
1065- tcx. type_of ( field. did ) . instantiate_identity ( ) ,
1066- ) ;
1054+ let span = tcx. def_span ( field. did ) ;
1055+ let ty = wfcx. normalize ( span, None , tcx. type_of ( field. did ) . instantiate_identity ( ) ) ;
10671056 wfcx. register_bound (
10681057 traits:: ObligationCause :: new (
1069- hir_ty . span ,
1058+ span,
10701059 wfcx. body_def_id ,
10711060 ObligationCauseCode :: FieldSized {
1072- adt_kind : match & item. kind {
1073- ItemKind :: Struct ( ..) => AdtKind :: Struct ,
1074- ItemKind :: Union ( ..) => AdtKind :: Union ,
1075- ItemKind :: Enum ( ..) => AdtKind :: Enum ,
1076- kind => span_bug ! (
1077- item. span,
1078- "should be wfchecking an ADT, got {kind:?}"
1079- ) ,
1080- } ,
1081- span : hir_ty. span ,
1061+ adt_kind : adt_def. adt_kind ( ) ,
1062+ field : field. did ,
10821063 last,
10831064 } ,
10841065 ) ,
10851066 wfcx. param_env ,
10861067 ty,
1087- tcx. require_lang_item ( LangItem :: Sized , hir_ty . span ) ,
1068+ tcx. require_lang_item ( LangItem :: Sized , span) ,
10881069 ) ;
10891070 }
10901071
@@ -1100,16 +1081,13 @@ fn check_type_defn<'tcx>(
11001081 }
11011082 }
11021083
1103- check_where_clauses ( wfcx, item. owner_id . def_id ) ;
1084+ check_where_clauses ( wfcx, item) ;
11041085 Ok ( ( ) )
11051086 } )
11061087}
11071088
1108- #[ instrument( skip( tcx, item) ) ]
1109- fn check_trait ( tcx : TyCtxt < ' _ > , item : & hir:: Item < ' _ > ) -> Result < ( ) , ErrorGuaranteed > {
1110- debug ! ( ?item. owner_id) ;
1111-
1112- let def_id = item. owner_id . def_id ;
1089+ #[ instrument( skip( tcx) ) ]
1090+ pub ( crate ) fn check_trait ( tcx : TyCtxt < ' _ > , def_id : LocalDefId ) -> Result < ( ) , ErrorGuaranteed > {
11131091 if tcx. is_lang_item ( def_id. into ( ) , LangItem :: PointeeSized ) {
11141092 // `PointeeSized` is removed during lowering.
11151093 return Ok ( ( ) ) ;
@@ -1135,10 +1113,6 @@ fn check_trait(tcx: TyCtxt<'_>, item: &hir::Item<'_>) -> Result<(), ErrorGuarant
11351113 Ok ( ( ) )
11361114 } ) ;
11371115
1138- // Only check traits, don't check trait aliases
1139- if let hir:: ItemKind :: Trait ( ..) = item. kind {
1140- check_gat_where_clauses ( tcx, item. owner_id . def_id ) ;
1141- }
11421116 res
11431117}
11441118
0 commit comments