resolve.rs 8.8 KB
Newer Older
1
use super::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
M
Mark Rousskov 已提交
2
use super::{FixupError, FixupResult, InferCtxt, Span};
M
Mazdak Farrokhzad 已提交
3 4
use rustc_middle::ty::fold::{TypeFolder, TypeVisitor};
use rustc_middle::ty::{self, Const, InferConst, Ty, TyCtxt, TypeFoldable};
5

6 7
use std::ops::ControlFlow;

8
///////////////////////////////////////////////////////////////////////////
V
varkor 已提交
9
// OPPORTUNISTIC VAR RESOLVER
10

V
varkor 已提交
11
/// The opportunistic resolver can be used at any time. It simply replaces
12
/// type/const variables that have been unified with the things they have
V
varkor 已提交
13
/// been unified with (similar to `shallow_resolve`, but deep). This is
14 15
/// useful for printing messages etc but also required at various
/// points for correctness.
16
pub struct OpportunisticVarResolver<'a, 'tcx> {
17
    infcx: &'a InferCtxt<'a, 'tcx>,
18 19
}

20
impl<'a, 'tcx> OpportunisticVarResolver<'a, 'tcx> {
21
    #[inline]
22
    pub fn new(infcx: &'a InferCtxt<'a, 'tcx>) -> Self {
23
        OpportunisticVarResolver { infcx }
24
    }
25 26
}

27 28
impl<'a, 'tcx> TypeFolder<'tcx> for OpportunisticVarResolver<'a, 'tcx> {
    fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
29 30 31
        self.infcx.tcx
    }

32
    fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
M
Matthew Jasper 已提交
33
        if !t.has_infer_types_or_consts() {
34 35
            t // micro-optimize -- if there is nothing in this type that this fold affects...
        } else {
36 37 38 39 40 41
            let t = self.infcx.shallow_resolve(t);
            t.super_fold_with(self)
        }
    }

    fn fold_const(&mut self, ct: &'tcx Const<'tcx>) -> &'tcx Const<'tcx> {
M
Matthew Jasper 已提交
42
        if !ct.has_infer_types_or_consts() {
43 44 45 46
            ct // micro-optimize -- if there is nothing in this const that this fold affects...
        } else {
            let ct = self.infcx.shallow_resolve(ct);
            ct.super_fold_with(self)
47 48
        }
    }
49
}
50

51 52 53 54 55 56 57 58
/// The opportunistic region resolver opportunistically resolves regions
/// variables to the variable with the least variable id. It is used when
/// normlizing projections to avoid hitting the recursion limit by creating
/// many versions of a predicate for types that in the end have to unify.
///
/// If you want to resolve type and const variables as well, call
/// [InferCtxt::resolve_vars_if_possible] first.
pub struct OpportunisticRegionResolver<'a, 'tcx> {
59
    infcx: &'a InferCtxt<'a, 'tcx>,
60 61
}

62
impl<'a, 'tcx> OpportunisticRegionResolver<'a, 'tcx> {
63
    pub fn new(infcx: &'a InferCtxt<'a, 'tcx>) -> Self {
64
        OpportunisticRegionResolver { infcx }
65 66 67
    }
}

68
impl<'a, 'tcx> TypeFolder<'tcx> for OpportunisticRegionResolver<'a, 'tcx> {
69
    fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
70 71 72 73
        self.infcx.tcx
    }

    fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
74
        if !t.has_infer_regions() {
75 76
            t // micro-optimize -- if there is nothing in this type that this fold affects...
        } else {
77
            t.super_fold_with(self)
78 79 80
        }
    }

N
Niko Matsakis 已提交
81
    fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
82
        match *r {
83 84 85 86 87 88 89
            ty::ReVar(rid) => {
                let resolved = self
                    .infcx
                    .inner
                    .borrow_mut()
                    .unwrap_region_constraints()
                    .opportunistic_resolve_var(rid);
90
                self.tcx().reuse_or_mk_region(r, ty::ReVar(resolved))
91
            }
M
Mark Rousskov 已提交
92
            _ => r,
93 94
        }
    }
95

V
varkor 已提交
96
    fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
97
        if !ct.has_infer_regions() {
98 99
            ct // micro-optimize -- if there is nothing in this const that this fold affects...
        } else {
100
            ct.super_fold_with(self)
101 102
        }
    }
103 104
}

105 106 107
///////////////////////////////////////////////////////////////////////////
// UNRESOLVED TYPE FINDER

108 109 110
/// The unresolved type **finder** walks a type searching for
/// type variables that don't yet have a value. The first unresolved type is stored.
/// It does not construct the fully resolved type (which might
111
/// involve some hashing and so forth).
112
pub struct UnresolvedTypeFinder<'a, 'tcx> {
113
    infcx: &'a InferCtxt<'a, 'tcx>,
114 115

