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