Unnamed repository; edit this file 'description' to name the repository.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//! Things for resolving vars in the infer context of the next-trait-solver.

use rustc_type_ir::{
    TypeFolder, TypeSuperFoldable, TypeVisitableExt,
    data_structures::DelayedMap,
    inherent::{Const as _, Ty as _},
};

use crate::next_solver::{Const, DbInterner, ErrorGuaranteed, Region, Ty};

use super::InferCtxt;

///////////////////////////////////////////////////////////////////////////
// OPPORTUNISTIC VAR RESOLVER

/// The opportunistic resolver can be used at any time. It simply replaces
/// type/const variables that have been unified with the things they have
/// been unified with (similar to `shallow_resolve`, but deep). This is
/// useful for printing messages etc but also required at various
/// points for correctness.
pub struct OpportunisticVarResolver<'a, 'db> {
    infcx: &'a InferCtxt<'db>,
    /// We're able to use a cache here as the folder does
    /// not have any mutable state.
    cache: DelayedMap<Ty<'db>, Ty<'db>>,
}

impl<'a, 'db> OpportunisticVarResolver<'a, 'db> {
    #[inline]
    pub fn new(infcx: &'a InferCtxt<'db>) -> Self {
        OpportunisticVarResolver { infcx, cache: Default::default() }
    }
}

impl<'a, 'db> TypeFolder<DbInterner<'db>> for OpportunisticVarResolver<'a, 'db> {
    fn cx(&self) -> DbInterner<'db> {
        self.infcx.interner
    }

    #[inline]
    fn fold_ty(&mut self, t: Ty<'db>) -> Ty<'db> {
        if !t.has_non_region_infer() {
            t // micro-optimize -- if there is nothing in this type that this fold affects...
        } else if let Some(ty) = self.cache.get(&t) {
            *ty
        } else {
            let shallow = self.infcx.shallow_resolve(t);
            let res = shallow.super_fold_with(self);
            assert!(self.cache.insert(t, res));
            res
        }
    }

    fn fold_const(&mut self, ct: Const<'db>) -> Const<'db> {
        if !ct.has_non_region_infer() {
            ct // micro-optimize -- if there is nothing in this const that this fold affects...
        } else {
            let ct = self.infcx.shallow_resolve_const(ct);
            ct.super_fold_with(self)
        }
    }
}

pub struct ReplaceInferWithError<'db> {
    interner: DbInterner<'db>,
}

impl<'db> ReplaceInferWithError<'db> {
    #[inline]
    pub fn new(interner: DbInterner<'db>) -> Self {
        Self { interner }
    }
}

impl<'db> TypeFolder<DbInterner<'db>> for ReplaceInferWithError<'db> {
    fn cx(&self) -> DbInterner<'db> {
        self.interner
    }

    fn fold_ty(&mut self, t: Ty<'db>) -> Ty<'db> {
        if !t.has_infer() {
            return t;
        }

        if t.is_infer() {
            Ty::new_error(self.interner, ErrorGuaranteed)
        } else {
            t.super_fold_with(self)
        }
    }

    fn fold_const(&mut self, c: Const<'db>) -> Const<'db> {
        if !c.has_infer() {
            return c;
        }

        if c.is_ct_infer() {
            Const::new_error(self.interner, ErrorGuaranteed)
        } else {
            c.super_fold_with(self)
        }
    }

    fn fold_region(&mut self, r: Region<'db>) -> Region<'db> {
        if r.is_var() { Region::error(self.interner) } else { r }
    }
}