]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/check/method/confirm.rs
Optimized error reporting for recursive requirements #47720
[rust.git] / src / librustc_typeck / check / method / confirm.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use super::{probe, MethodCallee};
12
13 use astconv::AstConv;
14 use check::{FnCtxt, LvalueOp, callee};
15 use hir::def_id::DefId;
16 use rustc::ty::subst::Substs;
17 use rustc::traits;
18 use rustc::ty::{self, LvaluePreference, NoPreference, PreferMutLvalue, Ty};
19 use rustc::ty::subst::Subst;
20 use rustc::ty::adjustment::{Adjustment, Adjust, AutoBorrow, OverloadedDeref};
21 use rustc::ty::fold::TypeFoldable;
22 use rustc::infer::{self, InferOk};
23 use syntax_pos::Span;
24 use rustc::hir;
25
26 use std::ops::Deref;
27
28 struct ConfirmContext<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
29     fcx: &'a FnCtxt<'a, 'gcx, 'tcx>,
30     span: Span,
31     self_expr: &'gcx hir::Expr,
32     call_expr: &'gcx hir::Expr,
33 }
34
35 impl<'a, 'gcx, 'tcx> Deref for ConfirmContext<'a, 'gcx, 'tcx> {
36     type Target = FnCtxt<'a, 'gcx, 'tcx>;
37     fn deref(&self) -> &Self::Target {
38         &self.fcx
39     }
40 }
41
42 pub struct ConfirmResult<'tcx> {
43     pub callee: MethodCallee<'tcx>,
44     pub illegal_sized_bound: bool,
45 }
46
47 impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
48     pub fn confirm_method(&self,
49                           span: Span,
50                           self_expr: &'gcx hir::Expr,
51                           call_expr: &'gcx hir::Expr,
52                           unadjusted_self_ty: Ty<'tcx>,
53                           pick: probe::Pick<'tcx>,
54                           segment: &hir::PathSegment)
55                           -> ConfirmResult<'tcx> {
56         debug!("confirm(unadjusted_self_ty={:?}, pick={:?}, generic_args={:?})",
57                unadjusted_self_ty,
58                pick,
59                segment.parameters);
60
61         let mut confirm_cx = ConfirmContext::new(self, span, self_expr, call_expr);
62         confirm_cx.confirm(unadjusted_self_ty, pick, segment)
63     }
64 }
65
66 impl<'a, 'gcx, 'tcx> ConfirmContext<'a, 'gcx, 'tcx> {
67     fn new(fcx: &'a FnCtxt<'a, 'gcx, 'tcx>,
68            span: Span,
69            self_expr: &'gcx hir::Expr,
70            call_expr: &'gcx hir::Expr)
71            -> ConfirmContext<'a, 'gcx, 'tcx> {
72         ConfirmContext {
73             fcx,
74             span,
75             self_expr,
76             call_expr,
77         }
78     }
79
80     fn confirm(&mut self,
81                unadjusted_self_ty: Ty<'tcx>,
82                pick: probe::Pick<'tcx>,
83                segment: &hir::PathSegment)
84                -> ConfirmResult<'tcx> {
85         // Adjust the self expression the user provided and obtain the adjusted type.
86         let self_ty = self.adjust_self_ty(unadjusted_self_ty, &pick);
87
88         // Create substitutions for the method's type parameters.
89         let rcvr_substs = self.fresh_receiver_substs(self_ty, &pick);
90         let all_substs = self.instantiate_method_substs(&pick, segment, rcvr_substs);
91
92         debug!("all_substs={:?}", all_substs);
93
94         // Create the final signature for the method, replacing late-bound regions.
95         let (method_sig, method_predicates) = self.instantiate_method_sig(&pick, all_substs);
96
97         // Unify the (adjusted) self type with what the method expects.
98         //
99         // SUBTLE: if we want good error messages, because of "guessing" while matching
100         // traits, no trait system method can be called before this point because they
101         // could alter our Self-type, except for normalizing the receiver from the
102         // signature (which is also done during probing).
103         let method_sig_rcvr =
104             self.normalize_associated_types_in(self.span, &method_sig.inputs()[0]);
105         self.unify_receivers(self_ty, method_sig_rcvr);
106
107         let (method_sig, method_predicates) =
108             self.normalize_associated_types_in(self.span, &(method_sig, method_predicates));
109
110         // Make sure nobody calls `drop()` explicitly.
111         self.enforce_illegal_method_limitations(&pick);
112
113         // If there is a `Self: Sized` bound and `Self` is a trait object, it is possible that
114         // something which derefs to `Self` actually implements the trait and the caller
115         // wanted to make a static dispatch on it but forgot to import the trait.
116         // See test `src/test/ui/issue-35976.rs`.
117         //
118         // In that case, we'll error anyway, but we'll also re-run the search with all traits
119         // in scope, and if we find another method which can be used, we'll output an
120         // appropriate hint suggesting to import the trait.
121         let illegal_sized_bound = self.predicates_require_illegal_sized_bound(&method_predicates);
122
123         // Add any trait/regions obligations specified on the method's type parameters.
124         // We won't add these if we encountered an illegal sized bound, so that we can use
125         // a custom error in that case.
126         if !illegal_sized_bound {
127             let method_ty = self.tcx.mk_fn_ptr(ty::Binder(method_sig));
128             self.add_obligations(method_ty, all_substs, &method_predicates);
129         }
130
131         // Create the final `MethodCallee`.
132         let callee = MethodCallee {
133             def_id: pick.item.def_id,
134             substs: all_substs,
135             sig: method_sig,
136         };
137
138         if let Some(hir::MutMutable) = pick.autoref {
139             self.convert_lvalue_derefs_to_mutable();
140         }
141
142         ConfirmResult { callee, illegal_sized_bound }
143     }
144
145     ///////////////////////////////////////////////////////////////////////////
146     // ADJUSTMENTS
147
148     fn adjust_self_ty(&mut self,
149                       unadjusted_self_ty: Ty<'tcx>,
150                       pick: &probe::Pick<'tcx>)
151                       -> Ty<'tcx> {
152         // Commit the autoderefs by calling `autoderef` again, but this
153         // time writing the results into the various tables.
154         let mut autoderef = self.autoderef(self.span, unadjusted_self_ty);
155         let (_, n) = autoderef.nth(pick.autoderefs).unwrap();
156         assert_eq!(n, pick.autoderefs);
157
158         let mut adjustments = autoderef.adjust_steps(LvaluePreference::NoPreference);
159
160         let mut target = autoderef.unambiguous_final_ty();
161
162         if let Some(mutbl) = pick.autoref {
163             let region = self.next_region_var(infer::Autoref(self.span));
164             target = self.tcx.mk_ref(region, ty::TypeAndMut {
165                 mutbl,
166                 ty: target
167             });
168             adjustments.push(Adjustment {
169                 kind: Adjust::Borrow(AutoBorrow::Ref(region, mutbl)),
170                 target
171             });
172
173             if let Some(unsize_target) = pick.unsize {
174                 target = self.tcx.mk_ref(region, ty::TypeAndMut {
175                     mutbl,
176                     ty: unsize_target
177                 });
178                 adjustments.push(Adjustment {
179                     kind: Adjust::Unsize,
180                     target
181                 });
182             }
183         } else {
184             // No unsizing should be performed without autoref (at
185             // least during method dispach). This is because we
186             // currently only unsize `[T;N]` to `[T]`, and naturally
187             // that must occur being a reference.
188             assert!(pick.unsize.is_none());
189         }
190
191         autoderef.finalize();
192
193         // Write out the final adjustments.
194         self.apply_adjustments(self.self_expr, adjustments);
195
196         target
197     }
198
199     ///////////////////////////////////////////////////////////////////////////
200     //
201
202     /// Returns a set of substitutions for the method *receiver* where all type and region
203     /// parameters are instantiated with fresh variables. This substitution does not include any
204     /// parameters declared on the method itself.
205     ///
206     /// Note that this substitution may include late-bound regions from the impl level. If so,
207     /// these are instantiated later in the `instantiate_method_sig` routine.
208     fn fresh_receiver_substs(&mut self,
209                              self_ty: Ty<'tcx>,
210                              pick: &probe::Pick<'tcx>)
211                              -> &'tcx Substs<'tcx> {
212         match pick.kind {
213             probe::InherentImplPick => {
214                 let impl_def_id = pick.item.container.id();
215                 assert!(self.tcx.impl_trait_ref(impl_def_id).is_none(),
216                         "impl {:?} is not an inherent impl",
217                         impl_def_id);
218                 self.impl_self_ty(self.span, impl_def_id).substs
219             }
220
221             probe::ObjectPick => {
222                 let trait_def_id = pick.item.container.id();
223                 self.extract_existential_trait_ref(self_ty, |this, object_ty, principal| {
224                     // The object data has no entry for the Self
225                     // Type. For the purposes of this method call, we
226                     // substitute the object type itself. This
227                     // wouldn't be a sound substitution in all cases,
228                     // since each instance of the object type is a
229                     // different existential and hence could match
230                     // distinct types (e.g., if `Self` appeared as an
231                     // argument type), but those cases have already
232                     // been ruled out when we deemed the trait to be
233                     // "object safe".
234                     let original_poly_trait_ref = principal.with_self_ty(this.tcx, object_ty);
235                     let upcast_poly_trait_ref = this.upcast(original_poly_trait_ref, trait_def_id);
236                     let upcast_trait_ref =
237                         this.replace_late_bound_regions_with_fresh_var(&upcast_poly_trait_ref);
238                     debug!("original_poly_trait_ref={:?} upcast_trait_ref={:?} target_trait={:?}",
239                            original_poly_trait_ref,
240                            upcast_trait_ref,
241                            trait_def_id);
242                     upcast_trait_ref.substs
243                 })
244             }
245
246             probe::TraitPick => {
247                 let trait_def_id = pick.item.container.id();
248
249                 // Make a trait reference `$0 : Trait<$1...$n>`
250                 // consisting entirely of type variables. Later on in
251                 // the process we will unify the transformed-self-type
252                 // of the method with the actual type in order to
253                 // unify some of these variables.
254                 self.fresh_substs_for_item(self.span, trait_def_id)
255             }
256
257             probe::WhereClausePick(ref poly_trait_ref) => {
258                 // Where clauses can have bound regions in them. We need to instantiate
259                 // those to convert from a poly-trait-ref to a trait-ref.
260                 self.replace_late_bound_regions_with_fresh_var(&poly_trait_ref).substs
261             }
262         }
263     }
264
265     fn extract_existential_trait_ref<R, F>(&mut self, self_ty: Ty<'tcx>, mut closure: F) -> R
266         where F: FnMut(&mut ConfirmContext<'a, 'gcx, 'tcx>,
267                        Ty<'tcx>,
268                        ty::PolyExistentialTraitRef<'tcx>)
269                        -> R
270     {
271         // If we specified that this is an object method, then the
272         // self-type ought to be something that can be dereferenced to
273         // yield an object-type (e.g., `&Object` or `Box<Object>`
274         // etc).
275
276         // FIXME: this feels, like, super dubious
277         self.fcx
278             .autoderef(self.span, self_ty)
279             .include_raw_pointers()
280             .filter_map(|(ty, _)| {
281                 match ty.sty {
282                     ty::TyDynamic(ref data, ..) => data.principal().map(|p| closure(self, ty, p)),
283                     _ => None,
284                 }
285             })
286             .next()
287             .unwrap_or_else(|| {
288                 span_bug!(self.span,
289                           "self-type `{}` for ObjectPick never dereferenced to an object",
290                           self_ty)
291             })
292     }
293
294     fn instantiate_method_substs(&mut self,
295                                  pick: &probe::Pick<'tcx>,
296                                  segment: &hir::PathSegment,
297                                  parent_substs: &Substs<'tcx>)
298                                  -> &'tcx Substs<'tcx> {
299         // Determine the values for the generic parameters of the method.
300         // If they were not explicitly supplied, just construct fresh
301         // variables.
302         let method_generics = self.tcx.generics_of(pick.item.def_id);
303         let mut fn_segment = Some((segment, method_generics));
304         self.fcx.check_path_parameter_count(self.span, &mut fn_segment, true);
305
306         // Create subst for early-bound lifetime parameters, combining
307         // parameters from the type and those from the method.
308         assert_eq!(method_generics.parent_count(), parent_substs.len());
309         let provided = &segment.parameters;
310         Substs::for_item(self.tcx, pick.item.def_id, |def, _| {
311             let i = def.index as usize;
312             if i < parent_substs.len() {
313                 parent_substs.region_at(i)
314             } else if let Some(lifetime)
315                     = provided.as_ref().and_then(|p| p.lifetimes.get(i - parent_substs.len())) {
316                 AstConv::ast_region_to_region(self.fcx, lifetime, Some(def))
317             } else {
318                 self.region_var_for_def(self.span, def)
319             }
320         }, |def, cur_substs| {
321             let i = def.index as usize;
322             if i < parent_substs.len() {
323                 parent_substs.type_at(i)
324             } else if let Some(ast_ty)
325                 = provided.as_ref().and_then(|p| {
326                     p.types.get(i - parent_substs.len() - method_generics.regions.len())
327                 })
328             {
329                 self.to_ty(ast_ty)
330             } else {
331                 self.type_var_for_def(self.span, def, cur_substs)
332             }
333         })
334     }
335
336     fn unify_receivers(&mut self, self_ty: Ty<'tcx>, method_self_ty: Ty<'tcx>) {
337         match self.at(&self.misc(self.span), self.param_env).sup(method_self_ty, self_ty) {
338             Ok(InferOk { obligations, value: () }) => {
339                 self.register_predicates(obligations);
340             }
341             Err(_) => {
342                 span_bug!(self.span,
343                           "{} was a subtype of {} but now is not?",
344                           self_ty,
345                           method_self_ty);
346             }
347         }
348     }
349
350     ///////////////////////////////////////////////////////////////////////////
351     //
352
353     // NOTE: this returns the *unnormalized* predicates and method sig. Because of
354     // inference guessing, the predicates and method signature can't be normalized
355     // until we unify the `Self` type.
356     fn instantiate_method_sig(&mut self,
357                               pick: &probe::Pick<'tcx>,
358                               all_substs: &'tcx Substs<'tcx>)
359                               -> (ty::FnSig<'tcx>, ty::InstantiatedPredicates<'tcx>) {
360         debug!("instantiate_method_sig(pick={:?}, all_substs={:?})",
361                pick,
362                all_substs);
363
364         // Instantiate the bounds on the method with the
365         // type/early-bound-regions substitutions performed. There can
366         // be no late-bound regions appearing here.
367         let def_id = pick.item.def_id;
368         let method_predicates = self.tcx.predicates_of(def_id)
369                                     .instantiate(self.tcx, all_substs);
370
371         debug!("method_predicates after subst = {:?}", method_predicates);
372
373         let sig = self.tcx.fn_sig(def_id);
374
375         // Instantiate late-bound regions and substitute the trait
376         // parameters into the method type to get the actual method type.
377         //
378         // NB: Instantiate late-bound regions first so that
379         // `instantiate_type_scheme` can normalize associated types that
380         // may reference those regions.
381         let method_sig = self.replace_late_bound_regions_with_fresh_var(&sig);
382         debug!("late-bound lifetimes from method instantiated, method_sig={:?}",
383                method_sig);
384
385         let method_sig = method_sig.subst(self.tcx, all_substs);
386         debug!("type scheme substituted, method_sig={:?}", method_sig);
387
388         (method_sig, method_predicates)
389     }
390
391     fn add_obligations(&mut self,
392                        fty: Ty<'tcx>,
393                        all_substs: &Substs<'tcx>,
394                        method_predicates: &ty::InstantiatedPredicates<'tcx>) {
395         debug!("add_obligations: fty={:?} all_substs={:?} method_predicates={:?}",
396                fty,
397                all_substs,
398                method_predicates);
399
400         self.add_obligations_for_parameters(traits::ObligationCause::misc(self.span, self.body_id),
401                                             method_predicates);
402
403         // this is a projection from a trait reference, so we have to
404         // make sure that the trait reference inputs are well-formed.
405         self.add_wf_bounds(all_substs, self.call_expr);
406
407         // the function type must also be well-formed (this is not
408         // implied by the substs being well-formed because of inherent
409         // impls and late-bound regions - see issue #28609).
410         self.register_wf_obligation(fty, self.span, traits::MiscObligation);
411     }
412
413     ///////////////////////////////////////////////////////////////////////////
414     // RECONCILIATION
415
416     /// When we select a method with a mutable autoref, we have to go convert any
417     /// auto-derefs, indices, etc from `Deref` and `Index` into `DerefMut` and `IndexMut`
418     /// respectively.
419     fn convert_lvalue_derefs_to_mutable(&self) {
420         // Gather up expressions we want to munge.
421         let mut exprs = Vec::new();
422         exprs.push(self.self_expr);
423         loop {
424             let last = exprs[exprs.len() - 1];
425             match last.node {
426                 hir::ExprField(ref expr, _) |
427                 hir::ExprTupField(ref expr, _) |
428                 hir::ExprIndex(ref expr, _) |
429                 hir::ExprUnary(hir::UnDeref, ref expr) => exprs.push(&expr),
430                 _ => break,
431             }
432         }
433
434         debug!("convert_lvalue_derefs_to_mutable: exprs={:?}", exprs);
435
436         // Fix up autoderefs and derefs.
437         for (i, &expr) in exprs.iter().rev().enumerate() {
438             debug!("convert_lvalue_derefs_to_mutable: i={} expr={:?}", i, expr);
439
440             // Fix up the autoderefs. Autorefs can only occur immediately preceding
441             // overloaded lvalue ops, and will be fixed by them in order to get
442             // the correct region.
443             let mut source = self.node_ty(expr.hir_id);
444             // Do not mutate adjustments in place, but rather take them,
445             // and replace them after mutating them, to avoid having the
446             // tables borrowed during (`deref_mut`) method resolution.
447             let previous_adjustments = self.tables
448                                            .borrow_mut()
449                                            .adjustments_mut()
450                                            .remove(expr.hir_id);
451             if let Some(mut adjustments) = previous_adjustments {
452                 let pref = LvaluePreference::PreferMutLvalue;
453                 for adjustment in &mut adjustments {
454                     if let Adjust::Deref(Some(ref mut deref)) = adjustment.kind {
455                         if let Some(ok) = self.try_overloaded_deref(expr.span, source, pref) {
456                             let method = self.register_infer_ok_obligations(ok);
457                             if let ty::TyRef(region, mt) = method.sig.output().sty {
458                                 *deref = OverloadedDeref {
459                                     region,
460                                     mutbl: mt.mutbl
461                                 };
462                             }
463                         }
464                     }
465                     source = adjustment.target;
466                 }
467                 self.tables.borrow_mut().adjustments_mut().insert(expr.hir_id, adjustments);
468             }
469
470             match expr.node {
471                 hir::ExprIndex(ref base_expr, ref index_expr) => {
472                     let index_expr_ty = self.node_ty(index_expr.hir_id);
473                     self.convert_lvalue_op_to_mutable(
474                         LvalueOp::Index, expr, base_expr, &[index_expr_ty]);
475                 }
476                 hir::ExprUnary(hir::UnDeref, ref base_expr) => {
477                     self.convert_lvalue_op_to_mutable(
478                         LvalueOp::Deref, expr, base_expr, &[]);
479                 }
480                 _ => {}
481             }
482         }
483     }
484
485     fn convert_lvalue_op_to_mutable(&self,
486                                     op: LvalueOp,
487                                     expr: &hir::Expr,
488                                     base_expr: &hir::Expr,
489                                     arg_tys: &[Ty<'tcx>])
490     {
491         debug!("convert_lvalue_op_to_mutable({:?}, {:?}, {:?}, {:?})",
492                op, expr, base_expr, arg_tys);
493         if !self.tables.borrow().is_method_call(expr) {
494             debug!("convert_lvalue_op_to_mutable - builtin, nothing to do");
495             return
496         }
497
498         let base_ty = self.tables.borrow().expr_adjustments(base_expr).last()
499             .map_or_else(|| self.node_ty(expr.hir_id), |adj| adj.target);
500         let base_ty = self.resolve_type_vars_if_possible(&base_ty);
501
502         // Need to deref because overloaded lvalue ops take self by-reference.
503         let base_ty = base_ty.builtin_deref(false, NoPreference)
504             .expect("lvalue op takes something that is not a ref")
505             .ty;
506
507         let method = self.try_overloaded_lvalue_op(
508             expr.span, base_ty, arg_tys, PreferMutLvalue, op);
509         let method = match method {
510             Some(ok) => self.register_infer_ok_obligations(ok),
511             None => return self.tcx.sess.delay_span_bug(expr.span, "re-trying op failed")
512         };
513         debug!("convert_lvalue_op_to_mutable: method={:?}", method);
514         self.write_method_call(expr.hir_id, method);
515
516         let (region, mutbl) = if let ty::TyRef(r, mt) = method.sig.inputs()[0].sty {
517             (r, mt.mutbl)
518         } else {
519             span_bug!(expr.span, "input to lvalue op is not a ref?");
520         };
521
522         // Convert the autoref in the base expr to mutable with the correct
523         // region and mutability.
524         let base_expr_ty = self.node_ty(base_expr.hir_id);
525         if let Some(adjustments) = self.tables
526                                        .borrow_mut()
527                                        .adjustments_mut()
528                                        .get_mut(base_expr.hir_id) {
529             let mut source = base_expr_ty;
530             for adjustment in &mut adjustments[..] {
531                 if let Adjust::Borrow(AutoBorrow::Ref(..)) = adjustment.kind {
532                     debug!("convert_lvalue_op_to_mutable: converting autoref {:?}", adjustment);
533                     adjustment.kind = Adjust::Borrow(AutoBorrow::Ref(region, mutbl));
534                     adjustment.target = self.tcx.mk_ref(region, ty::TypeAndMut {
535                         ty: source,
536                         mutbl
537                     });
538                 }
539                 source = adjustment.target;
540             }
541
542             // If we have an autoref followed by unsizing at the end, fix the unsize target.
543             match adjustments[..] {
544                 [.., Adjustment { kind: Adjust::Borrow(AutoBorrow::Ref(..)), .. },
545                  Adjustment { kind: Adjust::Unsize, ref mut target }] => {
546                     *target = method.sig.inputs()[0];
547                 }
548                 _ => {}
549             }
550         }
551     }
552
553     ///////////////////////////////////////////////////////////////////////////
554     // MISCELLANY
555
556     fn predicates_require_illegal_sized_bound(&self,
557                                               predicates: &ty::InstantiatedPredicates<'tcx>)
558                                               -> bool {
559         let sized_def_id = match self.tcx.lang_items().sized_trait() {
560             Some(def_id) => def_id,
561             None => return false,
562         };
563
564         traits::elaborate_predicates(self.tcx, predicates.predicates.clone())
565             .filter_map(|predicate| {
566                 match predicate {
567                     ty::Predicate::Trait(trait_pred) if trait_pred.def_id() == sized_def_id =>
568                         Some(trait_pred),
569                     _ => None,
570                 }
571             })
572             .any(|trait_pred| {
573                 match trait_pred.0.self_ty().sty {
574                     ty::TyDynamic(..) => true,
575                     _ => false,
576                 }
577             })
578     }
579
580     fn enforce_illegal_method_limitations(&self, pick: &probe::Pick) {
581         // Disallow calls to the method `drop` defined in the `Drop` trait.
582         match pick.item.container {
583             ty::TraitContainer(trait_def_id) => {
584                 callee::check_legal_trait_for_method_call(self.tcx, self.span, trait_def_id)
585             }
586             ty::ImplContainer(..) => {}
587         }
588     }
589
590     fn upcast(&mut self,
591               source_trait_ref: ty::PolyTraitRef<'tcx>,
592               target_trait_def_id: DefId)
593               -> ty::PolyTraitRef<'tcx> {
594         let upcast_trait_refs = self.tcx
595             .upcast_choices(source_trait_ref.clone(), target_trait_def_id);
596
597         // must be exactly one trait ref or we'd get an ambig error etc
598         if upcast_trait_refs.len() != 1 {
599             span_bug!(self.span,
600                       "cannot uniquely upcast `{:?}` to `{:?}`: `{:?}`",
601                       source_trait_ref,
602                       target_trait_def_id,
603                       upcast_trait_refs);
604         }
605
606         upcast_trait_refs.into_iter().next().unwrap()
607     }
608
609     fn replace_late_bound_regions_with_fresh_var<T>(&self, value: &ty::Binder<T>) -> T
610         where T: TypeFoldable<'tcx>
611     {
612         self.fcx
613             .replace_late_bound_regions_with_fresh_var(self.span, infer::FnCall, value)
614             .0
615     }
616 }