Skip to content

Commit cca072c

Browse files
committed
Make check_param_wf only go through the HIR in the error path
1 parent 215009b commit cca072c

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
@@ -881,60 +879,62 @@ fn check_impl_item<'tcx>(
881879
check_associated_item(tcx, impl_item.owner_id.def_id, span, method_sig)
882880
}
883881

884-
fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) -> Result<(), ErrorGuaranteed> {
882+
fn check_param_wf(tcx: TyCtxt<'_>, param: &ty::GenericParamDef) -> Result<(), ErrorGuaranteed> {
885883
match param.kind {
886884
// 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(()),
888886

889887
// Const parameters are well formed if their type is structural match.
890-
hir::GenericParamKind::Const { ty: hir_ty, default: _, synthetic: _ } => {
888+
ty::GenericParamDefKind::Const { .. } => {
891889
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();
892892

893893
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| {
895895
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)),
901897
wfcx.param_env,
902898
ty,
903-
tcx.require_lang_item(LangItem::UnsizedConstParamTy, hir_ty.span),
899+
tcx.require_lang_item(LangItem::UnsizedConstParamTy, span),
904900
);
905901
Ok(())
906902
})
907903
} 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| {
909905
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)),
915907
wfcx.param_env,
916908
ty,
917-
tcx.require_lang_item(LangItem::ConstParamTy, hir_ty.span),
909+
tcx.require_lang_item(LangItem::ConstParamTy, span),
918910
);
919911
Ok(())
920912
})
921913
} 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+
};
922922
let mut diag = match ty.kind() {
923923
ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Error(_) => return Ok(()),
924924
ty::FnPtr(..) => tcx.dcx().struct_span_err(
925-
hir_ty.span,
925+
span(),
926926
"using function pointers as const generic parameters is forbidden",
927927
),
928928
ty::RawPtr(_, _) => tcx.dcx().struct_span_err(
929-
hir_ty.span,
929+
span(),
930930
"using raw pointers as const generic parameters is forbidden",
931931
),
932932
_ => {
933933
// Avoid showing "{type error}" to users. See #118179.
934934
ty.error_reported()?;
935935

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

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

947-
let cause = ObligationCause::misc(hir_ty.span, param.def_id);
947+
let cause = ObligationCause::misc(span(), def_id);
948948
let adt_const_params_feature_string =
949949
" more complex and user defined types".to_string();
950950
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)