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(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(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() && ct.args.has_non_region_param() {
119                        let def_kind = self.def_kind(instance.def_id());
120                        assert!(
121                            matches!(
122                                def_kind,
123                                DefKind::InlineConst | DefKind::AnonConst | DefKind::AssocConst
124                            ),
125                            "{cid:?} is {def_kind:?}",
126                        );
127                        let mir_body = self.mir_for_ctfe(instance.def_id());
128                        if mir_body.is_polymorphic {
129                            let Some(local_def_id) = ct.def.as_local() else { return };
130                            self.node_span_lint(
131                                lint::builtin::CONST_EVALUATABLE_UNCHECKED,
132                                self.local_def_id_to_hir_id(local_def_id),
133                                self.def_span(ct.def),
134                                |lint| { lint.primary_message("cannot use constants which depend on generic parameters in types"); },
135                            )
136                        }
137                    }
138                })
139            }
140            // For errors during resolution, we deliberately do not point at the usage site of the constant,
141            // since for these errors the place the constant is used shouldn't matter.
142            Ok(None) => Err(ErrorHandled::TooGeneric(DUMMY_SP)),
143            Err(err) => {
144                Err(ErrorHandled::Reported(ReportedErrorInfo::non_const_eval_error(err), DUMMY_SP))
145            }
146        }
147    }
148
149    pub fn const_eval_instance(
150        self,
151        typing_env: ty::TypingEnv<'tcx>,
152        instance: ty::Instance<'tcx>,
153        span: Span,
154    ) -> EvalToConstValueResult<'tcx> {
155        self.const_eval_global_id(typing_env, GlobalId { instance, promoted: None }, span)
156    }
157
158    /// Evaluate a constant to a `ConstValue`.
159    #[instrument(skip(self), level = "debug")]
160    pub fn const_eval_global_id(
161        self,
162        typing_env: ty::TypingEnv<'tcx>,
163        cid: GlobalId<'tcx>,
164        span: Span,
165    ) -> EvalToConstValueResult<'tcx> {
166        // Const-eval shouldn't depend on lifetimes at all, so we can erase them, which should
167        // improve caching of queries.
168        let inputs =
169            self.erase_regions(typing_env.with_post_analysis_normalized(self).as_query_input(cid));
170        if !span.is_dummy() {
171            // The query doesn't know where it is being invoked, so we need to fix the span.
172            self.at(span).eval_to_const_value_raw(inputs).map_err(|e| e.with_span(span))
173        } else {
174            self.eval_to_const_value_raw(inputs)
175        }
176    }
177
178    /// Evaluate a constant to a type-level constant.
179    #[instrument(skip(self), level = "debug")]
180    pub fn const_eval_global_id_for_typeck(
181        self,
182        typing_env: ty::TypingEnv<'tcx>,
183        cid: GlobalId<'tcx>,
184        span: Span,
185    ) -> EvalToValTreeResult<'tcx> {
186        // Const-eval shouldn't depend on lifetimes at all, so we can erase them, which should
187        // improve caching of queries.
188        let inputs =
189            self.erase_regions(typing_env.with_post_analysis_normalized(self).as_query_input(cid));
190        debug!(?inputs);
191        if !span.is_dummy() {
192            // The query doesn't know where it is being invoked, so we need to fix the span.
193            self.at(span).eval_to_valtree(inputs).map_err(|e| e.with_span(span))
194        } else {
195            self.eval_to_valtree(inputs)
196        }
197    }
198}
199
200impl<'tcx> TyCtxtEnsureOk<'tcx> {
201    /// Evaluates a constant without providing any generic parameters. This is useful to evaluate consts
202    /// that can't take any generic arguments like const items or enum discriminants. If a
203    /// generic parameter is used within the constant `ErrorHandled::TooGeneric` will be returned.
204    #[instrument(skip(self), level = "debug")]
205    pub fn const_eval_poly(self, def_id: DefId) {
206        // In some situations def_id will have generic parameters within scope, but they aren't allowed
207        // to be used. So we can't use `Instance::mono`, instead we feed unresolved generic parameters
208        // into `const_eval` which will return `ErrorHandled::TooGeneric` if any of them are
209        // encountered.
210        let args = GenericArgs::identity_for_item(self.tcx, def_id);
211        let instance = ty::Instance::new(def_id, self.tcx.erase_regions(args));
212        let cid = GlobalId { instance, promoted: None };
213        let typing_env = ty::TypingEnv::post_analysis(self.tcx, def_id);
214        // Const-eval shouldn't depend on lifetimes at all, so we can erase them, which should
215        // improve caching of queries.
216        let inputs = self.tcx.erase_regions(typing_env.as_query_input(cid));
217        self.eval_to_const_value_raw(inputs)
218    }
219}