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