@@ -6,10 +6,11 @@ use rustc_abi::ExternAbi;
66use rustc_data_structures:: fx:: { FxHashSet , FxIndexMap , FxIndexSet } ;
77use rustc_errors:: codes:: * ;
88use rustc_errors:: { Applicability , ErrorGuaranteed , pluralize, struct_span_code_err} ;
9+ use rustc_hir:: attrs:: AttributeKind ;
910use rustc_hir:: def:: { DefKind , Res } ;
1011use rustc_hir:: def_id:: { DefId , LocalDefId } ;
1112use rustc_hir:: lang_items:: LangItem ;
12- use rustc_hir:: { AmbigArg , ItemKind } ;
13+ use rustc_hir:: { AmbigArg , ItemKind , find_attr } ;
1314use rustc_infer:: infer:: outlives:: env:: OutlivesEnvironment ;
1415use rustc_infer:: infer:: { self , InferCtxt , SubregionOrigin , TyCtxtInferExt } ;
1516use rustc_lint_defs:: builtin:: SUPERTRAIT_ITEM_SHADOWING_DEFINITION ;
@@ -925,11 +926,11 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &ty::GenericParamDef) -> Result<(), Er
925926#[ instrument( level = "debug" , skip( tcx) ) ]
926927pub ( crate ) fn check_associated_item (
927928 tcx : TyCtxt < ' _ > ,
928- item_id : LocalDefId ,
929+ def_id : LocalDefId ,
929930) -> Result < ( ) , ErrorGuaranteed > {
930- let loc = Some ( WellFormedLoc :: Ty ( item_id ) ) ;
931- enter_wf_checking_ctxt ( tcx, item_id , |wfcx| {
932- let item = tcx. associated_item ( item_id ) ;
931+ let loc = Some ( WellFormedLoc :: Ty ( def_id ) ) ;
932+ enter_wf_checking_ctxt ( tcx, def_id , |wfcx| {
933+ let item = tcx. associated_item ( def_id ) ;
933934
934935 // Avoid bogus "type annotations needed `Foo: Bar`" errors on `impl Bar for Foo` in case
935936 // other `Foo` impls are incoherent.
@@ -942,36 +943,42 @@ pub(crate) fn check_associated_item(
942943 }
943944 } ;
944945
945- let span = tcx. def_span ( item_id ) ;
946+ let span = tcx. def_span ( def_id ) ;
946947
947948 match item. kind {
948949 ty:: AssocKind :: Const { .. } => {
949- let ty = tcx. type_of ( item . def_id ) . instantiate_identity ( ) ;
950- let ty = wfcx. deeply_normalize ( span, Some ( WellFormedLoc :: Ty ( item_id ) ) , ty) ;
950+ let ty = tcx. type_of ( def_id) . instantiate_identity ( ) ;
951+ let ty = wfcx. deeply_normalize ( span, Some ( WellFormedLoc :: Ty ( def_id ) ) , ty) ;
951952 wfcx. register_wf_obligation ( span, loc, ty. into ( ) ) ;
952- check_sized_if_body (
953- wfcx,
954- item. def_id . expect_local ( ) ,
955- ty,
956- Some ( span) ,
957- ObligationCauseCode :: SizedConstOrStatic ,
958- ) ;
953+
954+ if item. defaultness ( tcx) . has_value ( ) {
955+ let code = ObligationCauseCode :: SizedConstOrStatic ;
956+ wfcx. register_bound (
957+ ObligationCause :: new ( span, def_id, code) ,
958+ wfcx. param_env ,
959+ ty,
960+ tcx. require_lang_item ( LangItem :: Sized , span) ,
961+ ) ;
962+
963+ check_const_item_rhs ( wfcx, def_id) ?;
964+ }
965+
959966 Ok ( ( ) )
960967 }
961968 ty:: AssocKind :: Fn { .. } => {
962- let sig = tcx. fn_sig ( item . def_id ) . instantiate_identity ( ) ;
969+ let sig = tcx. fn_sig ( def_id) . instantiate_identity ( ) ;
963970 let hir_sig =
964- tcx. hir_node_by_def_id ( item_id ) . fn_sig ( ) . expect ( "bad signature for method" ) ;
965- check_fn_or_method ( wfcx, sig, hir_sig. decl , item_id ) ;
971+ tcx. hir_node_by_def_id ( def_id ) . fn_sig ( ) . expect ( "bad signature for method" ) ;
972+ check_fn_or_method ( wfcx, sig, hir_sig. decl , def_id ) ;
966973 check_method_receiver ( wfcx, hir_sig, item, self_ty)
967974 }
968975 ty:: AssocKind :: Type { .. } => {
969976 if let ty:: AssocContainer :: Trait = item. container {
970977 check_associated_type_bounds ( wfcx, item, span)
971978 }
972979 if item. defaultness ( tcx) . has_value ( ) {
973- let ty = tcx. type_of ( item . def_id ) . instantiate_identity ( ) ;
974- let ty = wfcx. deeply_normalize ( span, Some ( WellFormedLoc :: Ty ( item_id ) ) , ty) ;
980+ let ty = tcx. type_of ( def_id) . instantiate_identity ( ) ;
981+ let ty = wfcx. deeply_normalize ( span, Some ( WellFormedLoc :: Ty ( def_id ) ) , ty) ;
975982 wfcx. register_wf_obligation ( span, loc, ty. into ( ) ) ;
976983 }
977984 Ok ( ( ) )
@@ -1222,28 +1229,19 @@ pub(crate) fn check_static_item<'tcx>(
12221229 } )
12231230}
12241231
1225- pub ( crate ) fn check_const_item ( tcx : TyCtxt < ' _ > , def_id : LocalDefId ) -> Result < ( ) , ErrorGuaranteed > {
1226- enter_wf_checking_ctxt ( tcx, def_id, |wfcx| {
1227- let ty = tcx. type_of ( def_id) . instantiate_identity ( ) ;
1228- let ty_span = tcx. ty_span ( def_id) ;
1229- let ty = wfcx. deeply_normalize ( ty_span, Some ( WellFormedLoc :: Ty ( def_id) ) , ty) ;
1230-
1231- wfcx. register_wf_obligation ( ty_span, Some ( WellFormedLoc :: Ty ( def_id) ) , ty. into ( ) ) ;
1232- wfcx. register_bound (
1233- traits:: ObligationCause :: new (
1234- ty_span,
1235- wfcx. body_def_id ,
1236- ObligationCauseCode :: SizedConstOrStatic ,
1237- ) ,
1238- wfcx. param_env ,
1239- ty,
1240- tcx. require_lang_item ( LangItem :: Sized , ty_span) ,
1241- ) ;
1242-
1243- check_where_clauses ( wfcx, def_id) ;
1244-
1245- Ok ( ( ) )
1246- } )
1232+ #[ instrument( level = "debug" , skip( wfcx) ) ]
1233+ pub ( super ) fn check_const_item_rhs < ' tcx > (
1234+ wfcx : & WfCheckingCtxt < ' _ , ' tcx > ,
1235+ def_id : LocalDefId ,
1236+ ) -> Result < ( ) , ErrorGuaranteed > {
1237+ let tcx = wfcx. tcx ( ) ;
1238+ if find_attr ! ( tcx. get_all_attrs( def_id) , AttributeKind :: TypeConst ( _) ) {
1239+ let raw_ct = tcx. const_of_item ( def_id) . instantiate_identity ( ) ;
1240+ let span = tcx. def_span ( def_id) ;
1241+ let norm_ct = wfcx. deeply_normalize ( span, Some ( WellFormedLoc :: Ty ( def_id) ) , raw_ct) ;
1242+ wfcx. register_wf_obligation ( span, Some ( WellFormedLoc :: Ty ( def_id) ) , norm_ct. into ( ) ) ;
1243+ }
1244+ Ok ( ( ) )
12471245}
12481246
12491247#[ instrument( level = "debug" , skip( tcx, impl_) ) ]
@@ -1583,33 +1581,16 @@ fn check_fn_or_method<'tcx>(
15831581 }
15841582
15851583 // If the function has a body, additionally require that the return type is sized.
1586- check_sized_if_body (
1587- wfcx,
1588- def_id,
1589- sig. output ( ) ,
1590- match hir_decl. output {
1591- hir:: FnRetTy :: Return ( ty) => Some ( ty. span ) ,
1592- hir:: FnRetTy :: DefaultReturn ( _) => None ,
1593- } ,
1594- ObligationCauseCode :: SizedReturnType ,
1595- ) ;
1596- }
1597-
1598- fn check_sized_if_body < ' tcx > (
1599- wfcx : & WfCheckingCtxt < ' _ , ' tcx > ,
1600- def_id : LocalDefId ,
1601- ty : Ty < ' tcx > ,
1602- maybe_span : Option < Span > ,
1603- code : ObligationCauseCode < ' tcx > ,
1604- ) {
1605- let tcx = wfcx. tcx ( ) ;
16061584 if let Some ( body) = tcx. hir_maybe_body_owned_by ( def_id) {
1607- let span = maybe_span. unwrap_or ( body. value . span ) ;
1585+ let span = match hir_decl. output {
1586+ hir:: FnRetTy :: Return ( ty) => ty. span ,
1587+ hir:: FnRetTy :: DefaultReturn ( _) => body. value . span ,
1588+ } ;
16081589
16091590 wfcx. register_bound (
1610- ObligationCause :: new ( span, def_id, code ) ,
1591+ ObligationCause :: new ( span, def_id, ObligationCauseCode :: SizedReturnType ) ,
16111592 wfcx. param_env ,
1612- ty ,
1593+ sig . output ( ) ,
16131594 tcx. require_lang_item ( LangItem :: Sized , span) ,
16141595 ) ;
16151596 }
0 commit comments