check types of const param default

This commit is contained in:
lcnr
2025-04-10 20:23:43 +02:00
parent dc8fe1f81c
commit 8cd12bf8fa
3 changed files with 71 additions and 0 deletions

View File

@@ -1488,6 +1488,39 @@ fn check_where_clauses<'tcx>(wfcx: &WfCheckingCtxt<'_, 'tcx>, span: Span, def_id
.then(|| WellFormedLoc::Ty(param.def_id.expect_local())),
default,
);
} else {
// If we've got a generic const parameter we still want to check its
// type is correct in case both it and the param type are fully concrete.
let GenericArgKind::Const(ct) = default.unpack() else {
continue;
};
let ct_ty = match ct.kind() {
ty::ConstKind::Infer(_)
| ty::ConstKind::Placeholder(_)
| ty::ConstKind::Bound(_, _) => unreachable!(),
ty::ConstKind::Error(_) | ty::ConstKind::Expr(_) => continue,
ty::ConstKind::Value(cv) => cv.ty,
ty::ConstKind::Unevaluated(uv) => {
infcx.tcx.type_of(uv.def).instantiate(infcx.tcx, uv.args)
}
ty::ConstKind::Param(param_ct) => param_ct.find_ty_from_env(wfcx.param_env),
};
let param_ty = tcx.type_of(param.def_id).instantiate_identity();
if !ct_ty.has_param() && !param_ty.has_param() {
let cause = traits::ObligationCause::new(
tcx.def_span(param.def_id),
wfcx.body_def_id,
ObligationCauseCode::WellFormed(None),
);
wfcx.register_obligation(Obligation::new(
tcx,
cause,
wfcx.param_env,
ty::ClauseKind::ConstArgHasType(ct, param_ty),
));
}
}
}
}