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