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