]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
1 | // Copyright 2014 The Rust Project Developers. See the COPYRIGHT |
2 | // file at the top-level directory of this distribution and at | |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
11 | // Type resolution: the phase that finds all the types in the AST with | |
12 | // unresolved type variables and replaces "ty_var" types with their | |
13 | // substitutions. | |
1a4d82fc | 14 | |
1a4d82fc | 15 | use check::FnCtxt; |
cc61c64b | 16 | use rustc::hir; |
3b2f2976 | 17 | use rustc::hir::def_id::{DefId, DefIndex}; |
cc61c64b XL |
18 | use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap}; |
19 | use rustc::infer::{InferCtxt}; | |
7cac9316 | 20 | use rustc::ty::{self, Ty, TyCtxt}; |
54a0048b | 21 | use rustc::ty::fold::{TypeFolder,TypeFoldable}; |
7cac9316 | 22 | use rustc::util::nodemap::DefIdSet; |
1a4d82fc | 23 | use syntax::ast; |
476ff2be | 24 | use syntax_pos::Span; |
cc61c64b | 25 | use std::mem; |
abe05a73 | 26 | use std::rc::Rc; |
1a4d82fc JJ |
27 | |
28 | /////////////////////////////////////////////////////////////////////////// | |
32a655c1 | 29 | // Entry point |
1a4d82fc | 30 | |
a7813a04 | 31 | impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { |
8bb4bdeb XL |
32 | pub fn resolve_type_vars_in_body(&self, body: &'gcx hir::Body) |
33 | -> &'gcx ty::TypeckTables<'gcx> { | |
32a655c1 SL |
34 | let item_id = self.tcx.hir.body_owner(body.id()); |
35 | let item_def_id = self.tcx.hir.local_def_id(item_id); | |
36 | ||
cc61c64b | 37 | let mut wbcx = WritebackCx::new(self, body); |
32a655c1 | 38 | for arg in &body.arguments { |
3b2f2976 | 39 | wbcx.visit_node_id(arg.pat.span, arg.hir_id); |
1a4d82fc | 40 | } |
32a655c1 | 41 | wbcx.visit_body(body); |
a7813a04 XL |
42 | wbcx.visit_upvar_borrow_map(); |
43 | wbcx.visit_closures(); | |
44 | wbcx.visit_liberated_fn_sigs(); | |
45 | wbcx.visit_fru_field_types(); | |
476ff2be | 46 | wbcx.visit_anon_types(); |
32a655c1 | 47 | wbcx.visit_cast_types(); |
8bb4bdeb | 48 | wbcx.visit_free_region_map(); |
ea8adc8c XL |
49 | wbcx.visit_generator_sigs(); |
50 | wbcx.visit_generator_interiors(); | |
8bb4bdeb XL |
51 | |
52 | let used_trait_imports = mem::replace(&mut self.tables.borrow_mut().used_trait_imports, | |
abe05a73 | 53 | Rc::new(DefIdSet())); |
8bb4bdeb XL |
54 | debug!("used_trait_imports({:?}) = {:?}", item_def_id, used_trait_imports); |
55 | wbcx.tables.used_trait_imports = used_trait_imports; | |
32a655c1 | 56 | |
8bb4bdeb XL |
57 | wbcx.tables.tainted_by_errors = self.is_tainted_by_errors(); |
58 | ||
59 | self.tcx.alloc_tables(wbcx.tables) | |
1a4d82fc | 60 | } |
1a4d82fc JJ |
61 | } |
62 | ||
63 | /////////////////////////////////////////////////////////////////////////// | |
64 | // The Writerback context. This visitor walks the AST, checking the | |
65 | // fn-specific tables to find references to types or regions. It | |
66 | // resolves those regions to remove inference variables and writes the | |
67 | // final result back into the master tables in the tcx. Here and | |
68 | // there, it applies a few ad-hoc checks that were not convenient to | |
69 | // do elsewhere. | |
70 | ||
a7813a04 XL |
71 | struct WritebackCx<'cx, 'gcx: 'cx+'tcx, 'tcx: 'cx> { |
72 | fcx: &'cx FnCtxt<'cx, 'gcx, 'tcx>, | |
5bcae85e | 73 | |
32a655c1 SL |
74 | tables: ty::TypeckTables<'gcx>, |
75 | ||
cc61c64b | 76 | body: &'gcx hir::Body, |
1a4d82fc JJ |
77 | } |
78 | ||
a7813a04 | 79 | impl<'cx, 'gcx, 'tcx> WritebackCx<'cx, 'gcx, 'tcx> { |
cc61c64b | 80 | fn new(fcx: &'cx FnCtxt<'cx, 'gcx, 'tcx>, body: &'gcx hir::Body) |
3b2f2976 XL |
81 | -> WritebackCx<'cx, 'gcx, 'tcx> |
82 | { | |
83 | let owner = fcx.tcx.hir.definitions().node_to_hir_id(body.id().node_id); | |
84 | ||
7cac9316 | 85 | WritebackCx { |
3b2f2976 XL |
86 | fcx, |
87 | tables: ty::TypeckTables::empty(Some(DefId::local(owner.owner))), | |
88 | body, | |
5bcae85e | 89 | } |
1a4d82fc JJ |
90 | } |
91 | ||
a7813a04 XL |
92 | fn tcx(&self) -> TyCtxt<'cx, 'gcx, 'tcx> { |
93 | self.fcx.tcx | |
1a4d82fc | 94 | } |
d9579d0f | 95 | |
3b2f2976 XL |
96 | fn write_ty_to_tables(&mut self, hir_id: hir::HirId, ty: Ty<'gcx>) { |
97 | debug!("write_ty_to_tables({:?}, {:?})", hir_id, ty); | |
476ff2be | 98 | assert!(!ty.needs_infer()); |
3b2f2976 | 99 | self.tables.node_types_mut().insert(hir_id, ty); |
476ff2be SL |
100 | } |
101 | ||
d9579d0f AL |
102 | // Hacky hack: During type-checking, we treat *all* operators |
103 | // as potentially overloaded. But then, during writeback, if | |
104 | // we observe that something like `a+b` is (known to be) | |
105 | // operating on scalars, we clear the overload. | |
32a655c1 | 106 | fn fix_scalar_builtin_expr(&mut self, e: &hir::Expr) { |
b039eaaf | 107 | match e.node { |
32a655c1 SL |
108 | hir::ExprUnary(hir::UnNeg, ref inner) | |
109 | hir::ExprUnary(hir::UnNot, ref inner) => { | |
3b2f2976 | 110 | let inner_ty = self.fcx.node_ty(inner.hir_id); |
32a655c1 SL |
111 | let inner_ty = self.fcx.resolve_type_vars_if_possible(&inner_ty); |
112 | ||
113 | if inner_ty.is_scalar() { | |
7cac9316 | 114 | let mut tables = self.fcx.tables.borrow_mut(); |
3b2f2976 XL |
115 | tables.type_dependent_defs_mut().remove(e.hir_id); |
116 | tables.node_substs_mut().remove(e.hir_id); | |
32a655c1 SL |
117 | } |
118 | } | |
b039eaaf SL |
119 | hir::ExprBinary(ref op, ref lhs, ref rhs) | |
120 | hir::ExprAssignOp(ref op, ref lhs, ref rhs) => { | |
3b2f2976 | 121 | let lhs_ty = self.fcx.node_ty(lhs.hir_id); |
a7813a04 | 122 | let lhs_ty = self.fcx.resolve_type_vars_if_possible(&lhs_ty); |
b039eaaf | 123 | |
3b2f2976 | 124 | let rhs_ty = self.fcx.node_ty(rhs.hir_id); |
a7813a04 | 125 | let rhs_ty = self.fcx.resolve_type_vars_if_possible(&rhs_ty); |
b039eaaf SL |
126 | |
127 | if lhs_ty.is_scalar() && rhs_ty.is_scalar() { | |
7cac9316 | 128 | let mut tables = self.fcx.tables.borrow_mut(); |
3b2f2976 XL |
129 | tables.type_dependent_defs_mut().remove(e.hir_id); |
130 | tables.node_substs_mut().remove(e.hir_id); | |
b039eaaf | 131 | |
b039eaaf SL |
132 | match e.node { |
133 | hir::ExprBinary(..) => { | |
54a0048b | 134 | if !op.node.is_by_value() { |
3b2f2976 XL |
135 | let mut adjustments = tables.adjustments_mut(); |
136 | adjustments.get_mut(lhs.hir_id).map(|a| a.pop()); | |
137 | adjustments.get_mut(rhs.hir_id).map(|a| a.pop()); | |
b039eaaf SL |
138 | } |
139 | }, | |
140 | hir::ExprAssignOp(..) => { | |
3b2f2976 | 141 | tables.adjustments_mut().get_mut(lhs.hir_id).map(|a| a.pop()); |
b039eaaf SL |
142 | }, |
143 | _ => {}, | |
144 | } | |
d9579d0f AL |
145 | } |
146 | } | |
b039eaaf | 147 | _ => {}, |
d9579d0f AL |
148 | } |
149 | } | |
1a4d82fc JJ |
150 | } |
151 | ||
152 | /////////////////////////////////////////////////////////////////////////// | |
153 | // Impl of Visitor for Resolver | |
154 | // | |
155 | // This is the master code which walks the AST. It delegates most of | |
156 | // the heavy lifting to the generic visit and resolve functions | |
157 | // below. In general, a function is made into a `visitor` if it must | |
158 | // traffic in node-ids or update tables in the type context etc. | |
159 | ||
476ff2be SL |
160 | impl<'cx, 'gcx, 'tcx> Visitor<'gcx> for WritebackCx<'cx, 'gcx, 'tcx> { |
161 | fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'gcx> { | |
32a655c1 | 162 | NestedVisitorMap::None |
476ff2be SL |
163 | } |
164 | ||
476ff2be | 165 | fn visit_expr(&mut self, e: &'gcx hir::Expr) { |
32a655c1 | 166 | self.fix_scalar_builtin_expr(e); |
c34b1796 | 167 | |
3b2f2976 | 168 | self.visit_node_id(e.span, e.hir_id); |
1a4d82fc | 169 | |
ea8adc8c | 170 | if let hir::ExprClosure(_, _, body, _, _) = e.node { |
32a655c1 SL |
171 | let body = self.fcx.tcx.hir.body(body); |
172 | for arg in &body.arguments { | |
3b2f2976 | 173 | self.visit_node_id(e.span, arg.hir_id); |
1a4d82fc | 174 | } |
32a655c1 SL |
175 | |
176 | self.visit_body(body); | |
1a4d82fc JJ |
177 | } |
178 | ||
92a42be0 | 179 | intravisit::walk_expr(self, e); |
1a4d82fc JJ |
180 | } |
181 | ||
476ff2be | 182 | fn visit_block(&mut self, b: &'gcx hir::Block) { |
3b2f2976 | 183 | self.visit_node_id(b.span, b.hir_id); |
92a42be0 | 184 | intravisit::walk_block(self, b); |
1a4d82fc JJ |
185 | } |
186 | ||
476ff2be | 187 | fn visit_pat(&mut self, p: &'gcx hir::Pat) { |
3b2f2976 XL |
188 | match p.node { |
189 | hir::PatKind::Binding(..) => { | |
190 | let bm = *self.fcx | |
191 | .tables | |
192 | .borrow() | |
193 | .pat_binding_modes() | |
194 | .get(p.hir_id) | |
195 | .expect("missing binding mode"); | |
196 | self.tables.pat_binding_modes_mut().insert(p.hir_id, bm); | |
197 | } | |
198 | _ => {} | |
199 | }; | |
200 | ||
ea8adc8c XL |
201 | self.visit_pat_adjustments(p.span, p.hir_id); |
202 | ||
3b2f2976 | 203 | self.visit_node_id(p.span, p.hir_id); |
92a42be0 | 204 | intravisit::walk_pat(self, p); |
1a4d82fc JJ |
205 | } |
206 | ||
476ff2be | 207 | fn visit_local(&mut self, l: &'gcx hir::Local) { |
cc61c64b | 208 | intravisit::walk_local(self, l); |
1a4d82fc | 209 | let var_ty = self.fcx.local_ty(l.span, l.id); |
cc61c64b | 210 | let var_ty = self.resolve(&var_ty, &l.span); |
3b2f2976 | 211 | self.write_ty_to_tables(l.hir_id, var_ty); |
1a4d82fc | 212 | } |
ea8adc8c XL |
213 | |
214 | fn visit_ty(&mut self, hir_ty: &'gcx hir::Ty) { | |
215 | intravisit::walk_ty(self, hir_ty); | |
216 | let ty = self.fcx.node_ty(hir_ty.hir_id); | |
217 | let ty = self.resolve(&ty, &hir_ty.span); | |
218 | self.write_ty_to_tables(hir_ty.hir_id, ty); | |
219 | } | |
1a4d82fc JJ |
220 | } |
221 | ||
a7813a04 | 222 | impl<'cx, 'gcx, 'tcx> WritebackCx<'cx, 'gcx, 'tcx> { |
32a655c1 | 223 | fn visit_upvar_borrow_map(&mut self) { |
a7813a04 | 224 | for (upvar_id, upvar_capture) in self.fcx.tables.borrow().upvar_capture_map.iter() { |
85aaf69f SL |
225 | let new_upvar_capture = match *upvar_capture { |
226 | ty::UpvarCapture::ByValue => ty::UpvarCapture::ByValue, | |
227 | ty::UpvarCapture::ByRef(ref upvar_borrow) => { | |
228 | let r = upvar_borrow.region; | |
cc61c64b | 229 | let r = self.resolve(&r, &upvar_id.var_id); |
85aaf69f SL |
230 | ty::UpvarCapture::ByRef( |
231 | ty::UpvarBorrow { kind: upvar_borrow.kind, region: r }) | |
232 | } | |
233 | }; | |
62682a34 SL |
234 | debug!("Upvar capture for {:?} resolved to {:?}", |
235 | upvar_id, | |
236 | new_upvar_capture); | |
32a655c1 | 237 | self.tables.upvar_capture_map.insert(*upvar_id, new_upvar_capture); |
1a4d82fc JJ |
238 | } |
239 | } | |
240 | ||
8bb4bdeb | 241 | fn visit_closures(&mut self) { |
3b2f2976 XL |
242 | let fcx_tables = self.fcx.tables.borrow(); |
243 | debug_assert_eq!(fcx_tables.local_id_root, self.tables.local_id_root); | |
244 | let common_local_id_root = fcx_tables.local_id_root.unwrap(); | |
245 | ||
246 | for (&id, closure_ty) in fcx_tables.closure_tys().iter() { | |
247 | let hir_id = hir::HirId { | |
248 | owner: common_local_id_root.index, | |
249 | local_id: id, | |
250 | }; | |
251 | let closure_ty = self.resolve(closure_ty, &hir_id); | |
252 | self.tables.closure_tys_mut().insert(hir_id, closure_ty); | |
85aaf69f SL |
253 | } |
254 | ||
3b2f2976 XL |
255 | for (&id, &closure_kind) in fcx_tables.closure_kinds().iter() { |
256 | let hir_id = hir::HirId { | |
257 | owner: common_local_id_root.index, | |
258 | local_id: id, | |
259 | }; | |
260 | self.tables.closure_kinds_mut().insert(hir_id, closure_kind); | |
1a4d82fc JJ |
261 | } |
262 | } | |
263 | ||
32a655c1 | 264 | fn visit_cast_types(&mut self) { |
3b2f2976 XL |
265 | let fcx_tables = self.fcx.tables.borrow(); |
266 | let fcx_cast_kinds = fcx_tables.cast_kinds(); | |
267 | debug_assert_eq!(fcx_tables.local_id_root, self.tables.local_id_root); | |
268 | let mut self_cast_kinds = self.tables.cast_kinds_mut(); | |
269 | let common_local_id_root = fcx_tables.local_id_root.unwrap(); | |
270 | ||
271 | for (&local_id, &cast_kind) in fcx_cast_kinds.iter() { | |
272 | let hir_id = hir::HirId { | |
273 | owner: common_local_id_root.index, | |
274 | local_id, | |
275 | }; | |
276 | self_cast_kinds.insert(hir_id, cast_kind); | |
277 | } | |
8bb4bdeb XL |
278 | } |
279 | ||
280 | fn visit_free_region_map(&mut self) { | |
7cac9316 XL |
281 | let free_region_map = self.tcx().lift_to_global(&self.fcx.tables.borrow().free_region_map); |
282 | let free_region_map = free_region_map.expect("all regions in free-region-map are global"); | |
283 | self.tables.free_region_map = free_region_map; | |
8bb4bdeb | 284 | } |
5bcae85e | 285 | |
8bb4bdeb | 286 | fn visit_anon_types(&mut self) { |
5bcae85e | 287 | let gcx = self.tcx().global_tcx(); |
8bb4bdeb | 288 | for (&node_id, &concrete_ty) in self.fcx.anon_types.borrow().iter() { |
cc61c64b | 289 | let inside_ty = self.resolve(&concrete_ty, &node_id); |
5bcae85e SL |
290 | |
291 | // Convert the type from the function into a type valid outside | |
7cac9316 XL |
292 | // the function, by replacing invalid regions with 'static, |
293 | // after producing an error for each of them. | |
5bcae85e | 294 | let outside_ty = gcx.fold_regions(&inside_ty, &mut false, |r, _| { |
9e0c209e | 295 | match *r { |
7cac9316 XL |
296 | // 'static and early-bound regions are valid. |
297 | ty::ReStatic | | |
298 | ty::ReEarlyBound(_) | | |
299 | ty::ReEmpty => r, | |
5bcae85e SL |
300 | |
301 | ty::ReFree(_) | | |
5bcae85e SL |
302 | ty::ReLateBound(..) | |
303 | ty::ReScope(_) | | |
304 | ty::ReSkolemized(..) => { | |
cc61c64b | 305 | let span = node_id.to_span(&self.fcx.tcx); |
5bcae85e SL |
306 | span_err!(self.tcx().sess, span, E0564, |
307 | "only named lifetimes are allowed in `impl Trait`, \ | |
308 | but `{}` was found in the type `{}`", r, inside_ty); | |
cc61c64b | 309 | gcx.types.re_static |
5bcae85e SL |
310 | } |
311 | ||
312 | ty::ReVar(_) | | |
5bcae85e | 313 | ty::ReErased => { |
cc61c64b | 314 | let span = node_id.to_span(&self.fcx.tcx); |
5bcae85e SL |
315 | span_bug!(span, "invalid region in impl Trait: {:?}", r); |
316 | } | |
317 | } | |
318 | }); | |
319 | ||
3b2f2976 XL |
320 | let hir_id = self.tcx().hir.node_to_hir_id(node_id); |
321 | self.tables.node_types_mut().insert(hir_id, outside_ty); | |
5bcae85e SL |
322 | } |
323 | } | |
324 | ||
3b2f2976 | 325 | fn visit_node_id(&mut self, span: Span, hir_id: hir::HirId) { |
7cac9316 | 326 | // Export associated path extensions and method resultions. |
3b2f2976 XL |
327 | if let Some(def) = self.fcx |
328 | .tables | |
329 | .borrow_mut() | |
330 | .type_dependent_defs_mut() | |
331 | .remove(hir_id) { | |
332 | self.tables.type_dependent_defs_mut().insert(hir_id, def); | |
476ff2be SL |
333 | } |
334 | ||
cc61c64b | 335 | // Resolve any borrowings for the node with id `node_id` |
3b2f2976 | 336 | self.visit_adjustments(span, hir_id); |
1a4d82fc | 337 | |
cc61c64b | 338 | // Resolve the type of the node with id `node_id` |
3b2f2976 | 339 | let n_ty = self.fcx.node_ty(hir_id); |
cc61c64b | 340 | let n_ty = self.resolve(&n_ty, &span); |
3b2f2976 XL |
341 | self.write_ty_to_tables(hir_id, n_ty); |
342 | debug!("Node {:?} has type {:?}", hir_id, n_ty); | |
1a4d82fc JJ |
343 | |
344 | // Resolve any substitutions | |
3b2f2976 | 345 | if let Some(substs) = self.fcx.tables.borrow().node_substs_opt(hir_id) { |
7cac9316 | 346 | let substs = self.resolve(&substs, &span); |
3b2f2976 | 347 | debug!("write_substs_to_tcx({:?}, {:?})", hir_id, substs); |
7cac9316 | 348 | assert!(!substs.needs_infer()); |
3b2f2976 | 349 | self.tables.node_substs_mut().insert(hir_id, substs); |
7cac9316 | 350 | } |
1a4d82fc JJ |
351 | } |
352 | ||
3b2f2976 XL |
353 | fn visit_adjustments(&mut self, span: Span, hir_id: hir::HirId) { |
354 | let adjustment = self.fcx | |
355 | .tables | |
356 | .borrow_mut() | |
357 | .adjustments_mut() | |
358 | .remove(hir_id); | |
7cac9316 | 359 | match adjustment { |
1a4d82fc | 360 | None => { |
3b2f2976 | 361 | debug!("No adjustments for node {:?}", hir_id); |
1a4d82fc JJ |
362 | } |
363 | ||
364 | Some(adjustment) => { | |
7cac9316 | 365 | let resolved_adjustment = self.resolve(&adjustment, &span); |
3b2f2976 XL |
366 | debug!("Adjustments for node {:?}: {:?}", hir_id, resolved_adjustment); |
367 | self.tables.adjustments_mut().insert(hir_id, resolved_adjustment); | |
1a4d82fc JJ |
368 | } |
369 | } | |
370 | } | |
371 | ||
ea8adc8c XL |
372 | fn visit_pat_adjustments(&mut self, span: Span, hir_id: hir::HirId) { |
373 | let adjustment = self.fcx | |
374 | .tables | |
375 | .borrow_mut() | |
376 | .pat_adjustments_mut() | |
377 | .remove(hir_id); | |
378 | match adjustment { | |
379 | None => { | |
380 | debug!("No pat_adjustments for node {:?}", hir_id); | |
381 | } | |
382 | ||
383 | Some(adjustment) => { | |
384 | let resolved_adjustment = self.resolve(&adjustment, &span); | |
385 | debug!("pat_adjustments for node {:?}: {:?}", hir_id, resolved_adjustment); | |
386 | self.tables.pat_adjustments_mut().insert(hir_id, resolved_adjustment); | |
387 | } | |
388 | } | |
389 | } | |
390 | ||
391 | fn visit_generator_interiors(&mut self) { | |
392 | let common_local_id_root = self.fcx.tables.borrow().local_id_root.unwrap(); | |
393 | for (&id, interior) in self.fcx.tables.borrow().generator_interiors().iter() { | |
394 | let hir_id = hir::HirId { | |
395 | owner: common_local_id_root.index, | |
396 | local_id: id, | |
397 | }; | |
398 | let interior = self.resolve(interior, &hir_id); | |
399 | self.tables.generator_interiors_mut().insert(hir_id, interior); | |
400 | } | |
401 | } | |
402 | ||
403 | fn visit_generator_sigs(&mut self) { | |
404 | let common_local_id_root = self.fcx.tables.borrow().local_id_root.unwrap(); | |
405 | for (&id, gen_sig) in self.fcx.tables.borrow().generator_sigs().iter() { | |
406 | let hir_id = hir::HirId { | |
407 | owner: common_local_id_root.index, | |
408 | local_id: id, | |
409 | }; | |
410 | let gen_sig = gen_sig.map(|s| ty::GenSig { | |
411 | yield_ty: self.resolve(&s.yield_ty, &hir_id), | |
412 | return_ty: self.resolve(&s.return_ty, &hir_id), | |
413 | }); | |
414 | self.tables.generator_sigs_mut().insert(hir_id, gen_sig); | |
415 | } | |
416 | } | |
417 | ||
32a655c1 | 418 | fn visit_liberated_fn_sigs(&mut self) { |
3b2f2976 XL |
419 | let fcx_tables = self.fcx.tables.borrow(); |
420 | debug_assert_eq!(fcx_tables.local_id_root, self.tables.local_id_root); | |
421 | let common_local_id_root = fcx_tables.local_id_root.unwrap(); | |
422 | ||
423 | for (&local_id, fn_sig) in fcx_tables.liberated_fn_sigs().iter() { | |
424 | let hir_id = hir::HirId { | |
425 | owner: common_local_id_root.index, | |
426 | local_id, | |
427 | }; | |
428 | let fn_sig = self.resolve(fn_sig, &hir_id); | |
429 | self.tables.liberated_fn_sigs_mut().insert(hir_id, fn_sig.clone()); | |
92a42be0 SL |
430 | } |
431 | } | |
432 | ||
32a655c1 | 433 | fn visit_fru_field_types(&mut self) { |
3b2f2976 XL |
434 | let fcx_tables = self.fcx.tables.borrow(); |
435 | debug_assert_eq!(fcx_tables.local_id_root, self.tables.local_id_root); | |
436 | let common_local_id_root = fcx_tables.local_id_root.unwrap(); | |
437 | ||
438 | for (&local_id, ftys) in fcx_tables.fru_field_types().iter() { | |
439 | let hir_id = hir::HirId { | |
440 | owner: common_local_id_root.index, | |
441 | local_id, | |
442 | }; | |
443 | let ftys = self.resolve(ftys, &hir_id); | |
444 | self.tables.fru_field_types_mut().insert(hir_id, ftys); | |
7453a54e SL |
445 | } |
446 | } | |
447 | ||
cc61c64b | 448 | fn resolve<T>(&self, x: &T, span: &Locatable) -> T::Lifted |
a7813a04 XL |
449 | where T: TypeFoldable<'tcx> + ty::Lift<'gcx> |
450 | { | |
cc61c64b | 451 | let x = x.fold_with(&mut Resolver::new(self.fcx, span, self.body)); |
a7813a04 XL |
452 | if let Some(lifted) = self.tcx().lift_to_global(&x) { |
453 | lifted | |
454 | } else { | |
cc61c64b XL |
455 | span_bug!(span.to_span(&self.fcx.tcx), |
456 | "writeback: `{:?}` missing from the global type context", | |
457 | x); | |
a7813a04 | 458 | } |
1a4d82fc JJ |
459 | } |
460 | } | |
461 | ||
cc61c64b XL |
462 | trait Locatable { |
463 | fn to_span(&self, tcx: &TyCtxt) -> Span; | |
1a4d82fc JJ |
464 | } |
465 | ||
cc61c64b XL |
466 | impl Locatable for Span { |
467 | fn to_span(&self, _: &TyCtxt) -> Span { *self } | |
468 | } | |
469 | ||
470 | impl Locatable for ast::NodeId { | |
471 | fn to_span(&self, tcx: &TyCtxt) -> Span { tcx.hir.span(*self) } | |
1a4d82fc JJ |
472 | } |
473 | ||
3b2f2976 XL |
474 | impl Locatable for DefIndex { |
475 | fn to_span(&self, tcx: &TyCtxt) -> Span { | |
476 | let node_id = tcx.hir.def_index_to_node_id(*self); | |
477 | tcx.hir.span(node_id) | |
478 | } | |
479 | } | |
480 | ||
481 | impl Locatable for hir::HirId { | |
482 | fn to_span(&self, tcx: &TyCtxt) -> Span { | |
483 | let node_id = tcx.hir.definitions().find_node_for_hir_id(*self); | |
484 | tcx.hir.span(node_id) | |
485 | } | |
486 | } | |
487 | ||
1a4d82fc JJ |
488 | /////////////////////////////////////////////////////////////////////////// |
489 | // The Resolver. This is the type folding engine that detects | |
490 | // unresolved types and so forth. | |
491 | ||
a7813a04 XL |
492 | struct Resolver<'cx, 'gcx: 'cx+'tcx, 'tcx: 'cx> { |
493 | tcx: TyCtxt<'cx, 'gcx, 'tcx>, | |
494 | infcx: &'cx InferCtxt<'cx, 'gcx, 'tcx>, | |
cc61c64b XL |
495 | span: &'cx Locatable, |
496 | body: &'gcx hir::Body, | |
1a4d82fc JJ |
497 | } |
498 | ||
a7813a04 | 499 | impl<'cx, 'gcx, 'tcx> Resolver<'cx, 'gcx, 'tcx> { |
cc61c64b XL |
500 | fn new(fcx: &'cx FnCtxt<'cx, 'gcx, 'tcx>, span: &'cx Locatable, body: &'gcx hir::Body) |
501 | -> Resolver<'cx, 'gcx, 'tcx> | |
1a4d82fc | 502 | { |
cc61c64b XL |
503 | Resolver { |
504 | tcx: fcx.tcx, | |
505 | infcx: fcx, | |
3b2f2976 XL |
506 | span, |
507 | body, | |
cc61c64b | 508 | } |
1a4d82fc JJ |
509 | } |
510 | ||
cc61c64b | 511 | fn report_error(&self, t: Ty<'tcx>) { |
1a4d82fc | 512 | if !self.tcx.sess.has_errors() { |
041b39d2 | 513 | self.infcx.need_type_info(Some(self.body.id()), self.span.to_span(&self.tcx), t); |
1a4d82fc JJ |
514 | } |
515 | } | |
516 | } | |
517 | ||
a7813a04 XL |
518 | impl<'cx, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for Resolver<'cx, 'gcx, 'tcx> { |
519 | fn tcx<'a>(&'a self) -> TyCtxt<'a, 'gcx, 'tcx> { | |
1a4d82fc JJ |
520 | self.tcx |
521 | } | |
522 | ||
523 | fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> { | |
524 | match self.infcx.fully_resolve(&t) { | |
525 | Ok(t) => t, | |
cc61c64b | 526 | Err(_) => { |
62682a34 SL |
527 | debug!("Resolver::fold_ty: input type `{:?}` not fully resolvable", |
528 | t); | |
cc61c64b | 529 | self.report_error(t); |
1a4d82fc JJ |
530 | self.tcx().types.err |
531 | } | |
532 | } | |
533 | } | |
534 | ||
cc61c64b XL |
535 | // FIXME This should be carefully checked |
536 | // We could use `self.report_error` but it doesn't accept a ty::Region, right now. | |
7cac9316 | 537 | fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> { |
1a4d82fc JJ |
538 | match self.infcx.fully_resolve(&r) { |
539 | Ok(r) => r, | |
cc61c64b XL |
540 | Err(_) => { |
541 | self.tcx.types.re_static | |
1a4d82fc JJ |
542 | } |
543 | } | |
544 | } | |
545 | } | |
546 | ||
547 | /////////////////////////////////////////////////////////////////////////// | |
548 | // During type check, we store promises with the result of trait | |
549 | // lookup rather than the actual results (because the results are not | |
550 | // necessarily available immediately). These routines unwind the | |
551 | // promises. It is expected that we will have already reported any | |
552 | // errors that may be encountered, so if the promises store an error, | |
553 | // a dummy result is returned. |