@@ -22,9 +22,9 @@ use rustc_middle::mir::interpret::ErrorHandled;
2222use rustc_middle:: traits:: solve:: NoSolution ;
2323use rustc_middle:: ty:: trait_def:: TraitSpecializationKind ;
2424use rustc_middle:: ty:: {
25- self , AdtKind , GenericArgKind , GenericArgs , GenericParamDefKind , Ty , TyCtxt , TypeFlags ,
26- TypeFoldable , TypeSuperVisitable , TypeVisitable , TypeVisitableExt , TypeVisitor , TypingMode ,
27- Unnormalized , Upcast ,
25+ self , GenericArgKind , GenericArgs , GenericParamDefKind , Ty , TyCtxt , TypeFlags , TypeFoldable ,
26+ TypeSuperVisitable , TypeVisitable , TypeVisitableExt , TypeVisitor , TypingMode , Unnormalized ,
27+ Upcast ,
2828} ;
2929use rustc_middle:: { bug, span_bug} ;
3030use rustc_session:: errors:: feature_err;
@@ -324,12 +324,8 @@ pub(super) fn check_item<'tcx>(
324324 res
325325 }
326326 hir:: ItemKind :: Fn { sig, .. } => check_item_fn ( tcx, def_id, sig. decl ) ,
327- hir:: ItemKind :: Struct ( ..) => check_type_defn ( tcx, item, false ) ,
328- hir:: ItemKind :: Union ( ..) => check_type_defn ( tcx, item, true ) ,
329- hir:: ItemKind :: Enum ( ..) => check_type_defn ( tcx, item, true ) ,
330- hir:: ItemKind :: Trait { .. } => check_trait ( tcx, item) ,
331- hir:: ItemKind :: TraitAlias ( ..) => check_trait ( tcx, item) ,
332- _ => Ok ( ( ) ) ,
327+ // Note: do not add new entries to this match. Instead add all new logic in `check_item_type`
328+ _ => span_bug ! ( item. span, "should have been handled by the type based wf check: {item:?}" ) ,
333329 }
334330}
335331
@@ -381,7 +377,7 @@ pub(crate) fn check_trait_item<'tcx>(
381377/// fn into_iter<'a>(&'a self) -> Self::Iter<'a>;
382378/// }
383379/// ```
384- fn check_gat_where_clauses ( tcx : TyCtxt < ' _ > , trait_def_id : LocalDefId ) {
380+ pub ( crate ) fn check_gat_where_clauses ( tcx : TyCtxt < ' _ > , trait_def_id : LocalDefId ) {
385381 // Associates every GAT's def_id to a list of possibly missing bounds detected by this lint.
386382 let mut required_bounds_by_item = FxIndexMap :: default ( ) ;
387383 let associated_items = tcx. associated_items ( trait_def_id) ;
@@ -1036,15 +1032,15 @@ pub(crate) fn check_associated_item(
10361032}
10371033
10381034/// In a type definition, we check that to ensure that the types of the fields are well-formed.
1039- fn check_type_defn < ' tcx > (
1035+ pub ( crate ) fn check_type_defn < ' tcx > (
10401036 tcx : TyCtxt < ' tcx > ,
1041- item : & hir :: Item < ' tcx > ,
1037+ item : LocalDefId ,
10421038 all_sized : bool ,
10431039) -> Result < ( ) , ErrorGuaranteed > {
1044- tcx. ensure_ok ( ) . check_representability ( item. owner_id . def_id ) ;
1045- let adt_def = tcx. adt_def ( item. owner_id ) ;
1040+ tcx. ensure_ok ( ) . check_representability ( item) ;
1041+ let adt_def = tcx. adt_def ( item) ;
10461042
1047- enter_wf_checking_ctxt ( tcx, item. owner_id . def_id , |wfcx| {
1043+ enter_wf_checking_ctxt ( tcx, item, |wfcx| {
10481044 let variants = adt_def. variants ( ) ;
10491045 let packed = adt_def. repr ( ) . packed ( ) ;
10501046
@@ -1057,6 +1053,7 @@ fn check_type_defn<'tcx>(
10571053 // FIXME(generic_const_exprs, default_field_values): this is a hack and needs to
10581054 // be refactored to check the instantiate-ability of the code better.
10591055 if let Some ( def_id) = def_id. as_local ( )
1056+ && let DefKind :: AnonConst = tcx. def_kind ( def_id)
10601057 && let hir:: Node :: AnonConst ( anon) = tcx. hir_node_by_def_id ( def_id)
10611058 && let expr = & tcx. hir_body ( anon. body ) . value
10621059 && let hir:: ExprKind :: Path ( hir:: QPath :: Resolved ( None , path) ) = expr. kind
@@ -1071,26 +1068,21 @@ fn check_type_defn<'tcx>(
10711068 }
10721069 }
10731070 let field_id = field. did . expect_local ( ) ;
1074- let hir:: FieldDef { ty : hir_ty, .. } =
1075- tcx. hir_node_by_def_id ( field_id) . expect_field ( ) ;
1071+ let span = tcx. ty_span ( field_id) ;
10761072 let ty = wfcx. deeply_normalize (
1077- hir_ty . span ,
1073+ span,
10781074 None ,
10791075 tcx. type_of ( field. did ) . instantiate_identity ( ) ,
10801076 ) ;
1081- wfcx. register_wf_obligation (
1082- hir_ty. span ,
1083- Some ( WellFormedLoc :: Ty ( field_id) ) ,
1084- ty. into ( ) ,
1085- ) ;
1077+ wfcx. register_wf_obligation ( span, Some ( WellFormedLoc :: Ty ( field_id) ) , ty. into ( ) ) ;
10861078
10871079 if matches ! ( ty. kind( ) , ty:: Adt ( def, _) if def. repr( ) . scalable( ) )
10881080 && !matches ! ( adt_def. repr( ) . scalable, Some ( ScalableElt :: Container ) )
10891081 {
10901082 // Scalable vectors can only be fields of structs if the type has a
10911083 // `rustc_scalable_vector` attribute w/out specifying an element count
10921084 tcx. dcx ( ) . span_err (
1093- hir_ty . span ,
1085+ span,
10941086 format ! (
10951087 "scalable vectors cannot be fields of a {}" ,
10961088 adt_def. variant_descr( )
@@ -1116,35 +1108,21 @@ fn check_type_defn<'tcx>(
11161108 variant. fields . raw [ ..variant. fields . len ( ) - unsized_len] . iter ( ) . enumerate ( )
11171109 {
11181110 let last = idx == variant. fields . len ( ) - 1 ;
1119- let field_id = field. did . expect_local ( ) ;
1120- let hir:: FieldDef { ty : hir_ty, .. } =
1121- tcx. hir_node_by_def_id ( field_id) . expect_field ( ) ;
1122- let ty = wfcx. normalize (
1123- hir_ty. span ,
1124- None ,
1125- tcx. type_of ( field. did ) . instantiate_identity ( ) ,
1126- ) ;
1111+ let span = tcx. ty_span ( field. did . expect_local ( ) ) ;
1112+ let ty = wfcx. normalize ( span, None , tcx. type_of ( field. did ) . instantiate_identity ( ) ) ;
11271113 wfcx. register_bound (
11281114 traits:: ObligationCause :: new (
1129- hir_ty . span ,
1115+ span,
11301116 wfcx. body_def_id ,
11311117 ObligationCauseCode :: FieldSized {
1132- adt_kind : match & item. kind {
1133- ItemKind :: Struct ( ..) => AdtKind :: Struct ,
1134- ItemKind :: Union ( ..) => AdtKind :: Union ,
1135- ItemKind :: Enum ( ..) => AdtKind :: Enum ,
1136- kind => span_bug ! (
1137- item. span,
1138- "should be wfchecking an ADT, got {kind:?}"
1139- ) ,
1140- } ,
1141- span : hir_ty. span ,
1118+ adt_kind : adt_def. adt_kind ( ) ,
1119+ span,
11421120 last,
11431121 } ,
11441122 ) ,
11451123 wfcx. param_env ,
11461124 ty,
1147- tcx. require_lang_item ( LangItem :: Sized , hir_ty . span ) ,
1125+ tcx. require_lang_item ( LangItem :: Sized , span) ,
11481126 ) ;
11491127 }
11501128
@@ -1160,16 +1138,13 @@ fn check_type_defn<'tcx>(
11601138 }
11611139 }
11621140
1163- check_where_clauses ( wfcx, item. owner_id . def_id ) ;
1141+ check_where_clauses ( wfcx, item) ;
11641142 Ok ( ( ) )
11651143 } )
11661144}
11671145
1168- #[ instrument( skip( tcx, item) ) ]
1169- fn check_trait ( tcx : TyCtxt < ' _ > , item : & hir:: Item < ' _ > ) -> Result < ( ) , ErrorGuaranteed > {
1170- debug ! ( ?item. owner_id) ;
1171-
1172- let def_id = item. owner_id . def_id ;
1146+ #[ instrument( skip( tcx) ) ]
1147+ pub ( crate ) fn check_trait ( tcx : TyCtxt < ' _ > , def_id : LocalDefId ) -> Result < ( ) , ErrorGuaranteed > {
11731148 if tcx. is_lang_item ( def_id. into ( ) , LangItem :: PointeeSized ) {
11741149 // `PointeeSized` is removed during lowering.
11751150 return Ok ( ( ) ) ;
@@ -1195,10 +1170,6 @@ fn check_trait(tcx: TyCtxt<'_>, item: &hir::Item<'_>) -> Result<(), ErrorGuarant
11951170 Ok ( ( ) )
11961171 } ) ;
11971172
1198- // Only check traits, don't check trait aliases
1199- if let hir:: ItemKind :: Trait { .. } = item. kind {
1200- check_gat_where_clauses ( tcx, item. owner_id . def_id ) ;
1201- }
12021173 res
12031174}
12041175
0 commit comments