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