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