@@ -198,10 +198,8 @@ fn check_well_formed(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(), ErrorGua
198
198
_ => unreachable ! ( "{node:?}" ) ,
199
199
} ;
200
200
201
- if let Some ( generics) = node. generics ( ) {
202
- for param in generics. params {
203
- res = res. and ( check_param_wf ( tcx, param) ) ;
204
- }
201
+ for param in & tcx. generics_of ( def_id) . own_params {
202
+ res = res. and ( check_param_wf ( tcx, param) ) ;
205
203
}
206
204
207
205
res
@@ -877,60 +875,62 @@ fn check_impl_item<'tcx>(
877
875
check_associated_item ( tcx, impl_item. owner_id . def_id , span, method_sig)
878
876
}
879
877
880
- fn check_param_wf ( tcx : TyCtxt < ' _ > , param : & hir :: GenericParam < ' _ > ) -> Result < ( ) , ErrorGuaranteed > {
878
+ fn check_param_wf ( tcx : TyCtxt < ' _ > , param : & ty :: GenericParamDef ) -> Result < ( ) , ErrorGuaranteed > {
881
879
match param. kind {
882
880
// We currently only check wf of const params here.
883
- hir :: GenericParamKind :: Lifetime { .. } | hir :: GenericParamKind :: Type { .. } => Ok ( ( ) ) ,
881
+ ty :: GenericParamDefKind :: Lifetime | ty :: GenericParamDefKind :: Type { .. } => Ok ( ( ) ) ,
884
882
885
883
// Const parameters are well formed if their type is structural match.
886
- hir :: GenericParamKind :: Const { ty : hir_ty , default : _ , synthetic : _ } => {
884
+ ty :: GenericParamDefKind :: Const { .. } => {
887
885
let ty = tcx. type_of ( param. def_id ) . instantiate_identity ( ) ;
886
+ let span = tcx. def_span ( param. def_id ) ;
887
+ let def_id = param. def_id . expect_local ( ) ;
888
888
889
889
if tcx. features ( ) . unsized_const_params ( ) {
890
- enter_wf_checking_ctxt ( tcx, tcx. local_parent ( param . def_id ) , |wfcx| {
890
+ enter_wf_checking_ctxt ( tcx, tcx. local_parent ( def_id) , |wfcx| {
891
891
wfcx. register_bound (
892
- ObligationCause :: new (
893
- hir_ty. span ,
894
- param. def_id ,
895
- ObligationCauseCode :: ConstParam ( ty) ,
896
- ) ,
892
+ ObligationCause :: new ( span, def_id, ObligationCauseCode :: ConstParam ( ty) ) ,
897
893
wfcx. param_env ,
898
894
ty,
899
- tcx. require_lang_item ( LangItem :: UnsizedConstParamTy , hir_ty . span ) ,
895
+ tcx. require_lang_item ( LangItem :: UnsizedConstParamTy , span) ,
900
896
) ;
901
897
Ok ( ( ) )
902
898
} )
903
899
} else if tcx. features ( ) . adt_const_params ( ) {
904
- enter_wf_checking_ctxt ( tcx, tcx. local_parent ( param . def_id ) , |wfcx| {
900
+ enter_wf_checking_ctxt ( tcx, tcx. local_parent ( def_id) , |wfcx| {
905
901
wfcx. register_bound (
906
- ObligationCause :: new (
907
- hir_ty. span ,
908
- param. def_id ,
909
- ObligationCauseCode :: ConstParam ( ty) ,
910
- ) ,
902
+ ObligationCause :: new ( span, def_id, ObligationCauseCode :: ConstParam ( ty) ) ,
911
903
wfcx. param_env ,
912
904
ty,
913
- tcx. require_lang_item ( LangItem :: ConstParamTy , hir_ty . span ) ,
905
+ tcx. require_lang_item ( LangItem :: ConstParamTy , span) ,
914
906
) ;
915
907
Ok ( ( ) )
916
908
} )
917
909
} else {
910
+ let span = || {
911
+ let hir:: GenericParamKind :: Const { ty : & hir:: Ty { span, .. } , .. } =
912
+ tcx. hir_node_by_def_id ( def_id) . expect_generic_param ( ) . kind
913
+ else {
914
+ bug ! ( )
915
+ } ;
916
+ span
917
+ } ;
918
918
let mut diag = match ty. kind ( ) {
919
919
ty:: Bool | ty:: Char | ty:: Int ( _) | ty:: Uint ( _) | ty:: Error ( _) => return Ok ( ( ) ) ,
920
920
ty:: FnPtr ( ..) => tcx. dcx ( ) . struct_span_err (
921
- hir_ty . span ,
921
+ span ( ) ,
922
922
"using function pointers as const generic parameters is forbidden" ,
923
923
) ,
924
924
ty:: RawPtr ( _, _) => tcx. dcx ( ) . struct_span_err (
925
- hir_ty . span ,
925
+ span ( ) ,
926
926
"using raw pointers as const generic parameters is forbidden" ,
927
927
) ,
928
928
_ => {
929
929
// Avoid showing "{type error}" to users. See #118179.
930
930
ty. error_reported ( ) ?;
931
931
932
932
tcx. dcx ( ) . struct_span_err (
933
- hir_ty . span ,
933
+ span ( ) ,
934
934
format ! (
935
935
"`{ty}` is forbidden as the type of a const generic parameter" ,
936
936
) ,
@@ -940,7 +940,7 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) -> Result<(),
940
940
941
941
diag. note ( "the only supported types are integers, `bool`, and `char`" ) ;
942
942
943
- let cause = ObligationCause :: misc ( hir_ty . span , param . def_id ) ;
943
+ let cause = ObligationCause :: misc ( span ( ) , def_id) ;
944
944
let adt_const_params_feature_string =
945
945
" more complex and user defined types" . to_string ( ) ;
946
946
let may_suggest_feature = match type_allowed_to_implement_const_param_ty (
0 commit comments