Skip to content

Commit 8bfe95a

Browse files
committed
Make check_param_wf only go through the HIR in the error path
1 parent f811076 commit 8bfe95a

File tree

2 files changed

+36
-27
lines changed

2 files changed

+36
-27
lines changed

compiler/rustc_hir_analysis/src/check/wfcheck.rs

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

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

206204
res
@@ -944,60 +942,62 @@ fn check_impl_item<'tcx>(
944942
check_associated_item(tcx, impl_item.owner_id.def_id, span, method_sig)
945943
}
946944

947-
fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) -> Result<(), ErrorGuaranteed> {
945+
fn check_param_wf(tcx: TyCtxt<'_>, param: &ty::GenericParamDef) -> Result<(), ErrorGuaranteed> {
948946
match param.kind {
949947
// 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(()),
951949

952950
// Const parameters are well formed if their type is structural match.
953-
hir::GenericParamKind::Const { ty: hir_ty, default: _, synthetic: _ } => {
951+
ty::GenericParamDefKind::Const { .. } => {
954952
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();
955955

956956
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| {
958958
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)),
964960
wfcx.param_env,
965961
ty,
966-
tcx.require_lang_item(LangItem::UnsizedConstParamTy, Some(hir_ty.span)),
962+
tcx.require_lang_item(LangItem::UnsizedConstParamTy, Some(span)),
967963
);
968964
Ok(())
969965
})
970966
} 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| {
972968
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)),
978970
wfcx.param_env,
979971
ty,
980-
tcx.require_lang_item(LangItem::ConstParamTy, Some(hir_ty.span)),
972+
tcx.require_lang_item(LangItem::ConstParamTy, Some(span)),
981973
);
982974
Ok(())
983975
})
984976
} 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+
};
985985
let mut diag = match ty.kind() {
986986
ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Error(_) => return Ok(()),
987987
ty::FnPtr(..) => tcx.dcx().struct_span_err(
988-
hir_ty.span,
988+
span(),
989989
"using function pointers as const generic parameters is forbidden",
990990
),
991991
ty::RawPtr(_, _) => tcx.dcx().struct_span_err(
992-
hir_ty.span,
992+
span(),
993993
"using raw pointers as const generic parameters is forbidden",
994994
),
995995
_ => {
996996
// Avoid showing "{type error}" to users. See #118179.
997997
ty.error_reported()?;
998998

999999
tcx.dcx().struct_span_err(
1000-
hir_ty.span,
1000+
span(),
10011001
format!(
10021002
"`{ty}` is forbidden as the type of a const generic parameter",
10031003
),
@@ -1007,7 +1007,7 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) -> Result<(),
10071007

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

1010-
let cause = ObligationCause::misc(hir_ty.span, param.def_id);
1010+
let cause = ObligationCause::misc(span(), def_id);
10111011
let adt_const_params_feature_string =
10121012
" more complex and user defined types".to_string();
10131013
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<(),
10581058
Ok(..) => Some(vec![(adt_const_params_feature_string, sym::adt_const_params)]),
10591059
};
10601060
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+
);
10621066
}
10631067

10641068
Err(diag.emit())

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)