rustc_next_trait_solver/
canonicalizer.rs

1use std::cmp::Ordering;
2
3use rustc_type_ir::data_structures::HashMap;
4use rustc_type_ir::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable};
5use rustc_type_ir::inherent::*;
6use rustc_type_ir::solve::{Goal, QueryInput};
7use rustc_type_ir::visit::TypeVisitableExt;
8use rustc_type_ir::{
9    self as ty, Canonical, CanonicalTyVarKind, CanonicalVarInfo, CanonicalVarKind, InferCtxtLike,
10    Interner,
11};
12
13use crate::delegate::SolverDelegate;
14
15/// Whether we're canonicalizing a query input or the query response.
16///
17/// When canonicalizing an input we're in the context of the caller
18/// while canonicalizing the response happens in the context of the
19/// query.
20#[derive(Debug, Clone, Copy)]
21enum CanonicalizeMode {
22    /// When canonicalizing the `param_env`, we keep `'static` as merging
23    /// trait candidates relies on it when deciding whether a where-bound
24    /// is trivial.
25    Input { keep_static: bool },
26    /// FIXME: We currently return region constraints referring to
27    /// placeholders and inference variables from a binder instantiated
28    /// inside of the query.
29    ///
30    /// In the long term we should eagerly deal with these constraints
31    /// inside of the query and only propagate constraints which are
32    /// actually nameable by the caller.
33    Response {
34        /// The highest universe nameable by the caller.
35        ///
36        /// All variables in a universe nameable by the caller get mapped
37        /// to the root universe in the response and then mapped back to
38        /// their correct universe when applying the query response in the
39        /// context of the caller.
40        ///
41        /// This doesn't work for universes created inside of the query so
42        /// we do remember their universe in the response.
43        max_input_universe: ty::UniverseIndex,
44    },
45}
46
47pub struct Canonicalizer<'a, D: SolverDelegate<Interner = I>, I: Interner> {
48    delegate: &'a D,
49
50    // Immutable field.
51    canonicalize_mode: CanonicalizeMode,
52
53    // Mutable fields.
54    variables: &'a mut Vec<I::GenericArg>,
55    primitive_var_infos: Vec<CanonicalVarInfo<I>>,
56    variable_lookup_table: HashMap<I::GenericArg, usize>,
57    binder_index: ty::DebruijnIndex,
58
59    /// We only use the debruijn index during lookup. We don't need to
60    /// track the `variables` as each generic arg only results in a single
61    /// bound variable regardless of how many times it is encountered.
62    cache: HashMap<(ty::DebruijnIndex, I::Ty), I::Ty>,
63}
64
65impl<'a, D: SolverDelegate<Interner = I>, I: Interner> Canonicalizer<'a, D, I> {
66    pub fn canonicalize_response<T: TypeFoldable<I>>(
67        delegate: &'a D,
68        max_input_universe: ty::UniverseIndex,
69        variables: &'a mut Vec<I::GenericArg>,
70        value: T,
71    ) -> ty::Canonical<I, T> {
72        let mut canonicalizer = Canonicalizer {
73            delegate,
74            canonicalize_mode: CanonicalizeMode::Response { max_input_universe },
75
76            variables,
77            variable_lookup_table: Default::default(),
78            primitive_var_infos: Vec::new(),
79            binder_index: ty::INNERMOST,
80
81            cache: Default::default(),
82        };
83
84        let value = value.fold_with(&mut canonicalizer);
85        assert!(!value.has_infer(), "unexpected infer in {value:?}");
86        assert!(!value.has_placeholders(), "unexpected placeholders in {value:?}");
87        let (max_universe, variables) = canonicalizer.finalize();
88        Canonical { max_universe, variables, value }
89    }
90
91    /// When canonicalizing query inputs, we keep `'static` in the `param_env`
92    /// but erase it everywhere else. We generally don't want to depend on region
93    /// identity, so while it should not matter whether `'static` is kept in the
94    /// value or opaque type storage as well, this prevents us from accidentally
95    /// relying on it in the future.
96    ///
97    /// We want to keep the option of canonicalizing `'static` to an existential
98    /// variable in the future by changing the way we detect global where-bounds.
99    pub fn canonicalize_input<P: TypeFoldable<I>>(
100        delegate: &'a D,
101        variables: &'a mut Vec<I::GenericArg>,
102        input: QueryInput<I, P>,
103    ) -> ty::Canonical<I, QueryInput<I, P>> {
104        // First canonicalize the `param_env` while keeping `'static`
105        let mut env_canonicalizer = Canonicalizer {
106            delegate,
107            canonicalize_mode: CanonicalizeMode::Input { keep_static: true },
108
109            variables,
110            variable_lookup_table: Default::default(),
111            primitive_var_infos: Vec::new(),
112            binder_index: ty::INNERMOST,
113
114            cache: Default::default(),
115        };
116        let param_env = input.goal.param_env.fold_with(&mut env_canonicalizer);
117        debug_assert_eq!(env_canonicalizer.binder_index, ty::INNERMOST);
118        // Then canonicalize the rest of the input without keeping `'static`
119        // while *mostly* reusing the canonicalizer from above.
120        let mut rest_canonicalizer = Canonicalizer {
121            delegate,
122            canonicalize_mode: CanonicalizeMode::Input { keep_static: false },
123
124            variables: env_canonicalizer.variables,
125            // We're able to reuse the `variable_lookup_table` as whether or not
126            // it already contains an entry for `'static` does not matter.
127            variable_lookup_table: env_canonicalizer.variable_lookup_table,
128            primitive_var_infos: env_canonicalizer.primitive_var_infos,
129            binder_index: ty::INNERMOST,
130
131            // We do not reuse the cache as it may contain entries whose canonicalized
132            // value contains `'static`. While we could alternatively handle this by
133            // checking for `'static` when using cached entries, this does not
134            // feel worth the effort. I do not expect that a `ParamEnv` will ever
135            // contain large enough types for caching to be necessary.
136            cache: Default::default(),
137        };
138
139        let predicate = input.goal.predicate.fold_with(&mut rest_canonicalizer);
140        let goal = Goal { param_env, predicate };
141        let predefined_opaques_in_body =
142            input.predefined_opaques_in_body.fold_with(&mut rest_canonicalizer);
143        let value = QueryInput { goal, predefined_opaques_in_body };
144
145        assert!(!value.has_infer(), "unexpected infer in {value:?}");
146        assert!(!value.has_placeholders(), "unexpected placeholders in {value:?}");
147        let (max_universe, variables) = rest_canonicalizer.finalize();
148        Canonical { max_universe, variables, value }
149    }
150
151    fn get_or_insert_bound_var(
152        &mut self,
153        arg: impl Into<I::GenericArg>,
154        canonical_var_info: CanonicalVarInfo<I>,
155    ) -> ty::BoundVar {
156        // FIXME: 16 is made up and arbitrary. We should look at some
157        // perf data here.
158        let arg = arg.into();
159        let idx = if self.variables.len() > 16 {
160            if self.variable_lookup_table.is_empty() {
161                self.variable_lookup_table.extend(self.variables.iter().copied().zip(0..));
162            }
163
164            *self.variable_lookup_table.entry(arg).or_insert_with(|| {
165                let var = self.variables.len();
166                self.variables.push(arg);
167                self.primitive_var_infos.push(canonical_var_info);
168                var
169            })
170        } else {
171            self.variables.iter().position(|&v| v == arg).unwrap_or_else(|| {
172                let var = self.variables.len();
173                self.variables.push(arg);
174                self.primitive_var_infos.push(canonical_var_info);
175                var
176            })
177        };
178
179        ty::BoundVar::from(idx)
180    }
181
182    fn finalize(self) -> (ty::UniverseIndex, I::CanonicalVars) {
183        let mut var_infos = self.primitive_var_infos;
184        // See the rustc-dev-guide section about how we deal with universes
185        // during canonicalization in the new solver.
186        match self.canonicalize_mode {
187            // We try to deduplicate as many query calls as possible and hide
188            // all information which should not matter for the solver.
189            //
190            // For this we compress universes as much as possible.
191            CanonicalizeMode::Input { .. } => {}
192            // When canonicalizing a response we map a universes already entered
193            // by the caller to the root universe and only return useful universe
194            // information for placeholders and inference variables created inside
195            // of the query.
196            CanonicalizeMode::Response { max_input_universe } => {
197                for var in var_infos.iter_mut() {
198                    let uv = var.universe();
199                    let new_uv = ty::UniverseIndex::from(
200                        uv.index().saturating_sub(max_input_universe.index()),
201                    );
202                    *var = var.with_updated_universe(new_uv);
203                }
204                let max_universe = var_infos
205                    .iter()
206                    .map(|info| info.universe())
207                    .max()
208                    .unwrap_or(ty::UniverseIndex::ROOT);
209
210                let var_infos = self.delegate.cx().mk_canonical_var_infos(&var_infos);
211                return (max_universe, var_infos);
212            }
213        }
214
215        // Given a `var_infos` with existentials `En` and universals `Un` in
216        // universes `n`, this algorithm compresses them in place so that:
217        //
218        // - the new universe indices are as small as possible
219        // - we create a new universe if we would otherwise
220        //   1. put existentials from a different universe into the same one
221        //   2. put a placeholder in the same universe as an existential which cannot name it
222        //
223        // Let's walk through an example:
224        // - var_infos: [E0, U1, E5, U2, E2, E6, U6], curr_compressed_uv: 0, next_orig_uv: 0
225        // - var_infos: [E0, U1, E5, U2, E2, E6, U6], curr_compressed_uv: 0, next_orig_uv: 1
226        // - var_infos: [E0, U1, E5, U2, E2, E6, U6], curr_compressed_uv: 1, next_orig_uv: 2
227        // - var_infos: [E0, U1, E5, U1, E1, E6, U6], curr_compressed_uv: 1, next_orig_uv: 5
228        // - var_infos: [E0, U1, E2, U1, E1, E6, U6], curr_compressed_uv: 2, next_orig_uv: 6
229        // - var_infos: [E0, U1, E1, U1, E1, E3, U3], curr_compressed_uv: 2, next_orig_uv: -
230        //
231        // This algorithm runs in `O(mn)` where `n` is the number of different universes and
232        // `m` the number of variables. This should be fine as both are expected to be small.
233        let mut curr_compressed_uv = ty::UniverseIndex::ROOT;
234        let mut existential_in_new_uv = None;
235        let mut next_orig_uv = Some(ty::UniverseIndex::ROOT);
236        while let Some(orig_uv) = next_orig_uv.take() {
237            let mut update_uv = |var: &mut CanonicalVarInfo<I>, orig_uv, is_existential| {
238                let uv = var.universe();
239                match uv.cmp(&orig_uv) {
240                    Ordering::Less => (), // Already updated
241                    Ordering::Equal => {
242                        if is_existential {
243                            if existential_in_new_uv.is_some_and(|uv| uv < orig_uv) {
244                                // Condition 1.
245                                //
246                                // We already put an existential from a outer universe
247                                // into the current compressed universe, so we need to
248                                // create a new one.
249                                curr_compressed_uv = curr_compressed_uv.next_universe();
250                            }
251
252                            // `curr_compressed_uv` will now contain an existential from
253                            // `orig_uv`. Trying to canonicalizing an existential from
254                            // a higher universe has to therefore use a new compressed
255                            // universe.
256                            existential_in_new_uv = Some(orig_uv);
257                        } else if existential_in_new_uv.is_some() {
258                            // Condition 2.
259                            //
260                            //  `var` is a placeholder from a universe which is not nameable
261                            // by an existential which we already put into the compressed
262                            // universe `curr_compressed_uv`. We therefore have to create a
263                            // new universe for `var`.
264                            curr_compressed_uv = curr_compressed_uv.next_universe();
265                            existential_in_new_uv = None;
266                        }
267
268                        *var = var.with_updated_universe(curr_compressed_uv);
269                    }
270                    Ordering::Greater => {
271                        // We can ignore this variable in this iteration. We only look at
272                        // universes which actually occur in the input for performance.
273                        //
274                        // For this we set `next_orig_uv` to the next smallest, not yet compressed,
275                        // universe of the input.
276                        if next_orig_uv.is_none_or(|curr_next_uv| uv.cannot_name(curr_next_uv)) {
277                            next_orig_uv = Some(uv);
278                        }
279                    }
280                }
281            };
282
283            // For each universe which occurs in the input, we first iterate over all
284            // placeholders and then over all inference variables.
285            //
286            // Whenever we compress the universe of a placeholder, no existential with
287            // an already compressed universe can name that placeholder.
288            for is_existential in [false, true] {
289                for var in var_infos.iter_mut() {
290                    // We simply put all regions from the input into the highest
291                    // compressed universe, so we only deal with them at the end.
292                    if !var.is_region() {
293                        if is_existential == var.is_existential() {
294                            update_uv(var, orig_uv, is_existential)
295                        }
296                    }
297                }
298            }
299        }
300
301        // We put all regions into a separate universe.
302        let mut first_region = true;
303        for var in var_infos.iter_mut() {
304            if var.is_region() {
305                if first_region {
306                    first_region = false;
307                    curr_compressed_uv = curr_compressed_uv.next_universe();
308                }
309                assert!(var.is_existential());
310                *var = var.with_updated_universe(curr_compressed_uv);
311            }
312        }
313
314        let var_infos = self.delegate.cx().mk_canonical_var_infos(&var_infos);
315        (curr_compressed_uv, var_infos)
316    }
317
318    fn cached_fold_ty(&mut self, t: I::Ty) -> I::Ty {
319        let kind = match t.kind() {
320            ty::Infer(i) => match i {
321                ty::TyVar(vid) => {
322                    assert_eq!(
323                        self.delegate.opportunistic_resolve_ty_var(vid),
324                        t,
325                        "ty vid should have been resolved fully before canonicalization"
326                    );
327
328                    CanonicalVarKind::Ty(CanonicalTyVarKind::General(
329                        self.delegate
330                            .universe_of_ty(vid)
331                            .unwrap_or_else(|| panic!("ty var should have been resolved: {t:?}")),
332                    ))
333                }
334                ty::IntVar(vid) => {
335                    assert_eq!(
336                        self.delegate.opportunistic_resolve_int_var(vid),
337                        t,
338                        "ty vid should have been resolved fully before canonicalization"
339                    );
340                    CanonicalVarKind::Ty(CanonicalTyVarKind::Int)
341                }
342                ty::FloatVar(vid) => {
343                    assert_eq!(
344                        self.delegate.opportunistic_resolve_float_var(vid),
345                        t,
346                        "ty vid should have been resolved fully before canonicalization"
347                    );
348                    CanonicalVarKind::Ty(CanonicalTyVarKind::Float)
349                }
350                ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_) => {
351                    panic!("fresh vars not expected in canonicalization")
352                }
353            },
354            ty::Placeholder(placeholder) => match self.canonicalize_mode {
355                CanonicalizeMode::Input { .. } => CanonicalVarKind::PlaceholderTy(
356                    PlaceholderLike::new(placeholder.universe(), self.variables.len().into()),
357                ),
358                CanonicalizeMode::Response { .. } => CanonicalVarKind::PlaceholderTy(placeholder),
359            },
360            ty::Param(_) => match self.canonicalize_mode {
361                CanonicalizeMode::Input { .. } => CanonicalVarKind::PlaceholderTy(
362                    PlaceholderLike::new(ty::UniverseIndex::ROOT, self.variables.len().into()),
363                ),
364                CanonicalizeMode::Response { .. } => panic!("param ty in response: {t:?}"),
365            },
366            ty::Bool
367            | ty::Char
368            | ty::Int(_)
369            | ty::Uint(_)
370            | ty::Float(_)
371            | ty::Adt(_, _)
372            | ty::Foreign(_)
373            | ty::Str
374            | ty::Array(_, _)
375            | ty::Slice(_)
376            | ty::RawPtr(_, _)
377            | ty::Ref(_, _, _)
378            | ty::Pat(_, _)
379            | ty::FnDef(_, _)
380            | ty::FnPtr(..)
381            | ty::UnsafeBinder(_)
382            | ty::Dynamic(_, _, _)
383            | ty::Closure(..)
384            | ty::CoroutineClosure(..)
385            | ty::Coroutine(_, _)
386            | ty::CoroutineWitness(..)
387            | ty::Never
388            | ty::Tuple(_)
389            | ty::Alias(_, _)
390            | ty::Bound(_, _)
391            | ty::Error(_) => {
392                return t.super_fold_with(self);
393            }
394        };
395
396        let var = self.get_or_insert_bound_var(t, CanonicalVarInfo { kind });
397
398        Ty::new_anon_bound(self.cx(), self.binder_index, var)
399    }
400}
401
402impl<D: SolverDelegate<Interner = I>, I: Interner> TypeFolder<I> for Canonicalizer<'_, D, I> {
403    fn cx(&self) -> I {
404        self.delegate.cx()
405    }
406
407    fn fold_binder<T>(&mut self, t: ty::Binder<I, T>) -> ty::Binder<I, T>
408    where
409        T: TypeFoldable<I>,
410    {
411        self.binder_index.shift_in(1);
412        let t = t.super_fold_with(self);
413        self.binder_index.shift_out(1);
414        t
415    }
416
417    fn fold_region(&mut self, r: I::Region) -> I::Region {
418        let kind = match r.kind() {
419            ty::ReBound(..) => return r,
420
421            // We don't canonicalize `ReStatic` in the `param_env` as we use it
422            // when checking whether a `ParamEnv` candidate is global.
423            ty::ReStatic => match self.canonicalize_mode {
424                CanonicalizeMode::Input { keep_static: false } => {
425                    CanonicalVarKind::Region(ty::UniverseIndex::ROOT)
426                }
427                CanonicalizeMode::Input { keep_static: true }
428                | CanonicalizeMode::Response { .. } => return r,
429            },
430
431            // `ReErased` should only be encountered in the hidden
432            // type of an opaque for regions that are ignored for the purposes of
433            // captures.
434            //
435            // FIXME: We should investigate the perf implications of not uniquifying
436            // `ReErased`. We may be able to short-circuit registering region
437            // obligations if we encounter a `ReErased` on one side, for example.
438            ty::ReErased | ty::ReError(_) => match self.canonicalize_mode {
439                CanonicalizeMode::Input { .. } => CanonicalVarKind::Region(ty::UniverseIndex::ROOT),
440                CanonicalizeMode::Response { .. } => return r,
441            },
442
443            ty::ReEarlyParam(_) | ty::ReLateParam(_) => match self.canonicalize_mode {
444                CanonicalizeMode::Input { .. } => CanonicalVarKind::Region(ty::UniverseIndex::ROOT),
445                CanonicalizeMode::Response { .. } => {
446                    panic!("unexpected region in response: {r:?}")
447                }
448            },
449
450            ty::RePlaceholder(placeholder) => match self.canonicalize_mode {
451                // We canonicalize placeholder regions as existentials in query inputs.
452                CanonicalizeMode::Input { .. } => CanonicalVarKind::Region(ty::UniverseIndex::ROOT),
453                CanonicalizeMode::Response { max_input_universe } => {
454                    // If we have a placeholder region inside of a query, it must be from
455                    // a new universe.
456                    if max_input_universe.can_name(placeholder.universe()) {
457                        panic!("new placeholder in universe {max_input_universe:?}: {r:?}");
458                    }
459                    CanonicalVarKind::PlaceholderRegion(placeholder)
460                }
461            },
462
463            ty::ReVar(vid) => {
464                assert_eq!(
465                    self.delegate.opportunistic_resolve_lt_var(vid),
466                    r,
467                    "region vid should have been resolved fully before canonicalization"
468                );
469                match self.canonicalize_mode {
470                    CanonicalizeMode::Input { keep_static: _ } => {
471                        CanonicalVarKind::Region(ty::UniverseIndex::ROOT)
472                    }
473                    CanonicalizeMode::Response { .. } => {
474                        CanonicalVarKind::Region(self.delegate.universe_of_lt(vid).unwrap())
475                    }
476                }
477            }
478        };
479
480        let var = self.get_or_insert_bound_var(r, CanonicalVarInfo { kind });
481
482        Region::new_anon_bound(self.cx(), self.binder_index, var)
483    }
484
485    fn fold_ty(&mut self, t: I::Ty) -> I::Ty {
486        if let Some(&ty) = self.cache.get(&(self.binder_index, t)) {
487            ty
488        } else {
489            let res = self.cached_fold_ty(t);
490            assert!(self.cache.insert((self.binder_index, t), res).is_none());
491            res
492        }
493    }
494
495    fn fold_const(&mut self, c: I::Const) -> I::Const {
496        let kind = match c.kind() {
497            ty::ConstKind::Infer(i) => match i {
498                ty::InferConst::Var(vid) => {
499                    assert_eq!(
500                        self.delegate.opportunistic_resolve_ct_var(vid),
501                        c,
502                        "const vid should have been resolved fully before canonicalization"
503                    );
504                    CanonicalVarKind::Const(self.delegate.universe_of_ct(vid).unwrap())
505                }
506                ty::InferConst::Fresh(_) => todo!(),
507            },
508            ty::ConstKind::Placeholder(placeholder) => match self.canonicalize_mode {
509                CanonicalizeMode::Input { .. } => CanonicalVarKind::PlaceholderConst(
510                    PlaceholderLike::new(placeholder.universe(), self.variables.len().into()),
511                ),
512                CanonicalizeMode::Response { .. } => {
513                    CanonicalVarKind::PlaceholderConst(placeholder)
514                }
515            },
516            ty::ConstKind::Param(_) => match self.canonicalize_mode {
517                CanonicalizeMode::Input { .. } => CanonicalVarKind::PlaceholderConst(
518                    PlaceholderLike::new(ty::UniverseIndex::ROOT, self.variables.len().into()),
519                ),
520                CanonicalizeMode::Response { .. } => panic!("param ty in response: {c:?}"),
521            },
522            // FIXME: See comment above -- we could fold the region separately or something.
523            ty::ConstKind::Bound(_, _)
524            | ty::ConstKind::Unevaluated(_)
525            | ty::ConstKind::Value(_)
526            | ty::ConstKind::Error(_)
527            | ty::ConstKind::Expr(_) => return c.super_fold_with(self),
528        };
529
530        let var = self.get_or_insert_bound_var(c, CanonicalVarInfo { kind });
531
532        Const::new_anon_bound(self.cx(), self.binder_index, var)
533    }
534}