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