rustc_middle/infer/
canonical.rs

1//! **Canonicalization** is the key to constructing a query in the
2//! middle of type inference. Ordinarily, it is not possible to store
3//! types from type inference in query keys, because they contain
4//! references to inference variables whose lifetimes are too short
5//! and so forth. Canonicalizing a value T1 using `canonicalize_query`
6//! produces two things:
7//!
8//! - a value T2 where each unbound inference variable has been
9//!   replaced with a **canonical variable**;
10//! - a map M (of type `CanonicalVarValues`) from those canonical
11//!   variables back to the original.
12//!
13//! We can then do queries using T2. These will give back constraints
14//! on the canonical variables which can be translated, using the map
15//! M, into constraints in our source context. This process of
16//! translating the results back is done by the
17//! `instantiate_query_result` method.
18//!
19//! For a more detailed look at what is happening here, check
20//! out the [chapter in the rustc dev guide][c].
21//!
22//! [c]: https://rust-lang.github.io/chalk/book/canonical_queries/canonicalization.html
23
24use std::collections::hash_map::Entry;
25
26use rustc_data_structures::fx::FxHashMap;
27use rustc_data_structures::sync::Lock;
28use rustc_macros::{HashStable, TypeFoldable, TypeVisitable};
29pub use rustc_type_ir as ir;
30use smallvec::SmallVec;
31
32use crate::mir::ConstraintCategory;
33use crate::ty::{self, GenericArg, List, Ty, TyCtxt, TypeFlags, TypeVisitableExt};
34
35pub type CanonicalQueryInput<'tcx, V> = ir::CanonicalQueryInput<TyCtxt<'tcx>, V>;
36pub type Canonical<'tcx, V> = ir::Canonical<TyCtxt<'tcx>, V>;
37pub type CanonicalVarKind<'tcx> = ir::CanonicalVarKind<TyCtxt<'tcx>>;
38pub type CanonicalVarValues<'tcx> = ir::CanonicalVarValues<TyCtxt<'tcx>>;
39pub type CanonicalVarKinds<'tcx> = &'tcx List<CanonicalVarKind<'tcx>>;
40
41/// When we canonicalize a value to form a query, we wind up replacing
42/// various parts of it with canonical variables. This struct stores
43/// those replaced bits to remember for when we process the query
44/// result.
45#[derive(Clone, Debug)]
46pub struct OriginalQueryValues<'tcx> {
47    /// Map from the universes that appear in the query to the universes in the
48    /// caller context. For all queries except `evaluate_goal` (used by Chalk),
49    /// we only ever put ROOT values into the query, so this map is very
50    /// simple.
51    pub universe_map: SmallVec<[ty::UniverseIndex; 4]>,
52
53    /// This is equivalent to `CanonicalVarValues`, but using a
54    /// `SmallVec` yields a significant performance win.
55    pub var_values: SmallVec<[GenericArg<'tcx>; 8]>,
56}
57
58impl<'tcx> Default for OriginalQueryValues<'tcx> {
59    fn default() -> Self {
60        let mut universe_map = SmallVec::default();
61        universe_map.push(ty::UniverseIndex::ROOT);
62
63        Self { universe_map, var_values: SmallVec::default() }
64    }
65}
66
67/// After we execute a query with a canonicalized key, we get back a
68/// `Canonical<QueryResponse<..>>`. You can use
69/// `instantiate_query_result` to access the data in this result.
70#[derive(Clone, Debug, HashStable, TypeFoldable, TypeVisitable)]
71pub struct QueryResponse<'tcx, R> {
72    pub var_values: CanonicalVarValues<'tcx>,
73    pub region_constraints: QueryRegionConstraints<'tcx>,
74    pub certainty: Certainty,
75    pub opaque_types: Vec<(ty::OpaqueTypeKey<'tcx>, Ty<'tcx>)>,
76    pub value: R,
77}
78
79#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
80#[derive(HashStable, TypeFoldable, TypeVisitable)]
81pub struct QueryRegionConstraints<'tcx> {
82    pub outlives: Vec<QueryOutlivesConstraint<'tcx>>,
83    pub assumptions: Vec<ty::ArgOutlivesPredicate<'tcx>>,
84}
85
86impl QueryRegionConstraints<'_> {
87    /// Represents an empty (trivially true) set of region constraints.
88    ///
89    /// FIXME(higher_ranked_auto): This could still just be true if there are only assumptions?
90    /// Because I don't expect for us to get cases where an assumption from one query would
91    /// discharge a requirement from another query, which is a potential problem if we did throw
92    /// away these assumptions because there were no constraints.
93    pub fn is_empty(&self) -> bool {
94        self.outlives.is_empty() && self.assumptions.is_empty()
95    }
96}
97
98pub type CanonicalQueryResponse<'tcx, T> = &'tcx Canonical<'tcx, QueryResponse<'tcx, T>>;
99
100/// Indicates whether or not we were able to prove the query to be
101/// true.
102#[derive(Copy, Clone, Debug, HashStable)]
103pub enum Certainty {
104    /// The query is known to be true, presuming that you apply the
105    /// given `var_values` and the region-constraints are satisfied.
106    Proven,
107
108    /// The query is not known to be true, but also not known to be
109    /// false. The `var_values` represent *either* values that must
110    /// hold in order for the query to be true, or helpful tips that
111    /// *might* make it true. Currently rustc's trait solver cannot
112    /// distinguish the two (e.g., due to our preference for where
113    /// clauses over impls).
114    ///
115    /// After some unification and things have been done, it makes
116    /// sense to try and prove again -- of course, at that point, the
117    /// canonical form will be different, making this a distinct
118    /// query.
119    Ambiguous,
120}
121
122impl Certainty {
123    pub fn is_proven(&self) -> bool {
124        match self {
125            Certainty::Proven => true,
126            Certainty::Ambiguous => false,
127        }
128    }
129}
130
131impl<'tcx, R> QueryResponse<'tcx, R> {
132    pub fn is_proven(&self) -> bool {
133        self.certainty.is_proven()
134    }
135}
136
137pub type QueryOutlivesConstraint<'tcx> = (ty::ArgOutlivesPredicate<'tcx>, ConstraintCategory<'tcx>);
138
139#[derive(Default)]
140pub struct CanonicalParamEnvCache<'tcx> {
141    map: Lock<
142        FxHashMap<
143            ty::ParamEnv<'tcx>,
144            (Canonical<'tcx, ty::ParamEnv<'tcx>>, &'tcx [GenericArg<'tcx>]),
145        >,
146    >,
147}
148
149impl<'tcx> CanonicalParamEnvCache<'tcx> {
150    /// Gets the cached canonical form of `key` or executes
151    /// `canonicalize_op` and caches the result if not present.
152    ///
153    /// `canonicalize_op` is intentionally not allowed to be a closure to
154    /// statically prevent it from capturing `InferCtxt` and resolving
155    /// inference variables, which invalidates the cache.
156    pub fn get_or_insert(
157        &self,
158        tcx: TyCtxt<'tcx>,
159        key: ty::ParamEnv<'tcx>,
160        state: &mut OriginalQueryValues<'tcx>,
161        canonicalize_op: fn(
162            TyCtxt<'tcx>,
163            ty::ParamEnv<'tcx>,
164            &mut OriginalQueryValues<'tcx>,
165        ) -> Canonical<'tcx, ty::ParamEnv<'tcx>>,
166    ) -> Canonical<'tcx, ty::ParamEnv<'tcx>> {
167        if !key.has_type_flags(
168            TypeFlags::HAS_INFER | TypeFlags::HAS_PLACEHOLDER | TypeFlags::HAS_FREE_REGIONS,
169        ) {
170            return Canonical {
171                max_universe: ty::UniverseIndex::ROOT,
172                variables: List::empty(),
173                value: key,
174            };
175        }
176
177        assert_eq!(state.var_values.len(), 0);
178        assert_eq!(state.universe_map.len(), 1);
179        debug_assert_eq!(&*state.universe_map, &[ty::UniverseIndex::ROOT]);
180
181        match self.map.borrow().entry(key) {
182            Entry::Occupied(e) => {
183                let (canonical, var_values) = e.get();
184                if cfg!(debug_assertions) {
185                    let mut state = state.clone();
186                    let rerun_canonical = canonicalize_op(tcx, key, &mut state);
187                    assert_eq!(rerun_canonical, *canonical);
188                    let OriginalQueryValues { var_values: rerun_var_values, universe_map } = state;
189                    assert_eq!(universe_map.len(), 1);
190                    assert_eq!(**var_values, *rerun_var_values);
191                }
192                state.var_values.extend_from_slice(var_values);
193                *canonical
194            }
195            Entry::Vacant(e) => {
196                let canonical = canonicalize_op(tcx, key, state);
197                let OriginalQueryValues { var_values, universe_map } = state;
198                assert_eq!(universe_map.len(), 1);
199                e.insert((canonical, tcx.arena.alloc_slice(var_values)));
200                canonical
201            }
202        }
203    }
204}