]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_typeck/src/check/writeback.rs
Rollup merge of #85766 - workingjubilee:file-options, r=yaahc
[rust.git] / compiler / rustc_typeck / src / check / writeback.rs
1 // Type resolution: the phase that finds all the types in the AST with
2 // unresolved type variables and replaces "ty_var" types with their
3 // substitutions.
4
5 use crate::check::FnCtxt;
6
7 use rustc_data_structures::stable_map::FxHashMap;
8 use rustc_errors::ErrorReported;
9 use rustc_hir as hir;
10 use rustc_hir::def_id::DefId;
11 use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
12 use rustc_infer::infer::error_reporting::TypeAnnotationNeeded::E0282;
13 use rustc_infer::infer::InferCtxt;
14 use rustc_middle::hir::place::Place as HirPlace;
15 use rustc_middle::mir::FakeReadCause;
16 use rustc_middle::ty::adjustment::{Adjust, Adjustment, PointerCast};
17 use rustc_middle::ty::fold::{TypeFoldable, TypeFolder};
18 use rustc_middle::ty::{self, ClosureSizeProfileData, Ty, TyCtxt};
19 use rustc_span::symbol::sym;
20 use rustc_span::Span;
21 use rustc_trait_selection::opaque_types::InferCtxtExt;
22
23 use std::mem;
24
25 ///////////////////////////////////////////////////////////////////////////
26 // Entry point
27
28 // During type inference, partially inferred types are
29 // represented using Type variables (ty::Infer). These don't appear in
30 // the final TypeckResults since all of the types should have been
31 // inferred once typeck is done.
32 // When type inference is running however, having to update the typeck
33 // typeck results every time a new type is inferred would be unreasonably slow,
34 // so instead all of the replacement happens at the end in
35 // resolve_type_vars_in_body, which creates a new TypeTables which
36 // doesn't contain any inference types.
37 impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
38     pub fn resolve_type_vars_in_body(
39         &self,
40         body: &'tcx hir::Body<'tcx>,
41     ) -> &'tcx ty::TypeckResults<'tcx> {
42         let item_id = self.tcx.hir().body_owner(body.id());
43         let item_def_id = self.tcx.hir().local_def_id(item_id);
44
45         // This attribute causes us to dump some writeback information
46         // in the form of errors, which is uSymbol for unit tests.
47         let rustc_dump_user_substs =
48             self.tcx.has_attr(item_def_id.to_def_id(), sym::rustc_dump_user_substs);
49
50         let mut wbcx = WritebackCx::new(self, body, rustc_dump_user_substs);
51         for param in body.params {
52             wbcx.visit_node_id(param.pat.span, param.hir_id);
53         }
54         // Type only exists for constants and statics, not functions.
55         match self.tcx.hir().body_owner_kind(item_id) {
56             hir::BodyOwnerKind::Const | hir::BodyOwnerKind::Static(_) => {
57                 wbcx.visit_node_id(body.value.span, item_id);
58             }
59             hir::BodyOwnerKind::Closure | hir::BodyOwnerKind::Fn => (),
60         }
61         wbcx.visit_body(body);
62         wbcx.visit_min_capture_map();
63         wbcx.eval_closure_size();
64         wbcx.visit_fake_reads_map();
65         wbcx.visit_closures();
66         wbcx.visit_liberated_fn_sigs();
67         wbcx.visit_fru_field_types();
68         wbcx.visit_opaque_types(body.value.span);
69         wbcx.visit_coercion_casts();
70         wbcx.visit_user_provided_tys();
71         wbcx.visit_user_provided_sigs();
72         wbcx.visit_generator_interior_types();
73
74         let used_trait_imports =
75             mem::take(&mut self.typeck_results.borrow_mut().used_trait_imports);
76         debug!("used_trait_imports({:?}) = {:?}", item_def_id, used_trait_imports);
77         wbcx.typeck_results.used_trait_imports = used_trait_imports;
78
79         wbcx.typeck_results.treat_byte_string_as_slice =
80             mem::take(&mut self.typeck_results.borrow_mut().treat_byte_string_as_slice);
81
82         if self.is_tainted_by_errors() {
83             // FIXME(eddyb) keep track of `ErrorReported` from where the error was emitted.
84             wbcx.typeck_results.tainted_by_errors = Some(ErrorReported);
85         }
86
87         debug!("writeback: typeck results for {:?} are {:#?}", item_def_id, wbcx.typeck_results);
88
89         self.tcx.arena.alloc(wbcx.typeck_results)
90     }
91 }
92
93 ///////////////////////////////////////////////////////////////////////////
94 // The Writeback context. This visitor walks the HIR, checking the
95 // fn-specific typeck results to find references to types or regions. It
96 // resolves those regions to remove inference variables and writes the
97 // final result back into the master typeck results in the tcx. Here and
98 // there, it applies a few ad-hoc checks that were not convenient to
99 // do elsewhere.
100
101 struct WritebackCx<'cx, 'tcx> {
102     fcx: &'cx FnCtxt<'cx, 'tcx>,
103
104     typeck_results: ty::TypeckResults<'tcx>,
105
106     body: &'tcx hir::Body<'tcx>,
107
108     rustc_dump_user_substs: bool,
109 }
110
111 impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
112     fn new(
113         fcx: &'cx FnCtxt<'cx, 'tcx>,
114         body: &'tcx hir::Body<'tcx>,
115         rustc_dump_user_substs: bool,
116     ) -> WritebackCx<'cx, 'tcx> {
117         let owner = body.id().hir_id.owner;
118
119         WritebackCx {
120             fcx,
121             typeck_results: ty::TypeckResults::new(owner),
122             body,
123             rustc_dump_user_substs,
124         }
125     }
126
127     fn tcx(&self) -> TyCtxt<'tcx> {
128         self.fcx.tcx
129     }
130
131     fn write_ty_to_typeck_results(&mut self, hir_id: hir::HirId, ty: Ty<'tcx>) {
132         debug!("write_ty_to_typeck_results({:?}, {:?})", hir_id, ty);
133         assert!(!ty.needs_infer() && !ty.has_placeholders() && !ty.has_free_regions(self.tcx()));
134         self.typeck_results.node_types_mut().insert(hir_id, ty);
135     }
136
137     // Hacky hack: During type-checking, we treat *all* operators
138     // as potentially overloaded. But then, during writeback, if
139     // we observe that something like `a+b` is (known to be)
140     // operating on scalars, we clear the overload.
141     fn fix_scalar_builtin_expr(&mut self, e: &hir::Expr<'_>) {
142         match e.kind {
143             hir::ExprKind::Unary(hir::UnOp::Neg | hir::UnOp::Not, inner) => {
144                 let inner_ty = self.fcx.node_ty(inner.hir_id);
145                 let inner_ty = self.fcx.resolve_vars_if_possible(inner_ty);
146
147                 if inner_ty.is_scalar() {
148                     let mut typeck_results = self.fcx.typeck_results.borrow_mut();
149                     typeck_results.type_dependent_defs_mut().remove(e.hir_id);
150                     typeck_results.node_substs_mut().remove(e.hir_id);
151                 }
152             }
153             hir::ExprKind::Binary(ref op, lhs, rhs) | hir::ExprKind::AssignOp(ref op, lhs, rhs) => {
154                 let lhs_ty = self.fcx.node_ty(lhs.hir_id);
155                 let lhs_ty = self.fcx.resolve_vars_if_possible(lhs_ty);
156
157                 let rhs_ty = self.fcx.node_ty(rhs.hir_id);
158                 let rhs_ty = self.fcx.resolve_vars_if_possible(rhs_ty);
159
160                 if lhs_ty.is_scalar() && rhs_ty.is_scalar() {
161                     let mut typeck_results = self.fcx.typeck_results.borrow_mut();
162                     typeck_results.type_dependent_defs_mut().remove(e.hir_id);
163                     typeck_results.node_substs_mut().remove(e.hir_id);
164
165                     match e.kind {
166                         hir::ExprKind::Binary(..) => {
167                             if !op.node.is_by_value() {
168                                 let mut adjustments = typeck_results.adjustments_mut();
169                                 if let Some(a) = adjustments.get_mut(lhs.hir_id) {
170                                     a.pop();
171                                 }
172                                 if let Some(a) = adjustments.get_mut(rhs.hir_id) {
173                                     a.pop();
174                                 }
175                             }
176                         }
177                         hir::ExprKind::AssignOp(..)
178                             if let Some(a) = typeck_results.adjustments_mut().get_mut(lhs.hir_id) =>
179                         {
180                             a.pop();
181                         }
182                         _ => {}
183                     }
184                 }
185             }
186             _ => {}
187         }
188     }
189
190     // Similar to operators, indexing is always assumed to be overloaded
191     // Here, correct cases where an indexing expression can be simplified
192     // to use builtin indexing because the index type is known to be
193     // usize-ish
194     fn fix_index_builtin_expr(&mut self, e: &hir::Expr<'_>) {
195         if let hir::ExprKind::Index(ref base, ref index) = e.kind {
196             let mut typeck_results = self.fcx.typeck_results.borrow_mut();
197
198             // All valid indexing looks like this; might encounter non-valid indexes at this point.
199             let base_ty = typeck_results
200                 .expr_ty_adjusted_opt(base)
201                 .map(|t| self.fcx.resolve_vars_if_possible(t).kind());
202             if base_ty.is_none() {
203                 // When encountering `return [0][0]` outside of a `fn` body we can encounter a base
204                 // that isn't in the type table. We assume more relevant errors have already been
205                 // emitted, so we delay an ICE if none have. (#64638)
206                 self.tcx().sess.delay_span_bug(e.span, &format!("bad base: `{:?}`", base));
207             }
208             if let Some(ty::Ref(_, base_ty, _)) = base_ty {
209                 let index_ty = typeck_results.expr_ty_adjusted_opt(index).unwrap_or_else(|| {
210                     // When encountering `return [0][0]` outside of a `fn` body we would attempt
211                     // to access an unexistend index. We assume that more relevant errors will
212                     // already have been emitted, so we only gate on this with an ICE if no
213                     // error has been emitted. (#64638)
214                     self.fcx.tcx.ty_error_with_message(
215                         e.span,
216                         &format!("bad index {:?} for base: `{:?}`", index, base),
217                     )
218                 });
219                 let index_ty = self.fcx.resolve_vars_if_possible(index_ty);
220
221                 if base_ty.builtin_index().is_some() && index_ty == self.fcx.tcx.types.usize {
222                     // Remove the method call record
223                     typeck_results.type_dependent_defs_mut().remove(e.hir_id);
224                     typeck_results.node_substs_mut().remove(e.hir_id);
225
226                     if let Some(a) = typeck_results.adjustments_mut().get_mut(base.hir_id) {
227                         // Discard the need for a mutable borrow
228
229                         // Extra adjustment made when indexing causes a drop
230                         // of size information - we need to get rid of it
231                         // Since this is "after" the other adjustment to be
232                         // discarded, we do an extra `pop()`
233                         if let Some(Adjustment {
234                             kind: Adjust::Pointer(PointerCast::Unsize), ..
235                         }) = a.pop()
236                         {
237                             // So the borrow discard actually happens here
238                             a.pop();
239                         }
240                     }
241                 }
242             }
243         }
244     }
245 }
246
247 ///////////////////////////////////////////////////////////////////////////
248 // Impl of Visitor for Resolver
249 //
250 // This is the master code which walks the AST. It delegates most of
251 // the heavy lifting to the generic visit and resolve functions
252 // below. In general, a function is made into a `visitor` if it must
253 // traffic in node-ids or update typeck results in the type context etc.
254
255 impl<'cx, 'tcx> Visitor<'tcx> for WritebackCx<'cx, 'tcx> {
256     type Map = intravisit::ErasedMap<'tcx>;
257
258     fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
259         NestedVisitorMap::None
260     }
261
262     fn visit_expr(&mut self, e: &'tcx hir::Expr<'tcx>) {
263         self.fix_scalar_builtin_expr(e);
264         self.fix_index_builtin_expr(e);
265
266         self.visit_node_id(e.span, e.hir_id);
267
268         match e.kind {
269             hir::ExprKind::Closure(_, _, body, _, _) => {
270                 let body = self.fcx.tcx.hir().body(body);
271                 for param in body.params {
272                     self.visit_node_id(e.span, param.hir_id);
273                 }
274
275                 self.visit_body(body);
276             }
277             hir::ExprKind::Struct(_, fields, _) => {
278                 for field in fields {
279                     self.visit_field_id(field.hir_id);
280                 }
281             }
282             hir::ExprKind::Field(..) => {
283                 self.visit_field_id(e.hir_id);
284             }
285             hir::ExprKind::ConstBlock(anon_const) => {
286                 self.visit_node_id(e.span, anon_const.hir_id);
287
288                 let body = self.tcx().hir().body(anon_const.body);
289                 self.visit_body(body);
290             }
291             _ => {}
292         }
293
294         intravisit::walk_expr(self, e);
295     }
296
297     fn visit_block(&mut self, b: &'tcx hir::Block<'tcx>) {
298         self.visit_node_id(b.span, b.hir_id);
299         intravisit::walk_block(self, b);
300     }
301
302     fn visit_pat(&mut self, p: &'tcx hir::Pat<'tcx>) {
303         match p.kind {
304             hir::PatKind::Binding(..) => {
305                 let typeck_results = self.fcx.typeck_results.borrow();
306                 if let Some(bm) =
307                     typeck_results.extract_binding_mode(self.tcx().sess, p.hir_id, p.span)
308                 {
309                     self.typeck_results.pat_binding_modes_mut().insert(p.hir_id, bm);
310                 }
311             }
312             hir::PatKind::Struct(_, fields, _) => {
313                 for field in fields {
314                     self.visit_field_id(field.hir_id);
315                 }
316             }
317             _ => {}
318         };
319
320         self.visit_pat_adjustments(p.span, p.hir_id);
321
322         self.visit_node_id(p.span, p.hir_id);
323         intravisit::walk_pat(self, p);
324     }
325
326     fn visit_local(&mut self, l: &'tcx hir::Local<'tcx>) {
327         intravisit::walk_local(self, l);
328         let var_ty = self.fcx.local_ty(l.span, l.hir_id).decl_ty;
329         let var_ty = self.resolve(var_ty, &l.span);
330         self.write_ty_to_typeck_results(l.hir_id, var_ty);
331     }
332
333     fn visit_ty(&mut self, hir_ty: &'tcx hir::Ty<'tcx>) {
334         intravisit::walk_ty(self, hir_ty);
335         let ty = self.fcx.node_ty(hir_ty.hir_id);
336         let ty = self.resolve(ty, &hir_ty.span);
337         self.write_ty_to_typeck_results(hir_ty.hir_id, ty);
338     }
339
340     fn visit_infer(&mut self, inf: &'tcx hir::InferArg) {
341         intravisit::walk_inf(self, inf);
342         // Ignore cases where the inference is a const.
343         if let Some(ty) = self.fcx.node_ty_opt(inf.hir_id) {
344             let ty = self.resolve(ty, &inf.span);
345             self.write_ty_to_typeck_results(inf.hir_id, ty);
346         }
347     }
348 }
349
350 impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
351     fn eval_closure_size(&mut self) {
352         let mut res: FxHashMap<DefId, ClosureSizeProfileData<'tcx>> = Default::default();
353         for (closure_def_id, data) in self.fcx.typeck_results.borrow().closure_size_eval.iter() {
354             let closure_hir_id =
355                 self.tcx().hir().local_def_id_to_hir_id(closure_def_id.expect_local());
356
357             let data = self.resolve(*data, &closure_hir_id);
358
359             res.insert(*closure_def_id, data);
360         }
361
362         self.typeck_results.closure_size_eval = res;
363     }
364     fn visit_min_capture_map(&mut self) {
365         let mut min_captures_wb = ty::MinCaptureInformationMap::with_capacity_and_hasher(
366             self.fcx.typeck_results.borrow().closure_min_captures.len(),
367             Default::default(),
368         );
369         for (closure_def_id, root_min_captures) in
370             self.fcx.typeck_results.borrow().closure_min_captures.iter()
371         {
372             let mut root_var_map_wb = ty::RootVariableMinCaptureList::with_capacity_and_hasher(
373                 root_min_captures.len(),
374                 Default::default(),
375             );
376             for (var_hir_id, min_list) in root_min_captures.iter() {
377                 let min_list_wb = min_list
378                     .iter()
379                     .map(|captured_place| {
380                         let locatable = captured_place.info.path_expr_id.unwrap_or_else(|| {
381                             self.tcx().hir().local_def_id_to_hir_id(closure_def_id.expect_local())
382                         });
383
384                         self.resolve(captured_place.clone(), &locatable)
385                     })
386                     .collect();
387                 root_var_map_wb.insert(*var_hir_id, min_list_wb);
388             }
389             min_captures_wb.insert(*closure_def_id, root_var_map_wb);
390         }
391
392         self.typeck_results.closure_min_captures = min_captures_wb;
393     }
394
395     fn visit_fake_reads_map(&mut self) {
396         let mut resolved_closure_fake_reads: FxHashMap<
397             DefId,
398             Vec<(HirPlace<'tcx>, FakeReadCause, hir::HirId)>,
399         > = Default::default();
400         for (closure_def_id, fake_reads) in
401             self.fcx.typeck_results.borrow().closure_fake_reads.iter()
402         {
403             let mut resolved_fake_reads = Vec::<(HirPlace<'tcx>, FakeReadCause, hir::HirId)>::new();
404             for (place, cause, hir_id) in fake_reads.iter() {
405                 let locatable =
406                     self.tcx().hir().local_def_id_to_hir_id(closure_def_id.expect_local());
407
408                 let resolved_fake_read = self.resolve(place.clone(), &locatable);
409                 resolved_fake_reads.push((resolved_fake_read, *cause, *hir_id));
410             }
411             resolved_closure_fake_reads.insert(*closure_def_id, resolved_fake_reads);
412         }
413         self.typeck_results.closure_fake_reads = resolved_closure_fake_reads;
414     }
415
416     fn visit_closures(&mut self) {
417         let fcx_typeck_results = self.fcx.typeck_results.borrow();
418         assert_eq!(fcx_typeck_results.hir_owner, self.typeck_results.hir_owner);
419         let common_hir_owner = fcx_typeck_results.hir_owner;
420
421         for (id, origin) in fcx_typeck_results.closure_kind_origins().iter() {
422             let hir_id = hir::HirId { owner: common_hir_owner, local_id: *id };
423             let place_span = origin.0;
424             let place = self.resolve(origin.1.clone(), &place_span);
425             self.typeck_results.closure_kind_origins_mut().insert(hir_id, (place_span, place));
426         }
427     }
428
429     fn visit_coercion_casts(&mut self) {
430         let fcx_typeck_results = self.fcx.typeck_results.borrow();
431         let fcx_coercion_casts = fcx_typeck_results.coercion_casts();
432         assert_eq!(fcx_typeck_results.hir_owner, self.typeck_results.hir_owner);
433
434         for local_id in fcx_coercion_casts {
435             self.typeck_results.set_coercion_cast(*local_id);
436         }
437     }
438
439     fn visit_user_provided_tys(&mut self) {
440         let fcx_typeck_results = self.fcx.typeck_results.borrow();
441         assert_eq!(fcx_typeck_results.hir_owner, self.typeck_results.hir_owner);
442         let common_hir_owner = fcx_typeck_results.hir_owner;
443
444         let mut errors_buffer = Vec::new();
445         for (&local_id, c_ty) in fcx_typeck_results.user_provided_types().iter() {
446             let hir_id = hir::HirId { owner: common_hir_owner, local_id };
447
448             if cfg!(debug_assertions) && c_ty.needs_infer() {
449                 span_bug!(
450                     hir_id.to_span(self.fcx.tcx),
451                     "writeback: `{:?}` has inference variables",
452                     c_ty
453                 );
454             };
455
456             self.typeck_results.user_provided_types_mut().insert(hir_id, *c_ty);
457
458             if let ty::UserType::TypeOf(_, user_substs) = c_ty.value {
459                 if self.rustc_dump_user_substs {
460                     // This is a unit-testing mechanism.
461                     let span = self.tcx().hir().span(hir_id);
462                     // We need to buffer the errors in order to guarantee a consistent
463                     // order when emitting them.
464                     let err = self
465                         .tcx()
466                         .sess
467                         .struct_span_err(span, &format!("user substs: {:?}", user_substs));
468                     err.buffer(&mut errors_buffer);
469                 }
470             }
471         }
472
473         if !errors_buffer.is_empty() {
474             errors_buffer.sort_by_key(|diag| diag.span.primary_span());
475             for diag in errors_buffer.drain(..) {
476                 self.tcx().sess.diagnostic().emit_diagnostic(&diag);
477             }
478         }
479     }
480
481     fn visit_user_provided_sigs(&mut self) {
482         let fcx_typeck_results = self.fcx.typeck_results.borrow();
483         assert_eq!(fcx_typeck_results.hir_owner, self.typeck_results.hir_owner);
484
485         for (&def_id, c_sig) in fcx_typeck_results.user_provided_sigs.iter() {
486             if cfg!(debug_assertions) && c_sig.needs_infer() {
487                 span_bug!(
488                     self.fcx.tcx.hir().span_if_local(def_id).unwrap(),
489                     "writeback: `{:?}` has inference variables",
490                     c_sig
491                 );
492             };
493
494             self.typeck_results.user_provided_sigs.insert(def_id, *c_sig);
495         }
496     }
497
498     fn visit_generator_interior_types(&mut self) {
499         let fcx_typeck_results = self.fcx.typeck_results.borrow();
500         assert_eq!(fcx_typeck_results.hir_owner, self.typeck_results.hir_owner);
501         self.typeck_results.generator_interior_types =
502             fcx_typeck_results.generator_interior_types.clone();
503     }
504
505     #[instrument(skip(self, span), level = "debug")]
506     fn visit_opaque_types(&mut self, span: Span) {
507         let opaque_types = self.fcx.infcx.inner.borrow().opaque_types.clone();
508         for (opaque_type_key, opaque_defn) in opaque_types {
509             let hir_id =
510                 self.tcx().hir().local_def_id_to_hir_id(opaque_type_key.def_id.expect_local());
511             let instantiated_ty = self.resolve(opaque_defn.concrete_ty, &hir_id);
512
513             debug_assert!(!instantiated_ty.has_escaping_bound_vars());
514
515             let opaque_type_key = self.fcx.fully_resolve(opaque_type_key).unwrap();
516
517             // Prevent:
518             // * `fn foo<T>() -> Foo<T>`
519             // * `fn foo<T: Bound + Other>() -> Foo<T>`
520             // from being defining.
521
522             // Also replace all generic params with the ones from the opaque type
523             // definition so that
524             // ```rust
525             // type Foo<T> = impl Baz + 'static;
526             // fn foo<U>() -> Foo<U> { .. }
527             // ```
528             // figures out the concrete type with `U`, but the stored type is with `T`.
529
530             // FIXME: why are we calling this here? This seems too early, and duplicated.
531             let definition_ty = self.fcx.infer_opaque_definition_from_instantiation(
532                 opaque_type_key,
533                 instantiated_ty,
534                 span,
535             );
536
537             let mut skip_add = false;
538
539             if let ty::Opaque(definition_ty_def_id, _substs) = *definition_ty.kind() {
540                 if opaque_defn.origin == hir::OpaqueTyOrigin::TyAlias {
541                     if opaque_type_key.def_id == definition_ty_def_id {
542                         debug!(
543                             "skipping adding concrete definition for opaque type {:?} {:?}",
544                             opaque_defn, opaque_type_key.def_id
545                         );
546                         skip_add = true;
547                     }
548                 }
549             }
550
551             if opaque_type_key.substs.needs_infer() {
552                 span_bug!(span, "{:#?} has inference variables", opaque_type_key.substs)
553             }
554
555             // We only want to add an entry into `concrete_opaque_types`
556             // if we actually found a defining usage of this opaque type.
557             // Otherwise, we do nothing - we'll either find a defining usage
558             // in some other location, or we'll end up emitting an error due
559             // to the lack of defining usage
560             if !skip_add {
561                 self.typeck_results.concrete_opaque_types.insert(opaque_type_key.def_id);
562             }
563         }
564     }
565
566     fn visit_field_id(&mut self, hir_id: hir::HirId) {
567         if let Some(index) = self.fcx.typeck_results.borrow_mut().field_indices_mut().remove(hir_id)
568         {
569             self.typeck_results.field_indices_mut().insert(hir_id, index);
570         }
571     }
572
573     #[instrument(skip(self, span), level = "debug")]
574     fn visit_node_id(&mut self, span: Span, hir_id: hir::HirId) {
575         // Export associated path extensions and method resolutions.
576         if let Some(def) =
577             self.fcx.typeck_results.borrow_mut().type_dependent_defs_mut().remove(hir_id)
578         {
579             self.typeck_results.type_dependent_defs_mut().insert(hir_id, def);
580         }
581
582         // Resolve any borrowings for the node with id `node_id`
583         self.visit_adjustments(span, hir_id);
584
585         // Resolve the type of the node with id `node_id`
586         let n_ty = self.fcx.node_ty(hir_id);
587         let n_ty = self.resolve(n_ty, &span);
588         self.write_ty_to_typeck_results(hir_id, n_ty);
589         debug!(?n_ty);
590
591         // Resolve any substitutions
592         if let Some(substs) = self.fcx.typeck_results.borrow().node_substs_opt(hir_id) {
593             let substs = self.resolve(substs, &span);
594             debug!("write_substs_to_tcx({:?}, {:?})", hir_id, substs);
595             assert!(!substs.needs_infer() && !substs.has_placeholders());
596             self.typeck_results.node_substs_mut().insert(hir_id, substs);
597         }
598     }
599
600     #[instrument(skip(self, span), level = "debug")]
601     fn visit_adjustments(&mut self, span: Span, hir_id: hir::HirId) {
602         let adjustment = self.fcx.typeck_results.borrow_mut().adjustments_mut().remove(hir_id);
603         match adjustment {
604             None => {
605                 debug!("no adjustments for node");
606             }
607
608             Some(adjustment) => {
609                 let resolved_adjustment = self.resolve(adjustment, &span);
610                 debug!(?resolved_adjustment);
611                 self.typeck_results.adjustments_mut().insert(hir_id, resolved_adjustment);
612             }
613         }
614     }
615
616     #[instrument(skip(self, span), level = "debug")]
617     fn visit_pat_adjustments(&mut self, span: Span, hir_id: hir::HirId) {
618         let adjustment = self.fcx.typeck_results.borrow_mut().pat_adjustments_mut().remove(hir_id);
619         match adjustment {
620             None => {
621                 debug!("no pat_adjustments for node");
622             }
623
624             Some(adjustment) => {
625                 let resolved_adjustment = self.resolve(adjustment, &span);
626                 debug!(?resolved_adjustment);
627                 self.typeck_results.pat_adjustments_mut().insert(hir_id, resolved_adjustment);
628             }
629         }
630     }
631
632     fn visit_liberated_fn_sigs(&mut self) {
633         let fcx_typeck_results = self.fcx.typeck_results.borrow();
634         assert_eq!(fcx_typeck_results.hir_owner, self.typeck_results.hir_owner);
635         let common_hir_owner = fcx_typeck_results.hir_owner;
636
637         for (&local_id, &fn_sig) in fcx_typeck_results.liberated_fn_sigs().iter() {
638             let hir_id = hir::HirId { owner: common_hir_owner, local_id };
639             let fn_sig = self.resolve(fn_sig, &hir_id);
640             self.typeck_results.liberated_fn_sigs_mut().insert(hir_id, fn_sig);
641         }
642     }
643
644     fn visit_fru_field_types(&mut self) {
645         let fcx_typeck_results = self.fcx.typeck_results.borrow();
646         assert_eq!(fcx_typeck_results.hir_owner, self.typeck_results.hir_owner);
647         let common_hir_owner = fcx_typeck_results.hir_owner;
648
649         for (&local_id, ftys) in fcx_typeck_results.fru_field_types().iter() {
650             let hir_id = hir::HirId { owner: common_hir_owner, local_id };
651             let ftys = self.resolve(ftys.clone(), &hir_id);
652             self.typeck_results.fru_field_types_mut().insert(hir_id, ftys);
653         }
654     }
655
656     fn resolve<T>(&mut self, x: T, span: &dyn Locatable) -> T
657     where
658         T: TypeFoldable<'tcx>,
659     {
660         let mut resolver = Resolver::new(self.fcx, span, self.body);
661         let x = x.fold_with(&mut resolver);
662         if cfg!(debug_assertions) && x.needs_infer() {
663             span_bug!(span.to_span(self.fcx.tcx), "writeback: `{:?}` has inference variables", x);
664         }
665
666         // We may have introduced e.g. `ty::Error`, if inference failed, make sure
667         // to mark the `TypeckResults` as tainted in that case, so that downstream
668         // users of the typeck results don't produce extra errors, or worse, ICEs.
669         if resolver.replaced_with_error {
670             // FIXME(eddyb) keep track of `ErrorReported` from where the error was emitted.
671             self.typeck_results.tainted_by_errors = Some(ErrorReported);
672         }
673
674         x
675     }
676 }
677
678 crate trait Locatable {
679     fn to_span(&self, tcx: TyCtxt<'_>) -> Span;
680 }
681
682 impl Locatable for Span {
683     fn to_span(&self, _: TyCtxt<'_>) -> Span {
684         *self
685     }
686 }
687
688 impl Locatable for hir::HirId {
689     fn to_span(&self, tcx: TyCtxt<'_>) -> Span {
690         tcx.hir().span(*self)
691     }
692 }
693
694 /// The Resolver. This is the type folding engine that detects
695 /// unresolved types and so forth.
696 struct Resolver<'cx, 'tcx> {
697     tcx: TyCtxt<'tcx>,
698     infcx: &'cx InferCtxt<'cx, 'tcx>,
699     span: &'cx dyn Locatable,
700     body: &'tcx hir::Body<'tcx>,
701
702     /// Set to `true` if any `Ty` or `ty::Const` had to be replaced with an `Error`.
703     replaced_with_error: bool,
704 }
705
706 impl<'cx, 'tcx> Resolver<'cx, 'tcx> {
707     fn new(
708         fcx: &'cx FnCtxt<'cx, 'tcx>,
709         span: &'cx dyn Locatable,
710         body: &'tcx hir::Body<'tcx>,
711     ) -> Resolver<'cx, 'tcx> {
712         Resolver { tcx: fcx.tcx, infcx: fcx, span, body, replaced_with_error: false }
713     }
714
715     fn report_type_error(&self, t: Ty<'tcx>) {
716         if !self.tcx.sess.has_errors() {
717             self.infcx
718                 .emit_inference_failure_err(
719                     Some(self.body.id()),
720                     self.span.to_span(self.tcx),
721                     t.into(),
722                     vec![],
723                     E0282,
724                 )
725                 .emit();
726         }
727     }
728
729     fn report_const_error(&self, c: &'tcx ty::Const<'tcx>) {
730         if !self.tcx.sess.has_errors() {
731             self.infcx
732                 .emit_inference_failure_err(
733                     Some(self.body.id()),
734                     self.span.to_span(self.tcx),
735                     c.into(),
736                     vec![],
737                     E0282,
738                 )
739                 .emit();
740         }
741     }
742 }
743
744 struct EraseEarlyRegions<'tcx> {
745     tcx: TyCtxt<'tcx>,
746 }
747
748 impl<'tcx> TypeFolder<'tcx> for EraseEarlyRegions<'tcx> {
749     fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
750         self.tcx
751     }
752     fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
753         if ty.has_type_flags(ty::TypeFlags::HAS_POTENTIAL_FREE_REGIONS) {
754             ty.super_fold_with(self)
755         } else {
756             ty
757         }
758     }
759     fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
760         if let ty::ReLateBound(..) = r { r } else { self.tcx.lifetimes.re_erased }
761     }
762 }
763
764 impl<'cx, 'tcx> TypeFolder<'tcx> for Resolver<'cx, 'tcx> {
765     fn tcx<'a>(&'a self) -> TyCtxt<'tcx> {
766         self.tcx
767     }
768
769     fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
770         match self.infcx.fully_resolve(t) {
771             Ok(t) => {
772                 // Do not anonymize late-bound regions
773                 // (e.g. keep `for<'a>` named `for<'a>`).
774                 // This allows NLL to generate error messages that
775                 // refer to the higher-ranked lifetime names written by the user.
776                 EraseEarlyRegions { tcx: self.infcx.tcx }.fold_ty(t)
777             }
778             Err(_) => {
779                 debug!("Resolver::fold_ty: input type `{:?}` not fully resolvable", t);
780                 self.report_type_error(t);
781                 self.replaced_with_error = true;
782                 self.tcx().ty_error()
783             }
784         }
785     }
786
787     fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
788         debug_assert!(!r.is_late_bound(), "Should not be resolving bound region.");
789         self.tcx.lifetimes.re_erased
790     }
791
792     fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
793         match self.infcx.fully_resolve(ct) {
794             Ok(ct) => self.infcx.tcx.erase_regions(ct),
795             Err(_) => {
796                 debug!("Resolver::fold_const: input const `{:?}` not fully resolvable", ct);
797                 self.report_const_error(ct);
798                 self.replaced_with_error = true;
799                 self.tcx().const_error(ct.ty)
800             }
801         }
802     }
803 }
804
805 ///////////////////////////////////////////////////////////////////////////
806 // During type check, we store promises with the result of trait
807 // lookup rather than the actual results (because the results are not
808 // necessarily available immediately). These routines unwind the
809 // promises. It is expected that we will have already reported any
810 // errors that may be encountered, so if the promises store an error,
811 // a dummy result is returned.