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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
use middle::ty::{FloatVar, FloatVid, IntVar, IntVid, RegionVid, TyVar, TyVid};
use middle::ty::{type_is_bot, IntType, UintType};
use middle::ty;
use middle::ty_fold;
use middle::typeck::infer::{Bounds, cyclic_ty, fixup_err, fres, InferCtxt};
use middle::typeck::infer::unresolved_ty;
use middle::typeck::infer::unify::Root;
use syntax::codemap::Span;
use util::common::indent;
use util::ppaux::{Repr, ty_to_str};
use syntax::ast;
pub static resolve_nested_tvar: uint = 0b0000000001;
pub static resolve_rvar: uint = 0b0000000010;
pub static resolve_ivar: uint = 0b0000000100;
pub static resolve_fvar: uint = 0b0000001000;
pub static resolve_all: uint = 0b0000001111;
pub static force_tvar: uint = 0b0000100000;
pub static force_rvar: uint = 0b0001000000;
pub static force_ivar: uint = 0b0010000000;
pub static force_fvar: uint = 0b0100000000;
pub static force_all: uint = 0b0111100000;
pub static not_regions: uint = !(force_rvar | resolve_rvar);
pub static try_resolve_tvar_shallow: uint = 0;
pub static resolve_and_force_all_but_regions: uint =
(resolve_all | force_all) & not_regions;
pub struct ResolveState<'a> {
infcx: &'a InferCtxt<'a>,
modes: uint,
err: Option<fixup_err>,
v_seen: Vec<TyVid> ,
type_depth: uint,
span: Option<Span>,
}
pub fn resolver<'a>(infcx: &'a InferCtxt,
modes: uint,
span: Option<Span>)
-> ResolveState<'a>
{
ResolveState {
infcx: infcx,
modes: modes,
err: None,
v_seen: Vec::new(),
type_depth: 0,
span: span
}
}
impl<'a> ty_fold::TypeFolder for ResolveState<'a> {
fn tcx<'a>(&'a self) -> &'a ty::ctxt {
self.infcx.tcx
}
fn fold_ty(&mut self, t: ty::t) -> ty::t {
self.resolve_type(t)
}
fn fold_region(&mut self, r: ty::Region) -> ty::Region {
self.resolve_region(r)
}
}
impl<'a> ResolveState<'a> {
pub fn should(&mut self, mode: uint) -> bool {
(self.modes & mode) == mode
}
pub fn resolve_type_chk(&mut self,
typ: ty::t)
-> fres<ty::t> {
self.err = None;
debug!("Resolving {} (modes={:x})",
ty_to_str(self.infcx.tcx, typ),
self.modes);assert!(self.v_seen.is_empty());
let rty = indent(|| self.resolve_type(typ) );
assert!(self.v_seen.is_empty());
match self.err {
None => {
debug!("Resolved to {} + {} (modes={:x})",
ty_to_str(self.infcx.tcx, rty),
ty_to_str(self.infcx.tcx, rty),
self.modes);
return Ok(rty);
}
Some(e) => return Err(e)
}
}
pub fn resolve_region_chk(&mut self,
orig: ty::Region)
-> fres<ty::Region> {
self.err = None;
let resolved = indent(|| self.resolve_region(orig) );
match self.err {
None => Ok(resolved),
Some(e) => Err(e)
}
}
pub fn resolve_type(&mut self, typ: ty::t) -> ty::t {
debug!("resolve_type({})", typ.repr(self.infcx.tcx));
if !ty::type_needs_infer(typ) {
return typ;
}
if self.type_depth > 0 && !self.should(resolve_nested_tvar) {
return typ;
}
match ty::get(typ).sty {
ty::ty_infer(TyVar(vid)) => {
self.resolve_ty_var(vid)
}
ty::ty_infer(IntVar(vid)) => {
self.resolve_int_var(vid)
}
ty::ty_infer(FloatVar(vid)) => {
self.resolve_float_var(vid)
}
_ => {
if self.modes & resolve_all == 0 {typ
} else {
self.type_depth += 1;
let result = ty_fold::super_fold_ty(self, typ);
self.type_depth -= 1;
result
}
}
}
}
pub fn resolve_region(&mut self, orig: ty::Region) -> ty::Region {
debug!("Resolve_region({})", orig.repr(self.infcx.tcx));
match orig {
ty::ReInfer(ty::ReVar(rid)) => self.resolve_region_var(rid),
_ => orig
}
}
pub fn resolve_region_var(&mut self, rid: RegionVid) -> ty::Region {
if !self.should(resolve_rvar) {
return ty::ReInfer(ty::ReVar(rid));
}
self.infcx.region_vars.resolve_var(rid)
}
pub fn resolve_ty_var(&mut self, vid: TyVid) -> ty::t {
if self.v_seen.contains(&vid) {
self.err = Some(cyclic_ty(vid));
return ty::mk_var(self.infcx.tcx, vid);
} else {
self.v_seen.push(vid);
let tcx = self.infcx.tcx;let node =
self.infcx.type_unification_table.borrow_mut().get(tcx, vid);
let t1 = match node.value {
Bounds { ub:_, lb:Some(t) } if !type_is_bot(t) => {
self.resolve_type(t)
}
Bounds { ub:Some(t), lb:_ } | Bounds { ub:_, lb:Some(t) } => {
self.resolve_type(t)
}
Bounds { ub:None, lb:None } => {
if self.should(force_tvar) {
self.err = Some(unresolved_ty(vid));
}
ty::mk_var(tcx, vid)
}
};
self.v_seen.pop().unwrap();
return t1;
}
}
pub fn resolve_int_var(&mut self, vid: IntVid) -> ty::t {
if !self.should(resolve_ivar) {
return ty::mk_int_var(self.infcx.tcx, vid);
}
let tcx = self.infcx.tcx;
let table = &self.infcx.int_unification_table;
let node = table.borrow_mut().get(tcx, vid);
match node.value {
Some(IntType(t)) => ty::mk_mach_int(t),
Some(UintType(t)) => ty::mk_mach_uint(t),
None => {
if self.should(force_ivar) {let ty = ty::mk_int();
table.borrow_mut().set(
tcx, node.key, Root(Some(IntType(ast::TyI)), node.rank));
match self.span {
Some(sp) => {
self.infcx.tcx.sess.span_err(
sp,
"cannot determine the type of this integer; add \
a suffix to specify the type explicitly");
}
None => { }
}
ty
} else {
ty::mk_int_var(self.infcx.tcx, vid)
}
}
}
}
pub fn resolve_float_var(&mut self, vid: FloatVid) -> ty::t {
if !self.should(resolve_fvar) {
return ty::mk_float_var(self.infcx.tcx, vid);
}
let tcx = self.infcx.tcx;
let table = &self.infcx.float_unification_table;
let node = table.borrow_mut().get(tcx, vid);
match node.value {
Some(t) => ty::mk_mach_float(t),
None => {
if self.should(force_fvar) {let ty = ty::mk_f64();
table.borrow_mut().set(
tcx, node.key, Root(Some(ast::TyF64), node.rank));
match self.span {
Some(sp) => {
self.infcx.tcx.sess.span_err(
sp,
"cannot determine the type of this number; add \
a suffix to specify the type explicitly");
}
None => { }
}
ty
} else {
ty::mk_float_var(self.infcx.tcx, vid)
}
}
}
}
}