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