    /// Used to find the type parameter name and location for error reporting.
116
    pub first_unresolved: Option<(Ty<'tcx>, Option<Span>)>,
117 118
}

119 120
impl<'a, 'tcx> UnresolvedTypeFinder<'a, 'tcx> {
    pub fn new(infcx: &'a InferCtxt<'a, 'tcx>) -> Self {
121
        UnresolvedTypeFinder { infcx, first_unresolved: None }
122 123 124
    }
}

125
impl<'a, 'tcx> TypeVisitor<'tcx> for UnresolvedTypeFinder<'a, 'tcx> {
L
LeSeulArtichaut 已提交
126
    fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<()> {
V
varkor 已提交
127
        let t = self.infcx.shallow_resolve(t);
128
        if t.has_infer_types() {
L
LeSeulArtichaut 已提交
129
            if let ty::Infer(infer_ty) = *t.kind() {
130 131
                // Since we called `shallow_resolve` above, this must
                // be an (as yet...) unresolved inference variable.
M
Mark Rousskov 已提交
132
                let ty_var_span = if let ty::TyVar(ty_vid) = infer_ty {
133 134
                    let mut inner = self.infcx.inner.borrow_mut();
                    let ty_vars = &inner.type_variables();
135
                    if let TypeVariableOrigin {
136
                        kind: TypeVariableOriginKind::TypeParameterDefinition(_, _),
137 138
                        span,
                    } = *ty_vars.var_origin(ty_vid)
139 140 141 142 143 144 145 146 147
                    {
                        Some(span)
                    } else {
                        None
                    }
                } else {
                    None
                };
                self.first_unresolved = Some((t, ty_var_span));
148
                ControlFlow::BREAK
149 150 151 152 153
            } else {
                // Otherwise, visit its contents.
                t.super_visit_with(self)
            }
        } else {
154 155
            // All type variables in inference types must already be resolved,
            // - no need to visit the contents, continue visiting.
156
            ControlFlow::CONTINUE
157 158 159 160
        }
    }
}

161
///////////////////////////////////////////////////////////////////////////
162 163 164 165 166
// FULL TYPE RESOLUTION

/// Full type resolution replaces all type and region variables with
/// their concrete results. If any variable cannot be replaced (never unified, etc)
/// then an `Err` result is returned.
167 168 169
pub fn fully_resolve<'a, 'tcx, T>(infcx: &InferCtxt<'a, 'tcx>, value: &T) -> FixupResult<'tcx, T>
where
    T: TypeFoldable<'tcx>,
170
{
171
    let mut full_resolver = FullTypeResolver { infcx, err: None };
172 173 174 175 176
    let result = value.fold_with(&mut full_resolver);
    match full_resolver.err {
        None => Ok(result),
        Some(e) => Err(e),
    }
177 178
}

179
// N.B. This type is not public because the protocol around checking the
M
Matthias Krüger 已提交
180
// `err` field is not enforceable otherwise.
181
struct FullTypeResolver<'a, 'tcx> {
182
    infcx: &'a InferCtxt<'a, 'tcx>,
183
    err: Option<FixupError<'tcx>>,
184 185
}

186 187
impl<'a, 'tcx> TypeFolder<'tcx> for FullTypeResolver<'a, 'tcx> {
    fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
188 189 190 191
        self.infcx.tcx
    }

    fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
192
        if !t.needs_infer() {
193 194
            t // micro-optimize -- if there is nothing in this type that this fold affects...
        } else {
V
varkor 已提交
195
            let t = self.infcx.shallow_resolve(t);
L
LeSeulArtichaut 已提交
196
            match *t.kind() {
V
varkor 已提交
197
                ty::Infer(ty::TyVar(vid)) => {
J
Jared Roesch 已提交
198
                    self.err = Some(FixupError::UnresolvedTy(vid));
199
                    self.tcx().ty_error()
200
                }
V
varkor 已提交
201
                ty::Infer(ty::IntVar(vid)) => {
J
Jared Roesch 已提交
202
                    self.err = Some(FixupError::UnresolvedIntTy(vid));
203
                    self.tcx().ty_error()
204
                }
V
varkor 已提交
205
                ty::Infer(ty::FloatVar(vid)) => {
J
Jared Roesch 已提交
206
                    self.err = Some(FixupError::UnresolvedFloatTy(vid));
207
                    self.tcx().ty_error()
208
                }
V
varkor 已提交
209
                ty::Infer(_) => {
210
                    bug!("Unexpected type in full type resolver: {:?}", t);
211
                }
M
Mark Rousskov 已提交
212
                _ => t.super_fold_with(self),
213
            }
214 215 216
        }
    }

N
Niko Matsakis 已提交
217
    fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
218
        match *r {
M
Mark Rousskov 已提交
219 220 221 222 223 224 225
            ty::ReVar(rid) => self
                .infcx
                .lexical_region_resolutions
                .borrow()
                .as_ref()
                .expect("region resolution not performed")
                .resolve_var(rid),
226
            _ => r,
227 228
        }
    }
229

V
varkor 已提交
230
    fn fold_const(&mut self, c: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
231
        if !c.needs_infer() {
232 233
            c // micro-optimize -- if there is nothing in this const that this fold affects...
        } else {
V
varkor 已提交
234
            let c = self.infcx.shallow_resolve(c);
V
varkor 已提交
235
            match c.val {
236
                ty::ConstKind::Infer(InferConst::Var(vid)) => {
V
varkor 已提交
237
                    self.err = Some(FixupError::UnresolvedConst(vid));
238
                    return self.tcx().const_error(c.ty);
V
varkor 已提交
239
                }
240
                ty::ConstKind::Infer(InferConst::Fresh(_)) => {
V
varkor 已提交
241
                    bug!("Unexpected const in full const resolver: {:?}", c);
242 243 244 245 246 247
                }
                _ => {}
            }
            c.super_fold_with(self)
        }
    }
248
}