]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/check/writeback.rs
3207ac44948f471885937034d32a4d5716010b10
[rust.git] / src / librustc_typeck / check / writeback.rs
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.
14
15 use check::FnCtxt;
16 use rustc::hir;
17 use rustc::hir::def_id::{DefId, DefIndex};
18 use rustc::hir::intravisit::{self, NestedVisitorMap, Visitor};
19 use rustc::infer::InferCtxt;
20 use rustc::ty::{self, Ty, TyCtxt};
21 use rustc::ty::adjustment::{Adjust, Adjustment};
22 use rustc::ty::fold::{TypeFoldable, TypeFolder};
23 use rustc::util::nodemap::DefIdSet;
24 use syntax::ast;
25 use syntax_pos::Span;
26 use std::mem;
27 use rustc_data_structures::sync::Lrc;
28
29 ///////////////////////////////////////////////////////////////////////////
30 // Entry point
31
32 impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
33     pub fn resolve_type_vars_in_body(&self, body: &'gcx hir::Body) -> &'gcx ty::TypeckTables<'gcx> {
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
37         let mut wbcx = WritebackCx::new(self, body);
38         for arg in &body.arguments {
39             wbcx.visit_node_id(arg.pat.span, arg.hir_id);
40         }
41         wbcx.visit_body(body);
42         wbcx.visit_upvar_borrow_map();
43         wbcx.visit_closures();
44         wbcx.visit_liberated_fn_sigs();
45         wbcx.visit_fru_field_types();
46         wbcx.visit_anon_types(body.value.span);
47         wbcx.visit_cast_types();
48         wbcx.visit_free_region_map();
49         wbcx.visit_user_provided_tys();
50
51         let used_trait_imports = mem::replace(
52             &mut self.tables.borrow_mut().used_trait_imports,
53             Lrc::new(DefIdSet()),
54         );
55         debug!(
56             "used_trait_imports({:?}) = {:?}",
57             item_def_id,
58             used_trait_imports
59         );
60         wbcx.tables.used_trait_imports = used_trait_imports;
61
62         wbcx.tables.tainted_by_errors = self.is_tainted_by_errors();
63
64         debug!(
65             "writeback: tables for {:?} are {:#?}",
66             item_def_id,
67             wbcx.tables
68         );
69
70         self.tcx.alloc_tables(wbcx.tables)
71     }
72 }
73
74 ///////////////////////////////////////////////////////////////////////////
75 // The Writerback context. This visitor walks the AST, checking the
76 // fn-specific tables to find references to types or regions. It
77 // resolves those regions to remove inference variables and writes the
78 // final result back into the master tables in the tcx. Here and
79 // there, it applies a few ad-hoc checks that were not convenient to
80 // do elsewhere.
81
82 struct WritebackCx<'cx, 'gcx: 'cx + 'tcx, 'tcx: 'cx> {
83     fcx: &'cx FnCtxt<'cx, 'gcx, 'tcx>,
84
85     tables: ty::TypeckTables<'gcx>,
86
87     body: &'gcx hir::Body,
88 }
89
90 impl<'cx, 'gcx, 'tcx> WritebackCx<'cx, 'gcx, 'tcx> {
91     fn new(
92         fcx: &'cx FnCtxt<'cx, 'gcx, 'tcx>,
93         body: &'gcx hir::Body,
94     ) -> WritebackCx<'cx, 'gcx, 'tcx> {
95         let owner = fcx.tcx.hir.definitions().node_to_hir_id(body.id().node_id);
96
97         WritebackCx {
98             fcx,
99             tables: ty::TypeckTables::empty(Some(DefId::local(owner.owner))),
100             body,
101         }
102     }
103
104     fn tcx(&self) -> TyCtxt<'cx, 'gcx, 'tcx> {
105         self.fcx.tcx
106     }
107
108     fn write_ty_to_tables(&mut self, hir_id: hir::HirId, ty: Ty<'gcx>) {
109         debug!("write_ty_to_tables({:?}, {:?})", hir_id, ty);
110         assert!(!ty.needs_infer() && !ty.has_skol());
111         self.tables.node_types_mut().insert(hir_id, ty);
112     }
113
114     // Hacky hack: During type-checking, we treat *all* operators
115     // as potentially overloaded. But then, during writeback, if
116     // we observe that something like `a+b` is (known to be)
117     // operating on scalars, we clear the overload.
118     fn fix_scalar_builtin_expr(&mut self, e: &hir::Expr) {
119         match e.node {
120             hir::ExprKind::Unary(hir::UnNeg, ref inner) |
121             hir::ExprKind::Unary(hir::UnNot, ref inner) => {
122                 let inner_ty = self.fcx.node_ty(inner.hir_id);
123                 let inner_ty = self.fcx.resolve_type_vars_if_possible(&inner_ty);
124
125                 if inner_ty.is_scalar() {
126                     let mut tables = self.fcx.tables.borrow_mut();
127                     tables.type_dependent_defs_mut().remove(e.hir_id);
128                     tables.node_substs_mut().remove(e.hir_id);
129                 }
130             }
131             hir::ExprKind::Binary(ref op, ref lhs, ref rhs)
132             | hir::ExprKind::AssignOp(ref op, ref lhs, ref rhs) => {
133                 let lhs_ty = self.fcx.node_ty(lhs.hir_id);
134                 let lhs_ty = self.fcx.resolve_type_vars_if_possible(&lhs_ty);
135
136                 let rhs_ty = self.fcx.node_ty(rhs.hir_id);
137                 let rhs_ty = self.fcx.resolve_type_vars_if_possible(&rhs_ty);
138
139                 if lhs_ty.is_scalar() && rhs_ty.is_scalar() {
140                     let mut tables = self.fcx.tables.borrow_mut();
141                     tables.type_dependent_defs_mut().remove(e.hir_id);
142                     tables.node_substs_mut().remove(e.hir_id);
143
144                     match e.node {
145                         hir::ExprKind::Binary(..) => {
146                             if !op.node.is_by_value() {
147                                 let mut adjustments = tables.adjustments_mut();
148                                 adjustments.get_mut(lhs.hir_id).map(|a| a.pop());
149                                 adjustments.get_mut(rhs.hir_id).map(|a| a.pop());
150                             }
151                         }
152                         hir::ExprKind::AssignOp(..) => {
153                             tables
154                                 .adjustments_mut()
155                                 .get_mut(lhs.hir_id)
156                                 .map(|a| a.pop());
157                         }
158                         _ => {}
159                     }
160                 }
161             }
162             _ => {}
163         }
164     }
165
166     // Similar to operators, indexing is always assumed to be overloaded
167     // Here, correct cases where an indexing expression can be simplified
168     // to use builtin indexing because the index type is known to be
169     // usize-ish
170     fn fix_index_builtin_expr(&mut self, e: &hir::Expr) {
171         if let hir::ExprKind::Index(ref base, ref index) = e.node {
172             let mut tables = self.fcx.tables.borrow_mut();
173
174             match tables.expr_ty_adjusted(&base).sty {
175                 // All valid indexing looks like this
176                 ty::TyRef(_, base_ty, _) => {
177                     let index_ty = tables.expr_ty_adjusted(&index);
178                     let index_ty = self.fcx.resolve_type_vars_if_possible(&index_ty);
179
180                     if base_ty.builtin_index().is_some()
181                         && index_ty == self.fcx.tcx.types.usize {
182                         // Remove the method call record
183                         tables.type_dependent_defs_mut().remove(e.hir_id);
184                         tables.node_substs_mut().remove(e.hir_id);
185
186                         tables.adjustments_mut().get_mut(base.hir_id).map(|a| {
187                             // Discard the need for a mutable borrow
188                             match a.pop() {
189                                 // Extra adjustment made when indexing causes a drop
190                                 // of size information - we need to get rid of it
191                                 // Since this is "after" the other adjustment to be
192                                 // discarded, we do an extra `pop()`
193                                 Some(Adjustment { kind: Adjust::Unsize, .. }) => {
194                                     // So the borrow discard actually happens here
195                                     a.pop();
196                                 },
197                                 _ => {}
198                             }
199                         });
200                     }
201                 },
202                 // Might encounter non-valid indexes at this point, so there
203                 // has to be a fall-through
204                 _ => {},
205             }
206         }
207     }
208 }
209
210
211 ///////////////////////////////////////////////////////////////////////////
212 // Impl of Visitor for Resolver
213 //
214 // This is the master code which walks the AST. It delegates most of
215 // the heavy lifting to the generic visit and resolve functions
216 // below. In general, a function is made into a `visitor` if it must
217 // traffic in node-ids or update tables in the type context etc.
218
219 impl<'cx, 'gcx, 'tcx> Visitor<'gcx> for WritebackCx<'cx, 'gcx, 'tcx> {
220     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'gcx> {
221         NestedVisitorMap::None
222     }
223
224     fn visit_expr(&mut self, e: &'gcx hir::Expr) {
225         self.fix_scalar_builtin_expr(e);
226         self.fix_index_builtin_expr(e);
227
228         self.visit_node_id(e.span, e.hir_id);
229
230         match e.node {
231             hir::ExprKind::Closure(_, _, body, _, _) => {
232                 let body = self.fcx.tcx.hir.body(body);
233                 for arg in &body.arguments {
234                     self.visit_node_id(e.span, arg.hir_id);
235                 }
236
237                 self.visit_body(body);
238             }
239             hir::ExprKind::Struct(_, ref fields, _) => {
240                 for field in fields {
241                     self.visit_field_id(field.id);
242                 }
243             }
244             hir::ExprKind::Field(..) => {
245                 self.visit_field_id(e.id);
246             }
247             _ => {}
248         }
249
250         intravisit::walk_expr(self, e);
251     }
252
253     fn visit_block(&mut self, b: &'gcx hir::Block) {
254         self.visit_node_id(b.span, b.hir_id);
255         intravisit::walk_block(self, b);
256     }
257
258     fn visit_pat(&mut self, p: &'gcx hir::Pat) {
259         match p.node {
260             hir::PatKind::Binding(..) => {
261                 if let Some(&bm) = self.fcx.tables.borrow().pat_binding_modes().get(p.hir_id) {
262                     self.tables.pat_binding_modes_mut().insert(p.hir_id, bm);
263                 } else {
264                     self.tcx().sess.delay_span_bug(p.span, "missing binding mode");
265                 }
266             }
267             hir::PatKind::Struct(_, ref fields, _) => {
268                 for field in fields {
269                     self.visit_field_id(field.node.id);
270                 }
271             }
272             _ => {}
273         };
274
275         self.visit_pat_adjustments(p.span, p.hir_id);
276
277         self.visit_node_id(p.span, p.hir_id);
278         intravisit::walk_pat(self, p);
279     }
280
281     fn visit_local(&mut self, l: &'gcx hir::Local) {
282         intravisit::walk_local(self, l);
283         let var_ty = self.fcx.local_ty(l.span, l.id);
284         let var_ty = self.resolve(&var_ty, &l.span);
285         self.write_ty_to_tables(l.hir_id, var_ty);
286     }
287
288     fn visit_ty(&mut self, hir_ty: &'gcx hir::Ty) {
289         intravisit::walk_ty(self, hir_ty);
290         let ty = self.fcx.node_ty(hir_ty.hir_id);
291         let ty = self.resolve(&ty, &hir_ty.span);
292         self.write_ty_to_tables(hir_ty.hir_id, ty);
293     }
294 }
295
296 impl<'cx, 'gcx, 'tcx> WritebackCx<'cx, 'gcx, 'tcx> {
297     fn visit_upvar_borrow_map(&mut self) {
298         for (upvar_id, upvar_capture) in self.fcx.tables.borrow().upvar_capture_map.iter() {
299             let new_upvar_capture = match *upvar_capture {
300                 ty::UpvarCapture::ByValue => ty::UpvarCapture::ByValue,
301                 ty::UpvarCapture::ByRef(ref upvar_borrow) => {
302                     let r = upvar_borrow.region;
303                     let r = self.resolve(&r, &upvar_id.var_id);
304                     ty::UpvarCapture::ByRef(ty::UpvarBorrow {
305                         kind: upvar_borrow.kind,
306                         region: r,
307                     })
308                 }
309             };
310             debug!(
311                 "Upvar capture for {:?} resolved to {:?}",
312                 upvar_id,
313                 new_upvar_capture
314             );
315             self.tables
316                 .upvar_capture_map
317                 .insert(*upvar_id, new_upvar_capture);
318         }
319     }
320
321     fn visit_closures(&mut self) {
322         let fcx_tables = self.fcx.tables.borrow();
323         debug_assert_eq!(fcx_tables.local_id_root, self.tables.local_id_root);
324         let common_local_id_root = fcx_tables.local_id_root.unwrap();
325
326         for (&id, &origin) in fcx_tables.closure_kind_origins().iter() {
327             let hir_id = hir::HirId {
328                 owner: common_local_id_root.index,
329                 local_id: id,
330             };
331             self.tables
332                 .closure_kind_origins_mut()
333                 .insert(hir_id, origin);
334         }
335     }
336
337     fn visit_cast_types(&mut self) {
338         let fcx_tables = self.fcx.tables.borrow();
339         let fcx_cast_kinds = fcx_tables.cast_kinds();
340         debug_assert_eq!(fcx_tables.local_id_root, self.tables.local_id_root);
341         let mut self_cast_kinds = self.tables.cast_kinds_mut();
342         let common_local_id_root = fcx_tables.local_id_root.unwrap();
343
344         for (&local_id, &cast_kind) in fcx_cast_kinds.iter() {
345             let hir_id = hir::HirId {
346                 owner: common_local_id_root.index,
347                 local_id,
348             };
349             self_cast_kinds.insert(hir_id, cast_kind);
350         }
351     }
352
353     fn visit_free_region_map(&mut self) {
354         let free_region_map = self.tcx()
355             .lift_to_global(&self.fcx.tables.borrow().free_region_map);
356         let free_region_map = free_region_map.expect("all regions in free-region-map are global");
357         self.tables.free_region_map = free_region_map;
358     }
359
360     fn visit_user_provided_tys(&mut self) {
361         let fcx_tables = self.fcx.tables.borrow();
362         debug_assert_eq!(fcx_tables.local_id_root, self.tables.local_id_root);
363         let common_local_id_root = fcx_tables.local_id_root.unwrap();
364
365         for (&local_id, c_ty) in fcx_tables.user_provided_tys().iter() {
366             let hir_id = hir::HirId {
367                 owner: common_local_id_root.index,
368                 local_id,
369             };
370
371             let c_ty = if let Some(c_ty) = self.tcx().lift_to_global(c_ty) {
372                 c_ty
373             } else {
374                 span_bug!(
375                     hir_id.to_span(&self.fcx.tcx),
376                     "writeback: `{:?}` missing from the global type context",
377                     c_ty
378                 );
379             };
380
381             self.tables
382                 .user_provided_tys_mut()
383                 .insert(hir_id, c_ty.clone());
384         }
385     }
386
387     fn visit_anon_types(&mut self, span: Span) {
388         for (&def_id, anon_defn) in self.fcx.anon_types.borrow().iter() {
389             let node_id = self.tcx().hir.as_local_node_id(def_id).unwrap();
390             let instantiated_ty = self.resolve(&anon_defn.concrete_ty, &node_id);
391             let definition_ty = self.fcx.infer_anon_definition_from_instantiation(
392                 def_id,
393                 anon_defn,
394                 instantiated_ty,
395             );
396             let old = self.tables.concrete_existential_types.insert(def_id, definition_ty);
397             if let Some(old) = old {
398                 if old != definition_ty {
399                     span_bug!(
400                         span,
401                         "visit_anon_types tried to write \
402                         different types for the same existential type: {:?}, {:?}, {:?}",
403                         def_id,
404                         definition_ty,
405                         old,
406                     );
407                 }
408             }
409         }
410     }
411
412     fn visit_field_id(&mut self, node_id: ast::NodeId) {
413         let hir_id = self.tcx().hir.node_to_hir_id(node_id);
414         if let Some(index) = self.fcx.tables.borrow_mut().field_indices_mut().remove(hir_id) {
415             self.tables.field_indices_mut().insert(hir_id, index);
416         }
417     }
418
419     fn visit_node_id(&mut self, span: Span, hir_id: hir::HirId) {
420         // Export associated path extensions and method resultions.
421         if let Some(def) = self.fcx
422             .tables
423             .borrow_mut()
424             .type_dependent_defs_mut()
425             .remove(hir_id)
426         {
427             self.tables.type_dependent_defs_mut().insert(hir_id, def);
428         }
429
430         // Resolve any borrowings for the node with id `node_id`
431         self.visit_adjustments(span, hir_id);
432
433         // Resolve the type of the node with id `node_id`
434         let n_ty = self.fcx.node_ty(hir_id);
435         let n_ty = self.resolve(&n_ty, &span);
436         self.write_ty_to_tables(hir_id, n_ty);
437         debug!("Node {:?} has type {:?}", hir_id, n_ty);
438
439         // Resolve any substitutions
440         if let Some(substs) = self.fcx.tables.borrow().node_substs_opt(hir_id) {
441             let substs = self.resolve(&substs, &span);
442             debug!("write_substs_to_tcx({:?}, {:?})", hir_id, substs);
443             assert!(!substs.needs_infer() && !substs.has_skol());
444             self.tables.node_substs_mut().insert(hir_id, substs);
445         }
446     }
447
448     fn visit_adjustments(&mut self, span: Span, hir_id: hir::HirId) {
449         let adjustment = self.fcx
450             .tables
451             .borrow_mut()
452             .adjustments_mut()
453             .remove(hir_id);
454         match adjustment {
455             None => {
456                 debug!("No adjustments for node {:?}", hir_id);
457             }
458
459             Some(adjustment) => {
460                 let resolved_adjustment = self.resolve(&adjustment, &span);
461                 debug!(
462                     "Adjustments for node {:?}: {:?}",
463                     hir_id,
464                     resolved_adjustment
465                 );
466                 self.tables
467                     .adjustments_mut()
468                     .insert(hir_id, resolved_adjustment);
469             }
470         }
471     }
472
473     fn visit_pat_adjustments(&mut self, span: Span, hir_id: hir::HirId) {
474         let adjustment = self.fcx
475             .tables
476             .borrow_mut()
477             .pat_adjustments_mut()
478             .remove(hir_id);
479         match adjustment {
480             None => {
481                 debug!("No pat_adjustments for node {:?}", hir_id);
482             }
483
484             Some(adjustment) => {
485                 let resolved_adjustment = self.resolve(&adjustment, &span);
486                 debug!(
487                     "pat_adjustments for node {:?}: {:?}",
488                     hir_id,
489                     resolved_adjustment
490                 );
491                 self.tables
492                     .pat_adjustments_mut()
493                     .insert(hir_id, resolved_adjustment);
494             }
495         }
496     }
497
498     fn visit_liberated_fn_sigs(&mut self) {
499         let fcx_tables = self.fcx.tables.borrow();
500         debug_assert_eq!(fcx_tables.local_id_root, self.tables.local_id_root);
501         let common_local_id_root = fcx_tables.local_id_root.unwrap();
502
503         for (&local_id, fn_sig) in fcx_tables.liberated_fn_sigs().iter() {
504             let hir_id = hir::HirId {
505                 owner: common_local_id_root.index,
506                 local_id,
507             };
508             let fn_sig = self.resolve(fn_sig, &hir_id);
509             self.tables
510                 .liberated_fn_sigs_mut()
511                 .insert(hir_id, fn_sig.clone());
512         }
513     }
514
515     fn visit_fru_field_types(&mut self) {
516         let fcx_tables = self.fcx.tables.borrow();
517         debug_assert_eq!(fcx_tables.local_id_root, self.tables.local_id_root);
518         let common_local_id_root = fcx_tables.local_id_root.unwrap();
519
520         for (&local_id, ftys) in fcx_tables.fru_field_types().iter() {
521             let hir_id = hir::HirId {
522                 owner: common_local_id_root.index,
523                 local_id,
524             };
525             let ftys = self.resolve(ftys, &hir_id);
526             self.tables.fru_field_types_mut().insert(hir_id, ftys);
527         }
528     }
529
530     fn resolve<T>(&self, x: &T, span: &dyn Locatable) -> T::Lifted
531     where
532         T: TypeFoldable<'tcx> + ty::Lift<'gcx>,
533     {
534         let x = x.fold_with(&mut Resolver::new(self.fcx, span, self.body));
535         if let Some(lifted) = self.tcx().lift_to_global(&x) {
536             lifted
537         } else {
538             span_bug!(
539                 span.to_span(&self.fcx.tcx),
540                 "writeback: `{:?}` missing from the global type context",
541                 x
542             );
543         }
544     }
545 }
546
547 trait Locatable {
548     fn to_span(&self, tcx: &TyCtxt) -> Span;
549 }
550
551 impl Locatable for Span {
552     fn to_span(&self, _: &TyCtxt) -> Span {
553         *self
554     }
555 }
556
557 impl Locatable for ast::NodeId {
558     fn to_span(&self, tcx: &TyCtxt) -> Span {
559         tcx.hir.span(*self)
560     }
561 }
562
563 impl Locatable for DefIndex {
564     fn to_span(&self, tcx: &TyCtxt) -> Span {
565         let node_id = tcx.hir.def_index_to_node_id(*self);
566         tcx.hir.span(node_id)
567     }
568 }
569
570 impl Locatable for hir::HirId {
571     fn to_span(&self, tcx: &TyCtxt) -> Span {
572         let node_id = tcx.hir.hir_to_node_id(*self);
573         tcx.hir.span(node_id)
574     }
575 }
576
577 ///////////////////////////////////////////////////////////////////////////
578 // The Resolver. This is the type folding engine that detects
579 // unresolved types and so forth.
580
581 struct Resolver<'cx, 'gcx: 'cx + 'tcx, 'tcx: 'cx> {
582     tcx: TyCtxt<'cx, 'gcx, 'tcx>,
583     infcx: &'cx InferCtxt<'cx, 'gcx, 'tcx>,
584     span: &'cx dyn Locatable,
585     body: &'gcx hir::Body,
586 }
587
588 impl<'cx, 'gcx, 'tcx> Resolver<'cx, 'gcx, 'tcx> {
589     fn new(
590         fcx: &'cx FnCtxt<'cx, 'gcx, 'tcx>,
591         span: &'cx dyn Locatable,
592         body: &'gcx hir::Body,
593     ) -> Resolver<'cx, 'gcx, 'tcx> {
594         Resolver {
595             tcx: fcx.tcx,
596             infcx: fcx,
597             span,
598             body,
599         }
600     }
601
602     fn report_error(&self, t: Ty<'tcx>) {
603         if !self.tcx.sess.has_errors() {
604             self.infcx
605                 .need_type_info_err(Some(self.body.id()), self.span.to_span(&self.tcx), t).emit();
606         }
607     }
608 }
609
610 impl<'cx, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for Resolver<'cx, 'gcx, 'tcx> {
611     fn tcx<'a>(&'a self) -> TyCtxt<'a, 'gcx, 'tcx> {
612         self.tcx
613     }
614
615     fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
616         match self.infcx.fully_resolve(&t) {
617             Ok(t) => t,
618             Err(_) => {
619                 debug!(
620                     "Resolver::fold_ty: input type `{:?}` not fully resolvable",
621                     t
622                 );
623                 self.report_error(t);
624                 self.tcx().types.err
625             }
626         }
627     }
628
629     // FIXME This should be carefully checked
630     // We could use `self.report_error` but it doesn't accept a ty::Region, right now.
631     fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
632         match self.infcx.fully_resolve(&r) {
633             Ok(r) => r,
634             Err(_) => self.tcx.types.re_static,
635         }
636     }
637 }
638
639 ///////////////////////////////////////////////////////////////////////////
640 // During type check, we store promises with the result of trait
641 // lookup rather than the actual results (because the results are not
642 // necessarily available immediately). These routines unwind the
643 // promises. It is expected that we will have already reported any
644 // errors that may be encountered, so if the promises store an error,
645 // a dummy result is returned.