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::visit::TypeVisitableExt;
14use crate::ty::{self, GenericArgs, TyCtxt};
15
16impl<'tcx> TyCtxt<'tcx> {
17    /// Evaluates a constant without providing any generic parameters. This is useful to evaluate consts
18    /// that can't take any generic arguments like const items or enum discriminants. If a
19    /// generic parameter is used within the constant `ErrorHandled::TooGeneric` will be returned.
20    #[instrument(skip(self), level = "debug")]
21    pub fn const_eval_poly(self, def_id: DefId) -> EvalToConstValueResult<'tcx> {
22        // In some situations def_id will have generic parameters within scope, but they aren't allowed
23        // to be used. So we can't use `Instance::mono`, instead we feed unresolved generic parameters
24        // into `const_eval` which will return `ErrorHandled::TooGeneric` if any of them are
25        // encountered.
26        let args = GenericArgs::identity_for_item(self, def_id);
27        let instance = ty::Instance::new(def_id, args);
28        let cid = GlobalId { instance, promoted: None };
29        let typing_env = ty::TypingEnv::post_analysis(self, def_id);
30        self.const_eval_global_id(typing_env, cid, DUMMY_SP)
31    }
32
33    /// Evaluates a constant without providing any generic parameters. This is useful to evaluate consts
34    /// that can't take any generic arguments like const items or enum discriminants. If a
35    /// generic parameter is used within the constant `ErrorHandled::TooGeneric` will be returned.
36    #[instrument(skip(self), level = "debug")]
37    pub fn const_eval_poly_to_alloc(self, def_id: DefId) -> EvalToAllocationRawResult<'tcx> {
38        // In some situations def_id will have generic parameters within scope, but they aren't allowed
39        // to be used. So we can't use `Instance::mono`, instead we feed unresolved generic parameters
40        // into `const_eval` which will return `ErrorHandled::TooGeneric` if any of them are
41        // encountered.
42        let args = GenericArgs::identity_for_item(self, def_id);
43        let instance = ty::Instance::new(def_id, args);
44        let cid = GlobalId { instance, promoted: None };
45        let typing_env = ty::TypingEnv::post_analysis(self, def_id);
46        let inputs = self.erase_regions(typing_env.as_query_input(cid));
47        self.eval_to_allocation_raw(inputs)
48    }
49
50    /// Resolves and evaluates a constant.
51    ///
52    /// The constant can be located on a trait like `<A as B>::C`, in which case the given
53    /// generic parameters and environment are used to resolve the constant. Alternatively if the
54    /// constant has generic parameters in scope the generic parameters are used to evaluate the value of
55    /// the constant. For example in `fn foo<T>() { let _ = [0; bar::<T>()]; }` the repeat count
56    /// constant `bar::<T>()` requires a instantiation for `T`, if the instantiation for `T` is still
57    /// too generic for the constant to be evaluated then `Err(ErrorHandled::TooGeneric)` is
58    /// returned.
59    #[instrument(level = "debug", skip(self))]
60    pub fn const_eval_resolve(
61        self,
62        typing_env: ty::TypingEnv<'tcx>,
63        ct: mir::UnevaluatedConst<'tcx>,
64        span: Span,
65    ) -> EvalToConstValueResult<'tcx> {
66        // Cannot resolve `Unevaluated` constants that contain inference
67        // variables. We reject those here since `resolve`
68        // would fail otherwise.
69        //
70        // When trying to evaluate constants containing inference variables,
71        // use `Infcx::const_eval_resolve` instead.
72        if ct.args.has_non_region_infer() {
73            bug!("did not expect inference variables here");
74        }
75
76        // FIXME: maybe have a separate version for resolving mir::UnevaluatedConst?
77        match ty::Instance::try_resolve(self, typing_env, ct.def, ct.args) {
78            Ok(Some(instance)) => {
79                let cid = GlobalId { instance, promoted: ct.promoted };
80                self.const_eval_global_id(typing_env, cid, span)
81            }
82            // For errors during resolution, we deliberately do not point at the usage site of the constant,
83            // since for these errors the place the constant is used shouldn't matter.
84            Ok(None) => Err(ErrorHandled::TooGeneric(DUMMY_SP)),
85            Err(err) => {
86                Err(ErrorHandled::Reported(ReportedErrorInfo::non_const_eval_error(err), DUMMY_SP))
87            }
88        }
89    }
90
91    #[instrument(level = "debug", skip(self))]
92    pub fn const_eval_resolve_for_typeck(
93        self,
94        typing_env: ty::TypingEnv<'tcx>,
95        ct: ty::UnevaluatedConst<'tcx>,
96        span: Span,
97    ) -> EvalToValTreeResult<'tcx> {
98        // Cannot resolve `Unevaluated` constants that contain inference
99        // variables. We reject those here since `resolve`
100        // would fail otherwise.
101        //
102        // When trying to evaluate constants containing inference variables,
103        // use `Infcx::const_eval_resolve` instead.
104        if ct.args.has_non_region_infer() {
105            bug!("did not expect inference variables here");
106        }
107
108        match ty::Instance::try_resolve(self, typing_env, ct.def, ct.args) {
109            Ok(Some(instance)) => {
110                let cid = GlobalId { instance, promoted: None };
111                self.const_eval_global_id_for_typeck(typing_env, cid, span).inspect(|_| {
112                    // We are emitting the lint here instead of in `is_const_evaluatable`
113                    // as we normalize obligations before checking them, and normalization
114                    // uses this function to evaluate this constant.
115                    //
116                    // @lcnr believes that successfully evaluating even though there are
117                    // used generic parameters is a bug of evaluation, so checking for it
118                    // here does feel somewhat sensible.
119                    if !self.features().generic_const_exprs() && ct.args.has_non_region_param() {
120                        let def_kind = self.def_kind(instance.def_id());
121                        assert!(
122                            matches!(
123                                def_kind,
124                                DefKind::InlineConst | DefKind::AnonConst | DefKind::AssocConst
125                            ),
126                            "{cid:?} is {def_kind:?}",
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(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}