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
108
109
110
111
112
113
114
use super::{InferCtxt, fixup_err, fres, unresolved_ty, unresolved_int_ty, unresolved_float_ty};
use middle::ty::{self, Ty};
use middle::ty_fold::{self, TypeFoldable};
use util::ppaux::Repr;
pub struct OpportunisticTypeResolver<'a, 'tcx:'a> {
infcx: &'a InferCtxt<'a, 'tcx>,
}
impl<'a, 'tcx> OpportunisticTypeResolver<'a, 'tcx> {
pub fn new(infcx: &'a InferCtxt<'a, 'tcx>) -> OpportunisticTypeResolver<'a, 'tcx> {
OpportunisticTypeResolver { infcx: infcx }
}
}
impl<'a, 'tcx> ty_fold::TypeFolder<'tcx> for OpportunisticTypeResolver<'a, 'tcx> {
fn tcx(&self) -> &ty::ctxt<'tcx> {
self.infcx.tcx
}
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
if !ty::type_has_ty_infer(t) {
t
} else {
let t0 = self.infcx.shallow_resolve(t);
ty_fold::super_fold_ty(self, t0)
}
}
}
pub fn fully_resolve<'a, 'tcx, T>(infcx: &InferCtxt<'a,'tcx>, value: &T) -> fres<T>
where T : TypeFoldable<'tcx>
{
let mut full_resolver = FullTypeResolver { infcx: infcx, err: None };
let result = value.fold_with(&mut full_resolver);
match full_resolver.err {
None => Ok(result),
Some(e) => Err(e),
}
}
struct FullTypeResolver<'a, 'tcx:'a> {
infcx: &'a InferCtxt<'a, 'tcx>,
err: Option<fixup_err>,
}
impl<'a, 'tcx> ty_fold::TypeFolder<'tcx> for FullTypeResolver<'a, 'tcx> {
fn tcx(&self) -> &ty::ctxt<'tcx> {
self.infcx.tcx
}
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
if !ty::type_needs_infer(t) {
t
} else {
let t = self.infcx.shallow_resolve(t);
match t.sty {
ty::ty_infer(ty::TyVar(vid)) => {
self.err = Some(unresolved_ty(vid));
self.tcx().types.err
}
ty::ty_infer(ty::IntVar(vid)) => {
self.err = Some(unresolved_int_ty(vid));
self.tcx().types.err
}
ty::ty_infer(ty::FloatVar(vid)) => {
self.err = Some(unresolved_float_ty(vid));
self.tcx().types.err
}
ty::ty_infer(_) => {
self.infcx.tcx.sess.bug(
&format!("Unexpected type in full type resolver: {}",
t.repr(self.infcx.tcx)));
}
_ => {
ty_fold::super_fold_ty(self, t)
}
}
}
}
fn fold_region(&mut self, r: ty::Region) -> ty::Region {
match r {
ty::ReInfer(ty::ReVar(rid)) => self.infcx.region_vars.resolve_var(rid),
_ => r,
}
}
}