hir_ty/next_solver/infer/
resolve.rs

1//! Things for resolving vars in the infer context of the next-trait-solver.
2
3use rustc_type_ir::{
4    ConstKind, FallibleTypeFolder, InferConst, InferTy, RegionKind, TyKind, TypeFoldable,
5    TypeFolder, TypeSuperFoldable, TypeVisitableExt, data_structures::DelayedMap,
6    inherent::IntoKind,
7};
8
9use crate::next_solver::{Const, DbInterner, Region, Ty};
10
11use super::{FixupError, FixupResult, InferCtxt};
12
13///////////////////////////////////////////////////////////////////////////
14// OPPORTUNISTIC VAR RESOLVER
15
16/// The opportunistic resolver can be used at any time. It simply replaces
17/// type/const variables that have been unified with the things they have
18/// been unified with (similar to `shallow_resolve`, but deep). This is
19/// useful for printing messages etc but also required at various
20/// points for correctness.
21pub struct OpportunisticVarResolver<'a, 'db> {
22    infcx: &'a InferCtxt<'db>,
23    /// We're able to use a cache here as the folder does
24    /// not have any mutable state.
25    cache: DelayedMap<Ty<'db>, Ty<'db>>,
26}
27
28impl<'a, 'db> OpportunisticVarResolver<'a, 'db> {
29    #[inline]
30    pub fn new(infcx: &'a InferCtxt<'db>) -> Self {
31        OpportunisticVarResolver { infcx, cache: Default::default() }
32    }
33}
34
35impl<'a, 'db> TypeFolder<DbInterner<'db>> for OpportunisticVarResolver<'a, 'db> {
36    fn cx(&self) -> DbInterner<'db> {
37        self.infcx.interner
38    }
39
40    #[inline]
41    fn fold_ty(&mut self, t: Ty<'db>) -> Ty<'db> {
42        if !t.has_non_region_infer() {
43            t // micro-optimize -- if there is nothing in this type that this fold affects...
44        } else if let Some(ty) = self.cache.get(&t) {
45            *ty
46        } else {
47            let shallow = self.infcx.shallow_resolve(t);
48            let res = shallow.super_fold_with(self);
49            assert!(self.cache.insert(t, res));
50            res
51        }
52    }
53
54    fn fold_const(&mut self, ct: Const<'db>) -> Const<'db> {
55        if !ct.has_non_region_infer() {
56            ct // micro-optimize -- if there is nothing in this const that this fold affects...
57        } else {
58            let ct = self.infcx.shallow_resolve_const(ct);
59            ct.super_fold_with(self)
60        }
61    }
62}