1 use rustc_hir::def::DefKind;
2 use rustc_infer::infer::InferCtxt;
3 use rustc_middle::mir::interpret::ErrorHandled;
4 use rustc_middle::ty::subst::SubstsRef;
5 use rustc_middle::ty::{self, TypeFoldable};
6 use rustc_session::lint;
7 use rustc_span::def_id::DefId;
10 pub fn is_const_evaluatable<'cx, 'tcx>(
11 infcx: &InferCtxt<'cx, 'tcx>,
12 def: ty::WithOptConstParam<DefId>,
13 substs: SubstsRef<'tcx>,
14 param_env: ty::ParamEnv<'tcx>,
16 ) -> Result<(), ErrorHandled> {
17 debug!("is_const_evaluatable({:?}, {:?})", def, substs);
18 if infcx.tcx.features().const_evaluatable_checked {
19 // FIXME(const_evaluatable_checked): Actually look into generic constants to
20 // implement const equality.
21 for pred in param_env.caller_bounds() {
22 match pred.skip_binders() {
23 ty::PredicateAtom::ConstEvaluatable(b_def, b_substs) => {
24 debug!("is_const_evaluatable: caller_bound={:?}, {:?}", b_def, b_substs);
25 if b_def == def && b_substs == substs {
26 debug!("is_const_evaluatable: caller_bound ~~> ok");
35 let future_compat_lint = || {
36 if let Some(local_def_id) = def.did.as_local() {
37 infcx.tcx.struct_span_lint_hir(
38 lint::builtin::CONST_EVALUATABLE_UNCHECKED,
39 infcx.tcx.hir().local_def_id_to_hir_id(local_def_id),
42 err.build("cannot use constants which depend on generic parameters in types")
49 // FIXME: We should only try to evaluate a given constant here if it is fully concrete
50 // as we don't want to allow things like `[u8; std::mem::size_of::<*mut T>()]`.
52 // We previously did not check this, so we only emit a future compat warning if
53 // const evaluation succeeds and the given constant is still polymorphic for now
54 // and hopefully soon change this to an error.
56 // See #74595 for more details about this.
57 let concrete = infcx.const_eval_resolve(param_env, def, substs, None, Some(span));
59 if concrete.is_ok() && substs.has_param_types_or_consts() {
60 match infcx.tcx.def_kind(def.did) {
61 DefKind::AnonConst => {
62 let mir_body = if let Some(def) = def.as_const_arg() {
63 infcx.tcx.optimized_mir_of_const_arg(def)
65 infcx.tcx.optimized_mir(def.did)
68 if mir_body.is_polymorphic {
72 _ => future_compat_lint(),
76 debug!(?concrete, "is_const_evaluatable");