@@ -197,10 +197,8 @@ fn check_well_formed(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(), ErrorGua
197
197
_ => unreachable ! ( "{node:?}" ) ,
198
198
} ;
199
199
200
- if let Some ( generics) = node. generics ( ) {
201
- for param in generics. params {
202
- res = res. and ( check_param_wf ( tcx, param) ) ;
203
- }
200
+ for param in & tcx. generics_of ( def_id) . own_params {
201
+ res = res. and ( check_param_wf ( tcx, param) ) ;
204
202
}
205
203
206
204
res
@@ -944,60 +942,62 @@ fn check_impl_item<'tcx>(
944
942
check_associated_item ( tcx, impl_item. owner_id . def_id , span, method_sig)
945
943
}
946
944
947
- fn check_param_wf ( tcx : TyCtxt < ' _ > , param : & hir :: GenericParam < ' _ > ) -> Result < ( ) , ErrorGuaranteed > {
945
+ fn check_param_wf ( tcx : TyCtxt < ' _ > , param : & ty :: GenericParamDef ) -> Result < ( ) , ErrorGuaranteed > {
948
946
match param. kind {
949
947
// We currently only check wf of const params here.
950
- hir :: GenericParamKind :: Lifetime { .. } | hir :: GenericParamKind :: Type { .. } => Ok ( ( ) ) ,
948
+ ty :: GenericParamDefKind :: Lifetime | ty :: GenericParamDefKind :: Type { .. } => Ok ( ( ) ) ,
951
949
952
950
// Const parameters are well formed if their type is structural match.
953
- hir :: GenericParamKind :: Const { ty : hir_ty , default : _ , synthetic : _ } => {
951
+ ty :: GenericParamDefKind :: Const { .. } => {
954
952
let ty = tcx. type_of ( param. def_id ) . instantiate_identity ( ) ;
953
+ let span = tcx. def_span ( param. def_id ) ;
954
+ let def_id = param. def_id . expect_local ( ) ;
955
955
956
956
if tcx. features ( ) . unsized_const_params ( ) {
957
- enter_wf_checking_ctxt ( tcx, tcx. local_parent ( param . def_id ) , |wfcx| {
957
+ enter_wf_checking_ctxt ( tcx, tcx. local_parent ( def_id) , |wfcx| {
958
958
wfcx. register_bound (
959
- ObligationCause :: new (
960
- hir_ty. span ,
961
- param. def_id ,
962
- ObligationCauseCode :: ConstParam ( ty) ,
963
- ) ,
959
+ ObligationCause :: new ( span, def_id, ObligationCauseCode :: ConstParam ( ty) ) ,
964
960
wfcx. param_env ,
965
961
ty,
966
- tcx. require_lang_item ( LangItem :: UnsizedConstParamTy , Some ( hir_ty . span ) ) ,
962
+ tcx. require_lang_item ( LangItem :: UnsizedConstParamTy , Some ( span) ) ,
967
963
) ;
968
964
Ok ( ( ) )
969
965
} )
970
966
} else if tcx. features ( ) . adt_const_params ( ) {
971
- enter_wf_checking_ctxt ( tcx, tcx. local_parent ( param . def_id ) , |wfcx| {
967
+ enter_wf_checking_ctxt ( tcx, tcx. local_parent ( def_id) , |wfcx| {
972
968
wfcx. register_bound (
973
- ObligationCause :: new (
974
- hir_ty. span ,
975
- param. def_id ,
976
- ObligationCauseCode :: ConstParam ( ty) ,
977
- ) ,
969
+ ObligationCause :: new ( span, def_id, ObligationCauseCode :: ConstParam ( ty) ) ,
978
970
wfcx. param_env ,
979
971
ty,
980
- tcx. require_lang_item ( LangItem :: ConstParamTy , Some ( hir_ty . span ) ) ,
972
+ tcx. require_lang_item ( LangItem :: ConstParamTy , Some ( span) ) ,
981
973
) ;
982
974
Ok ( ( ) )
983
975
} )
984
976
} else {
977
+ let span = || {
978
+ let hir:: GenericParamKind :: Const { ty : & hir:: Ty { span, .. } , .. } =
979
+ tcx. hir_node_by_def_id ( def_id) . expect_generic_param ( ) . kind
980
+ else {
981
+ bug ! ( )
982
+ } ;
983
+ span
984
+ } ;
985
985
let mut diag = match ty. kind ( ) {
986
986
ty:: Bool | ty:: Char | ty:: Int ( _) | ty:: Uint ( _) | ty:: Error ( _) => return Ok ( ( ) ) ,
987
987
ty:: FnPtr ( ..) => tcx. dcx ( ) . struct_span_err (
988
- hir_ty . span ,
988
+ span ( ) ,
989
989
"using function pointers as const generic parameters is forbidden" ,
990
990
) ,
991
991
ty:: RawPtr ( _, _) => tcx. dcx ( ) . struct_span_err (
992
- hir_ty . span ,
992
+ span ( ) ,
993
993
"using raw pointers as const generic parameters is forbidden" ,
994
994
) ,
995
995
_ => {
996
996
// Avoid showing "{type error}" to users. See #118179.
997
997
ty. error_reported ( ) ?;
998
998
999
999
tcx. dcx ( ) . struct_span_err (
1000
- hir_ty . span ,
1000
+ span ( ) ,
1001
1001
format ! (
1002
1002
"`{ty}` is forbidden as the type of a const generic parameter" ,
1003
1003
) ,
@@ -1007,7 +1007,7 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) -> Result<(),
1007
1007
1008
1008
diag. note ( "the only supported types are integers, `bool`, and `char`" ) ;
1009
1009
1010
- let cause = ObligationCause :: misc ( hir_ty . span , param . def_id ) ;
1010
+ let cause = ObligationCause :: misc ( span ( ) , def_id) ;
1011
1011
let adt_const_params_feature_string =
1012
1012
" more complex and user defined types" . to_string ( ) ;
1013
1013
let may_suggest_feature = match type_allowed_to_implement_const_param_ty (
@@ -1058,7 +1058,11 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) -> Result<(),
1058
1058
Ok ( ..) => Some ( vec ! [ ( adt_const_params_feature_string, sym:: adt_const_params) ] ) ,
1059
1059
} ;
1060
1060
if let Some ( features) = may_suggest_feature {
1061
- tcx. disabled_nightly_features ( & mut diag, Some ( param. hir_id ) , features) ;
1061
+ tcx. disabled_nightly_features (
1062
+ & mut diag,
1063
+ Some ( tcx. local_def_id_to_hir_id ( def_id) ) ,
1064
+ features,
1065
+ ) ;
1062
1066
}
1063
1067
1064
1068
Err ( diag. emit ( ) )
0 commit comments