rustc_middle/mir/interpret/queries.rs
1use rustc_hir::def::DefKind;
2use rustc_hir::def_id::DefId;
3use rustc_session::lint;
4use rustc_span::{DUMMY_SP, Span};
5use tracing::{debug, instrument};
6
7use super::{
8 ErrorHandled, EvalToAllocationRawResult, EvalToConstValueResult, GlobalId, ReportedErrorInfo,
9};
10use crate::mir::interpret::ValTreeCreationError;
11use crate::ty::{self, ConstToValTreeResult, GenericArgs, TyCtxt, TypeVisitableExt};
12use crate::{error, mir};
13
14impl<'tcx> TyCtxt<'tcx> {
15 /// Evaluates a constant without providing any generic parameters. This is useful to evaluate consts
16 /// that can't take any generic arguments like const items or enum discriminants. If a
17 /// generic parameter is used within the constant `ErrorHandled::TooGeneric` will be returned.
18 #[instrument(skip(self), level = "debug")]
19 pub fn const_eval_poly(self, def_id: DefId) -> EvalToConstValueResult<'tcx> {
20 // In some situations def_id will have generic parameters within scope, but they aren't allowed
21 // to be used. So we can't use `Instance::mono`, instead we feed unresolved generic parameters
22 // into `const_eval` which will return `ErrorHandled::TooGeneric` if any of them are
23 // encountered.
24 let args = GenericArgs::identity_for_item(self, def_id);
25 let instance = ty::Instance::new_raw(def_id, args);
26 let cid = GlobalId { instance, promoted: None };
27 let typing_env = ty::TypingEnv::post_analysis(self, def_id);
28 self.const_eval_global_id(typing_env, cid, DUMMY_SP)
29 }
30
31 /// Evaluates a constant without providing any generic parameters. This is useful to evaluate consts
32 /// that can't take any generic arguments like const items or enum discriminants. If a
33 /// generic parameter is used within the constant `ErrorHandled::TooGeneric` will be returned.
34 #[instrument(skip(self), level = "debug")]
35 pub fn const_eval_poly_to_alloc(self, def_id: DefId) -> EvalToAllocationRawResult<'tcx> {
36 // In some situations def_id will have generic parameters within scope, but they aren't allowed
37 // to be used. So we can't use `Instance::mono`, instead we feed unresolved generic parameters
38 // into `const_eval` which will return `ErrorHandled::TooGeneric` if any of them are
39 // encountered.
40 let args = GenericArgs::identity_for_item(self, def_id);
41 let instance = ty::Instance::new_raw(def_id, args);
42 let cid = GlobalId { instance, promoted: None };
43 let typing_env = ty::TypingEnv::post_analysis(self, def_id);
44 let inputs = self.erase_regions(typing_env.as_query_input(cid));
45 self.eval_to_allocation_raw(inputs)
46 }
47
48 /// Resolves and evaluates a constant.
49 ///
50 /// The constant can be located on a trait like `<A as B>::C`, in which case the given
51 /// generic parameters and environment are used to resolve the constant. Alternatively if the
52 /// constant has generic parameters in scope the generic parameters are used to evaluate the value of
53 /// the constant. For example in `fn foo<T>() { let _ = [0; bar::<T>()]; }` the repeat count
54 /// constant `bar::<T>()` requires a instantiation for `T`, if the instantiation for `T` is still
55 /// too generic for the constant to be evaluated then `Err(ErrorHandled::TooGeneric)` is
56 /// returned.
57 #[instrument(level = "debug", skip(self))]
58 pub fn const_eval_resolve(
59 self,
60 typing_env: ty::TypingEnv<'tcx>,
61 ct: mir::UnevaluatedConst<'tcx>,
62 span: Span,
63 ) -> EvalToConstValueResult<'tcx> {
64 // Cannot resolve `Unevaluated` constants that contain inference
65 // variables. We reject those here since `resolve`
66 // would fail otherwise.
67 //
68 // When trying to evaluate constants containing inference variables,
69 // use `Infcx::const_eval_resolve` instead.
70 if ct.args.has_non_region_infer() {
71 bug!("did not expect inference variables here");
72 }
73
74 // FIXME: maybe have a separate version for resolving mir::UnevaluatedConst?
75 match ty::Instance::try_resolve(self, typing_env, ct.def, ct.args) {
76 Ok(Some(instance)) => {
77 let cid = GlobalId { instance, promoted: ct.promoted };
78 self.const_eval_global_id(typing_env, cid, span)
79 }
80 // For errors during resolution, we deliberately do not point at the usage site of the constant,
81 // since for these errors the place the constant is used shouldn't matter.
82 Ok(None) => Err(ErrorHandled::TooGeneric(DUMMY_SP)),
83 Err(err) => {
84 Err(ErrorHandled::Reported(ReportedErrorInfo::non_const_eval_error(err), DUMMY_SP))
85 }
86 }
87 }
88
89 #[instrument(level = "debug", skip(self))]
90 pub fn const_eval_resolve_for_typeck(
91 self,
92 typing_env: ty::TypingEnv<'tcx>,
93 ct: ty::UnevaluatedConst<'tcx>,
94 span: Span,
95 ) -> ConstToValTreeResult<'tcx> {
96 // Cannot resolve `Unevaluated` constants that contain inference
97 // variables. We reject those here since `resolve`
98 // would fail otherwise.
99 //
100 // When trying to evaluate constants containing inference variables,
101 // use `Infcx::const_eval_resolve` instead.
102 if ct.args.has_non_region_infer() {
103 bug!("did not expect inference variables here");
104 }
105
106 let cid = match ty::Instance::try_resolve(self, typing_env, ct.def, ct.args) {
107 Ok(Some(instance)) => GlobalId { instance, promoted: None },
108 // For errors during resolution, we deliberately do not point at the usage site of the constant,
109 // since for these errors the place the constant is used shouldn't matter.
110 Ok(None) => return Err(ErrorHandled::TooGeneric(DUMMY_SP).into()),
111 Err(err) => {
112 return Err(ErrorHandled::Reported(
113 ReportedErrorInfo::non_const_eval_error(err),
114 DUMMY_SP,
115 )
116 .into());
117 }
118 };
119
120 self.const_eval_global_id_for_typeck(typing_env, cid, span).inspect(|_| {
121 // We are emitting the lint here instead of in `is_const_evaluatable`
122 // as we normalize obligations before checking them, and normalization
123 // uses this function to evaluate this constant.
124 //
125 // @lcnr believes that successfully evaluating even though there are
126 // used generic parameters is a bug of evaluation, so checking for it
127 // here does feel somewhat sensible.
128 if !self.features().generic_const_exprs()
129 && ct.args.has_non_region_param()
130 // We only FCW for anon consts as repeat expr counts with anon consts are the only place
131 // that we have a back compat hack for. We don't need to check this is a const argument
132 // as only anon consts as const args should get evaluated "for the type system".
133 //
134 // If we don't *only* FCW anon consts we can wind up incorrectly FCW'ing uses of assoc
135 // consts in pattern positions. #140447
136 && self.def_kind(cid.instance.def_id()) == DefKind::AnonConst
137 {
138 let mir_body = self.mir_for_ctfe(cid.instance.def_id());
139 if mir_body.is_polymorphic {
140 let Some(local_def_id) = ct.def.as_local() else { return };
141 self.node_span_lint(
142 lint::builtin::CONST_EVALUATABLE_UNCHECKED,
143 self.local_def_id_to_hir_id(local_def_id),
144 self.def_span(ct.def),
145 |lint| {
146 lint.primary_message(
147 "cannot use constants which depend on generic parameters in types",
148 );
149 },
150 )
151 }
152 }
153 })
154 }
155
156 pub fn const_eval_instance(
157 self,
158 typing_env: ty::TypingEnv<'tcx>,
159 instance: ty::Instance<'tcx>,
160 span: Span,
161 ) -> EvalToConstValueResult<'tcx> {
162 self.const_eval_global_id(typing_env, GlobalId { instance, promoted: None }, span)
163 }
164
165 /// Evaluate a constant to a `ConstValue`.
166 #[instrument(skip(self), level = "debug")]
167 pub fn const_eval_global_id(
168 self,
169 typing_env: ty::TypingEnv<'tcx>,
170 cid: GlobalId<'tcx>,
171 span: Span,
172 ) -> EvalToConstValueResult<'tcx> {
173 // Const-eval shouldn't depend on lifetimes at all, so we can erase them, which should
174 // improve caching of queries.
175 let inputs =
176 self.erase_regions(typing_env.with_post_analysis_normalized(self).as_query_input(cid));
177 if !span.is_dummy() {
178 // The query doesn't know where it is being invoked, so we need to fix the span.
179 self.at(span).eval_to_const_value_raw(inputs).map_err(|e| e.with_span(span))
180 } else {
181 self.eval_to_const_value_raw(inputs)
182 }
183 }
184
185 /// Evaluate a constant to a type-level constant.
186 #[instrument(skip(self), level = "debug")]
187 pub fn const_eval_global_id_for_typeck(
188 self,
189 typing_env: ty::TypingEnv<'tcx>,
190 cid: GlobalId<'tcx>,
191 span: Span,
192 ) -> ConstToValTreeResult<'tcx> {
193 // Const-eval shouldn't depend on lifetimes at all, so we can erase them, which should
194 // improve caching of queries.
195 let inputs =
196 self.erase_regions(typing_env.with_post_analysis_normalized(self).as_query_input(cid));
197 debug!(?inputs);
198 let res = if !span.is_dummy() {
199 // The query doesn't know where it is being invoked, so we need to fix the span.
200 self.at(span).eval_to_valtree(inputs).map_err(|e| e.with_span(span))
201 } else {
202 self.eval_to_valtree(inputs)
203 };
204 match res {
205 Ok(valtree) => Ok(Ok(valtree)),
206 Err(err) => {
207 match err {
208 // Let the caller decide how to handle this.
209 ValTreeCreationError::NonSupportedType(ty) => Ok(Err(ty)),
210 // Report the others.
211 ValTreeCreationError::NodesOverflow => {
212 let handled = self.dcx().emit_err(error::MaxNumNodesInValtree {
213 span,
214 global_const_id: cid.display(self),
215 });
216 Err(ReportedErrorInfo::allowed_in_infallible(handled).into())
217 }
218 ValTreeCreationError::InvalidConst => {
219 let handled = self.dcx().emit_err(error::InvalidConstInValtree {
220 span,
221 global_const_id: cid.display(self),
222 });
223 Err(ReportedErrorInfo::allowed_in_infallible(handled).into())
224 }
225 ValTreeCreationError::ErrorHandled(handled) => Err(handled),
226 }
227 }
228 }
229 }
230}