Skip to content

Commit c3a3fd4

Browse files
committed
Make check_param_wf only go through the HIR in the error path
1 parent 642874e commit c3a3fd4

File tree

2 files changed

+31
-26
lines changed

2 files changed

+31
-26
lines changed

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,8 @@ fn check_well_formed(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(), ErrorGua
198198
_ => unreachable!("{node:?}"),
199199
};
200200

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));
205203
}
206204

207205
res
@@ -877,60 +875,62 @@ fn check_impl_item<'tcx>(
877875
check_associated_item(tcx, impl_item.owner_id.def_id, span, method_sig)
878876
}
879877

880-
fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) -> Result<(), ErrorGuaranteed> {
878+
fn check_param_wf(tcx: TyCtxt<'_>, param: &ty::GenericParamDef) -> Result<(), ErrorGuaranteed> {
881879
match param.kind {
882880
// 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(()),
884882

885883
// Const parameters are well formed if their type is structural match.
886-
hir::GenericParamKind::Const { ty: hir_ty, default: _, synthetic: _ } => {
884+
ty::GenericParamDefKind::Const { .. } => {
887885
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();
888888

889889
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| {
891891
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)),
897893
wfcx.param_env,
898894
ty,
899-
tcx.require_lang_item(LangItem::UnsizedConstParamTy, hir_ty.span),
895+
tcx.require_lang_item(LangItem::UnsizedConstParamTy, span),
900896
);
901897
Ok(())
902898
})
903899
} 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| {
905901
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)),
911903
wfcx.param_env,
912904
ty,
913-
tcx.require_lang_item(LangItem::ConstParamTy, hir_ty.span),
905+
tcx.require_lang_item(LangItem::ConstParamTy, span),
914906
);
915907
Ok(())
916908
})
917909
} 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+
};
918918
let mut diag = match ty.kind() {
919919
ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Error(_) => return Ok(()),
920920
ty::FnPtr(..) => tcx.dcx().struct_span_err(
921-
hir_ty.span,
921+
span(),
922922
"using function pointers as const generic parameters is forbidden",
923923
),
924924
ty::RawPtr(_, _) => tcx.dcx().struct_span_err(
925-
hir_ty.span,
925+
span(),
926926
"using raw pointers as const generic parameters is forbidden",
927927
),
928928
_ => {
929929
// Avoid showing "{type error}" to users. See #118179.
930930
ty.error_reported()?;
931931

932932
tcx.dcx().struct_span_err(
933-
hir_ty.span,
933+
span(),
934934
format!(
935935
"`{ty}` is forbidden as the type of a const generic parameter",
936936
),
@@ -940,7 +940,7 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) -> Result<(),
940940

941941
diag.note("the only supported types are integers, `bool`, and `char`");
942942

943-
let cause = ObligationCause::misc(hir_ty.span, param.def_id);
943+
let cause = ObligationCause::misc(span(), def_id);
944944
let adt_const_params_feature_string =
945945
" more complex and user defined types".to_string();
946946
let may_suggest_feature = match type_allowed_to_implement_const_param_ty(

compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1186,7 +1186,12 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
11861186
ty: Ty<'tcx>,
11871187
obligation: &PredicateObligation<'tcx>,
11881188
) -> Diag<'a> {
1189-
let span = obligation.cause.span;
1189+
let param = obligation.cause.body_id;
1190+
let hir::GenericParamKind::Const { ty: &hir::Ty { span, .. }, .. } =
1191+
self.tcx.hir_node_by_def_id(param).expect_generic_param().kind
1192+
else {
1193+
bug!()
1194+
};
11901195

11911196
let mut diag = match ty.kind() {
11921197
ty::Float(_) => {

0 commit comments

Comments
 (0)