1 use super::{ConstEvalResult, ErrorHandled, GlobalId};
4 use crate::hir::def_id::DefId;
5 use crate::ty::{self, TyCtxt};
6 use crate::ty::subst::{InternalSubsts, SubstsRef};
10 impl<'tcx> TyCtxt<'tcx> {
12 /// Evaluates a constant without providing any substitutions. This is useful to evaluate consts
13 /// that can't take any generic arguments like statics, const items or enum discriminants. If a
14 /// generic parameter is used within the constant `ErrorHandled::ToGeneric` will be returned.
15 pub fn const_eval_poly(self, def_id: DefId) -> ConstEvalResult<'tcx> {
16 // In some situations def_id will have substitutions within scope, but they aren't allowed
17 // to be used. So we can't use `Instance::mono`, instead we feed unresolved substitutions
18 // into `const_eval` which will return `ErrorHandled::ToGeneric` if any og them are
20 let substs = InternalSubsts::identity_for_item(self, def_id);
21 let instance = ty::Instance::new(def_id, substs);
26 let param_env = self.param_env(def_id);
27 self.const_eval_validated(param_env.and(cid))
30 /// Resolves and evaluates a constant.
32 /// The constant can be located on a trait like `<A as B>::C`, in which case the given
33 /// substitutions and environment are used to resolve the constant. Alternatively if the
34 /// constant has generic parameters in scope the substitutions are used to evaluate the value of
35 /// the constant. For example in `fn foo<T>() { let _ = [0; bar::<T>()]; }` the repeat count
36 /// constant `bar::<T>()` requires a substitution for `T`, if the substitution for `T` is still
37 /// too generic for the constant to be evaluated then `Err(ErrorHandled::TooGeneric)` is
39 pub fn const_eval_resolve(
41 param_env: ty::ParamEnv<'tcx>,
43 substs: SubstsRef<'tcx>,
45 ) -> ConstEvalResult<'tcx> {
46 let instance = ty::Instance::resolve(
52 if let Some(instance) = instance {
53 self.const_eval_instance(param_env, instance, span)
55 Err(ErrorHandled::TooGeneric)
59 pub fn const_eval_instance(
61 param_env: ty::ParamEnv<'tcx>,
62 instance: ty::Instance<'tcx>,
64 ) -> ConstEvalResult<'tcx> {
69 if let Some(span) = span {
70 self.at(span).const_eval_validated(param_env.and(cid))
72 self.const_eval_validated(param_env.and(cid))
76 /// Evaluate a promoted constant.
77 pub fn const_eval_promoted(
79 instance: ty::Instance<'tcx>,
80 promoted: mir::Promoted
81 ) -> ConstEvalResult<'tcx> {
84 promoted: Some(promoted),
86 let param_env = ty::ParamEnv::reveal_all();
87 self.const_eval_validated(param_env.and(cid))