• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use super::{probe, MethodCallee};
2 
3 use crate::{callee, FnCtxt};
4 use rustc_hir as hir;
5 use rustc_hir::def_id::DefId;
6 use rustc_hir::GenericArg;
7 use rustc_hir_analysis::astconv::generics::{
8     check_generic_arg_count_for_call, create_substs_for_generic_args,
9 };
10 use rustc_hir_analysis::astconv::{AstConv, CreateSubstsForGenericArgsCtxt, IsMethodCall};
11 use rustc_infer::infer::{self, DefineOpaqueTypes, InferOk};
12 use rustc_middle::traits::{ObligationCauseCode, UnifyReceiverContext};
13 use rustc_middle::ty::adjustment::{Adjust, Adjustment, PointerCoercion};
14 use rustc_middle::ty::adjustment::{AllowTwoPhase, AutoBorrow, AutoBorrowMutability};
15 use rustc_middle::ty::fold::TypeFoldable;
16 use rustc_middle::ty::subst::{self, SubstsRef};
17 use rustc_middle::ty::{self, GenericParamDefKind, Ty, TyCtxt};
18 use rustc_middle::ty::{InternalSubsts, UserSubsts, UserType};
19 use rustc_span::{Span, DUMMY_SP};
20 use rustc_trait_selection::traits;
21 
22 use std::ops::Deref;
23 
24 struct ConfirmContext<'a, 'tcx> {
25     fcx: &'a FnCtxt<'a, 'tcx>,
26     span: Span,
27     self_expr: &'tcx hir::Expr<'tcx>,
28     call_expr: &'tcx hir::Expr<'tcx>,
29     skip_record_for_diagnostics: bool,
30 }
31 
32 impl<'a, 'tcx> Deref for ConfirmContext<'a, 'tcx> {
33     type Target = FnCtxt<'a, 'tcx>;
deref(&self) -> &Self::Target34     fn deref(&self) -> &Self::Target {
35         self.fcx
36     }
37 }
38 
39 #[derive(Debug)]
40 pub struct ConfirmResult<'tcx> {
41     pub callee: MethodCallee<'tcx>,
42     pub illegal_sized_bound: Option<Span>,
43 }
44 
45 impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
confirm_method( &self, span: Span, self_expr: &'tcx hir::Expr<'tcx>, call_expr: &'tcx hir::Expr<'tcx>, unadjusted_self_ty: Ty<'tcx>, pick: &probe::Pick<'tcx>, segment: &hir::PathSegment<'_>, ) -> ConfirmResult<'tcx>46     pub fn confirm_method(
47         &self,
48         span: Span,
49         self_expr: &'tcx hir::Expr<'tcx>,
50         call_expr: &'tcx hir::Expr<'tcx>,
51         unadjusted_self_ty: Ty<'tcx>,
52         pick: &probe::Pick<'tcx>,
53         segment: &hir::PathSegment<'_>,
54     ) -> ConfirmResult<'tcx> {
55         debug!(
56             "confirm(unadjusted_self_ty={:?}, pick={:?}, generic_args={:?})",
57             unadjusted_self_ty, pick, segment.args,
58         );
59 
60         let mut confirm_cx = ConfirmContext::new(self, span, self_expr, call_expr);
61         confirm_cx.confirm(unadjusted_self_ty, pick, segment)
62     }
63 
confirm_method_for_diagnostic( &self, span: Span, self_expr: &'tcx hir::Expr<'tcx>, call_expr: &'tcx hir::Expr<'tcx>, unadjusted_self_ty: Ty<'tcx>, pick: &probe::Pick<'tcx>, segment: &hir::PathSegment<'_>, ) -> ConfirmResult<'tcx>64     pub fn confirm_method_for_diagnostic(
65         &self,
66         span: Span,
67         self_expr: &'tcx hir::Expr<'tcx>,
68         call_expr: &'tcx hir::Expr<'tcx>,
69         unadjusted_self_ty: Ty<'tcx>,
70         pick: &probe::Pick<'tcx>,
71         segment: &hir::PathSegment<'_>,
72     ) -> ConfirmResult<'tcx> {
73         let mut confirm_cx = ConfirmContext::new(self, span, self_expr, call_expr);
74         confirm_cx.skip_record_for_diagnostics = true;
75         confirm_cx.confirm(unadjusted_self_ty, pick, segment)
76     }
77 }
78 
79 impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
new( fcx: &'a FnCtxt<'a, 'tcx>, span: Span, self_expr: &'tcx hir::Expr<'tcx>, call_expr: &'tcx hir::Expr<'tcx>, ) -> ConfirmContext<'a, 'tcx>80     fn new(
81         fcx: &'a FnCtxt<'a, 'tcx>,
82         span: Span,
83         self_expr: &'tcx hir::Expr<'tcx>,
84         call_expr: &'tcx hir::Expr<'tcx>,
85     ) -> ConfirmContext<'a, 'tcx> {
86         ConfirmContext { fcx, span, self_expr, call_expr, skip_record_for_diagnostics: false }
87     }
88 
confirm( &mut self, unadjusted_self_ty: Ty<'tcx>, pick: &probe::Pick<'tcx>, segment: &hir::PathSegment<'_>, ) -> ConfirmResult<'tcx>89     fn confirm(
90         &mut self,
91         unadjusted_self_ty: Ty<'tcx>,
92         pick: &probe::Pick<'tcx>,
93         segment: &hir::PathSegment<'_>,
94     ) -> ConfirmResult<'tcx> {
95         // Adjust the self expression the user provided and obtain the adjusted type.
96         let self_ty = self.adjust_self_ty(unadjusted_self_ty, &pick);
97 
98         // Create substitutions for the method's type parameters.
99         let rcvr_substs = self.fresh_receiver_substs(self_ty, &pick);
100         let all_substs = self.instantiate_method_substs(&pick, segment, rcvr_substs);
101 
102         debug!("rcvr_substs={rcvr_substs:?}, all_substs={all_substs:?}");
103 
104         // Create the final signature for the method, replacing late-bound regions.
105         let (method_sig, method_predicates) = self.instantiate_method_sig(&pick, all_substs);
106 
107         // If there is a `Self: Sized` bound and `Self` is a trait object, it is possible that
108         // something which derefs to `Self` actually implements the trait and the caller
109         // wanted to make a static dispatch on it but forgot to import the trait.
110         // See test `tests/ui/issue-35976.rs`.
111         //
112         // In that case, we'll error anyway, but we'll also re-run the search with all traits
113         // in scope, and if we find another method which can be used, we'll output an
114         // appropriate hint suggesting to import the trait.
115         let filler_substs = rcvr_substs
116             .extend_to(self.tcx, pick.item.def_id, |def, _| self.tcx.mk_param_from_def(def));
117         let illegal_sized_bound = self.predicates_require_illegal_sized_bound(
118             self.tcx.predicates_of(pick.item.def_id).instantiate(self.tcx, filler_substs),
119         );
120 
121         // Unify the (adjusted) self type with what the method expects.
122         //
123         // SUBTLE: if we want good error messages, because of "guessing" while matching
124         // traits, no trait system method can be called before this point because they
125         // could alter our Self-type, except for normalizing the receiver from the
126         // signature (which is also done during probing).
127         let method_sig_rcvr = self.normalize(self.span, method_sig.inputs()[0]);
128         debug!(
129             "confirm: self_ty={:?} method_sig_rcvr={:?} method_sig={:?} method_predicates={:?}",
130             self_ty, method_sig_rcvr, method_sig, method_predicates
131         );
132         self.unify_receivers(self_ty, method_sig_rcvr, &pick, all_substs);
133 
134         let (method_sig, method_predicates) =
135             self.normalize(self.span, (method_sig, method_predicates));
136         let method_sig = ty::Binder::dummy(method_sig);
137 
138         // Make sure nobody calls `drop()` explicitly.
139         self.enforce_illegal_method_limitations(&pick);
140 
141         // Add any trait/regions obligations specified on the method's type parameters.
142         // We won't add these if we encountered an illegal sized bound, so that we can use
143         // a custom error in that case.
144         if illegal_sized_bound.is_none() {
145             self.add_obligations(
146                 Ty::new_fn_ptr(self.tcx, method_sig),
147                 all_substs,
148                 method_predicates,
149                 pick.item.def_id,
150             );
151         }
152 
153         // Create the final `MethodCallee`.
154         let callee = MethodCallee {
155             def_id: pick.item.def_id,
156             substs: all_substs,
157             sig: method_sig.skip_binder(),
158         };
159         ConfirmResult { callee, illegal_sized_bound }
160     }
161 
162     ///////////////////////////////////////////////////////////////////////////
163     // ADJUSTMENTS
164 
adjust_self_ty( &mut self, unadjusted_self_ty: Ty<'tcx>, pick: &probe::Pick<'tcx>, ) -> Ty<'tcx>165     fn adjust_self_ty(
166         &mut self,
167         unadjusted_self_ty: Ty<'tcx>,
168         pick: &probe::Pick<'tcx>,
169     ) -> Ty<'tcx> {
170         // Commit the autoderefs by calling `autoderef` again, but this
171         // time writing the results into the various typeck results.
172         let mut autoderef = self.autoderef(self.call_expr.span, unadjusted_self_ty);
173         let Some((ty, n)) = autoderef.nth(pick.autoderefs) else {
174             return Ty::new_error_with_message(self.tcx,
175                 rustc_span::DUMMY_SP,
176                 format!("failed autoderef {}", pick.autoderefs),
177             );
178         };
179         assert_eq!(n, pick.autoderefs);
180 
181         let mut adjustments = self.adjust_steps(&autoderef);
182         let mut target = self.structurally_resolve_type(autoderef.span(), ty);
183 
184         match pick.autoref_or_ptr_adjustment {
185             Some(probe::AutorefOrPtrAdjustment::Autoref { mutbl, unsize }) => {
186                 let region = self.next_region_var(infer::Autoref(self.span));
187                 // Type we're wrapping in a reference, used later for unsizing
188                 let base_ty = target;
189 
190                 target = Ty::new_ref(self.tcx, region, ty::TypeAndMut { mutbl, ty: target });
191 
192                 // Method call receivers are the primary use case
193                 // for two-phase borrows.
194                 let mutbl = AutoBorrowMutability::new(mutbl, AllowTwoPhase::Yes);
195 
196                 adjustments.push(Adjustment {
197                     kind: Adjust::Borrow(AutoBorrow::Ref(region, mutbl)),
198                     target,
199                 });
200 
201                 if unsize {
202                     let unsized_ty = if let ty::Array(elem_ty, _) = base_ty.kind() {
203                         Ty::new_slice(self.tcx, *elem_ty)
204                     } else {
205                         bug!(
206                             "AutorefOrPtrAdjustment's unsize flag should only be set for array ty, found {}",
207                             base_ty
208                         )
209                     };
210                     target = Ty::new_ref(
211                         self.tcx,
212                         region,
213                         ty::TypeAndMut { mutbl: mutbl.into(), ty: unsized_ty },
214                     );
215                     adjustments.push(Adjustment {
216                         kind: Adjust::Pointer(PointerCoercion::Unsize),
217                         target,
218                     });
219                 }
220             }
221             Some(probe::AutorefOrPtrAdjustment::ToConstPtr) => {
222                 target = match target.kind() {
223                     &ty::RawPtr(ty::TypeAndMut { ty, mutbl }) => {
224                         assert!(mutbl.is_mut());
225                         Ty::new_ptr(self.tcx, ty::TypeAndMut { mutbl: hir::Mutability::Not, ty })
226                     }
227                     other => panic!("Cannot adjust receiver type {:?} to const ptr", other),
228                 };
229 
230                 adjustments.push(Adjustment {
231                     kind: Adjust::Pointer(PointerCoercion::MutToConstPointer),
232                     target,
233                 });
234             }
235             None => {}
236         }
237 
238         self.register_predicates(autoderef.into_obligations());
239 
240         // Write out the final adjustments.
241         if !self.skip_record_for_diagnostics {
242             self.apply_adjustments(self.self_expr, adjustments);
243         }
244 
245         target
246     }
247 
248     /// Returns a set of substitutions for the method *receiver* where all type and region
249     /// parameters are instantiated with fresh variables. This substitution does not include any
250     /// parameters declared on the method itself.
251     ///
252     /// Note that this substitution may include late-bound regions from the impl level. If so,
253     /// these are instantiated later in the `instantiate_method_sig` routine.
fresh_receiver_substs( &mut self, self_ty: Ty<'tcx>, pick: &probe::Pick<'tcx>, ) -> SubstsRef<'tcx>254     fn fresh_receiver_substs(
255         &mut self,
256         self_ty: Ty<'tcx>,
257         pick: &probe::Pick<'tcx>,
258     ) -> SubstsRef<'tcx> {
259         match pick.kind {
260             probe::InherentImplPick => {
261                 let impl_def_id = pick.item.container_id(self.tcx);
262                 assert!(
263                     self.tcx.impl_trait_ref(impl_def_id).is_none(),
264                     "impl {:?} is not an inherent impl",
265                     impl_def_id
266                 );
267                 self.fresh_substs_for_item(self.span, impl_def_id)
268             }
269 
270             probe::ObjectPick => {
271                 let trait_def_id = pick.item.container_id(self.tcx);
272                 self.extract_existential_trait_ref(self_ty, |this, object_ty, principal| {
273                     // The object data has no entry for the Self
274                     // Type. For the purposes of this method call, we
275                     // substitute the object type itself. This
276                     // wouldn't be a sound substitution in all cases,
277                     // since each instance of the object type is a
278                     // different existential and hence could match
279                     // distinct types (e.g., if `Self` appeared as an
280                     // argument type), but those cases have already
281                     // been ruled out when we deemed the trait to be
282                     // "object safe".
283                     let original_poly_trait_ref = principal.with_self_ty(this.tcx, object_ty);
284                     let upcast_poly_trait_ref = this.upcast(original_poly_trait_ref, trait_def_id);
285                     let upcast_trait_ref =
286                         this.instantiate_binder_with_fresh_vars(upcast_poly_trait_ref);
287                     debug!(
288                         "original_poly_trait_ref={:?} upcast_trait_ref={:?} target_trait={:?}",
289                         original_poly_trait_ref, upcast_trait_ref, trait_def_id
290                     );
291                     upcast_trait_ref.substs
292                 })
293             }
294 
295             probe::TraitPick => {
296                 let trait_def_id = pick.item.container_id(self.tcx);
297 
298                 // Make a trait reference `$0 : Trait<$1...$n>`
299                 // consisting entirely of type variables. Later on in
300                 // the process we will unify the transformed-self-type
301                 // of the method with the actual type in order to
302                 // unify some of these variables.
303                 self.fresh_substs_for_item(self.span, trait_def_id)
304             }
305 
306             probe::WhereClausePick(poly_trait_ref) => {
307                 // Where clauses can have bound regions in them. We need to instantiate
308                 // those to convert from a poly-trait-ref to a trait-ref.
309                 self.instantiate_binder_with_fresh_vars(poly_trait_ref).substs
310             }
311         }
312     }
313 
extract_existential_trait_ref<R, F>(&mut self, self_ty: Ty<'tcx>, mut closure: F) -> R where F: FnMut(&mut ConfirmContext<'a, 'tcx>, Ty<'tcx>, ty::PolyExistentialTraitRef<'tcx>) -> R,314     fn extract_existential_trait_ref<R, F>(&mut self, self_ty: Ty<'tcx>, mut closure: F) -> R
315     where
316         F: FnMut(&mut ConfirmContext<'a, 'tcx>, Ty<'tcx>, ty::PolyExistentialTraitRef<'tcx>) -> R,
317     {
318         // If we specified that this is an object method, then the
319         // self-type ought to be something that can be dereferenced to
320         // yield an object-type (e.g., `&Object` or `Box<Object>`
321         // etc).
322 
323         // FIXME: this feels, like, super dubious
324         self.fcx
325             .autoderef(self.span, self_ty)
326             .include_raw_pointers()
327             .find_map(|(ty, _)| match ty.kind() {
328                 ty::Dynamic(data, ..) => Some(closure(
329                     self,
330                     ty,
331                     data.principal().unwrap_or_else(|| {
332                         span_bug!(self.span, "calling trait method on empty object?")
333                     }),
334                 )),
335                 _ => None,
336             })
337             .unwrap_or_else(|| {
338                 span_bug!(
339                     self.span,
340                     "self-type `{}` for ObjectPick never dereferenced to an object",
341                     self_ty
342                 )
343             })
344     }
345 
instantiate_method_substs( &mut self, pick: &probe::Pick<'tcx>, seg: &hir::PathSegment<'_>, parent_substs: SubstsRef<'tcx>, ) -> SubstsRef<'tcx>346     fn instantiate_method_substs(
347         &mut self,
348         pick: &probe::Pick<'tcx>,
349         seg: &hir::PathSegment<'_>,
350         parent_substs: SubstsRef<'tcx>,
351     ) -> SubstsRef<'tcx> {
352         // Determine the values for the generic parameters of the method.
353         // If they were not explicitly supplied, just construct fresh
354         // variables.
355         let generics = self.tcx.generics_of(pick.item.def_id);
356 
357         let arg_count_correct = check_generic_arg_count_for_call(
358             self.tcx,
359             self.span,
360             pick.item.def_id,
361             generics,
362             seg,
363             IsMethodCall::Yes,
364         );
365 
366         // Create subst for early-bound lifetime parameters, combining
367         // parameters from the type and those from the method.
368         assert_eq!(generics.parent_count, parent_substs.len());
369 
370         struct MethodSubstsCtxt<'a, 'tcx> {
371             cfcx: &'a ConfirmContext<'a, 'tcx>,
372             pick: &'a probe::Pick<'tcx>,
373             seg: &'a hir::PathSegment<'a>,
374         }
375         impl<'a, 'tcx> CreateSubstsForGenericArgsCtxt<'a, 'tcx> for MethodSubstsCtxt<'a, 'tcx> {
376             fn args_for_def_id(
377                 &mut self,
378                 def_id: DefId,
379             ) -> (Option<&'a hir::GenericArgs<'a>>, bool) {
380                 if def_id == self.pick.item.def_id {
381                     if let Some(data) = self.seg.args {
382                         return (Some(data), false);
383                     }
384                 }
385                 (None, false)
386             }
387 
388             fn provided_kind(
389                 &mut self,
390                 param: &ty::GenericParamDef,
391                 arg: &GenericArg<'_>,
392             ) -> subst::GenericArg<'tcx> {
393                 match (&param.kind, arg) {
394                     (GenericParamDefKind::Lifetime, GenericArg::Lifetime(lt)) => {
395                         self.cfcx.fcx.astconv().ast_region_to_region(lt, Some(param)).into()
396                     }
397                     (GenericParamDefKind::Type { .. }, GenericArg::Type(ty)) => {
398                         self.cfcx.to_ty(ty).raw.into()
399                     }
400                     (GenericParamDefKind::Const { .. }, GenericArg::Const(ct)) => {
401                         self.cfcx.const_arg_to_const(&ct.value, param.def_id).into()
402                     }
403                     (GenericParamDefKind::Type { .. }, GenericArg::Infer(inf)) => {
404                         self.cfcx.ty_infer(Some(param), inf.span).into()
405                     }
406                     (GenericParamDefKind::Const { .. }, GenericArg::Infer(inf)) => {
407                         let tcx = self.cfcx.tcx();
408                         self.cfcx
409                             .ct_infer(
410                                 tcx.type_of(param.def_id)
411                                     .no_bound_vars()
412                                     .expect("const parameter types cannot be generic"),
413                                 Some(param),
414                                 inf.span,
415                             )
416                             .into()
417                     }
418                     _ => unreachable!(),
419                 }
420             }
421 
422             fn inferred_kind(
423                 &mut self,
424                 _substs: Option<&[subst::GenericArg<'tcx>]>,
425                 param: &ty::GenericParamDef,
426                 _infer_args: bool,
427             ) -> subst::GenericArg<'tcx> {
428                 self.cfcx.var_for_def(self.cfcx.span, param)
429             }
430         }
431 
432         let substs = create_substs_for_generic_args(
433             self.tcx,
434             pick.item.def_id,
435             parent_substs,
436             false,
437             None,
438             &arg_count_correct,
439             &mut MethodSubstsCtxt { cfcx: self, pick, seg },
440         );
441 
442         // When the method is confirmed, the `substs` includes
443         // parameters from not just the method, but also the impl of
444         // the method -- in particular, the `Self` type will be fully
445         // resolved. However, those are not something that the "user
446         // specified" -- i.e., those types come from the inferred type
447         // of the receiver, not something the user wrote. So when we
448         // create the user-substs, we want to replace those earlier
449         // types with just the types that the user actually wrote --
450         // that is, those that appear on the *method itself*.
451         //
452         // As an example, if the user wrote something like
453         // `foo.bar::<u32>(...)` -- the `Self` type here will be the
454         // type of `foo` (possibly adjusted), but we don't want to
455         // include that. We want just the `[_, u32]` part.
456         if !substs.is_empty() && !generics.params.is_empty() {
457             let user_type_annotation = self.probe(|_| {
458                 let user_substs = UserSubsts {
459                     substs: InternalSubsts::for_item(self.tcx, pick.item.def_id, |param, _| {
460                         let i = param.index as usize;
461                         if i < generics.parent_count {
462                             self.fcx.var_for_def(DUMMY_SP, param)
463                         } else {
464                             substs[i]
465                         }
466                     }),
467                     user_self_ty: None, // not relevant here
468                 };
469 
470                 self.fcx.canonicalize_user_type_annotation(UserType::TypeOf(
471                     pick.item.def_id,
472                     user_substs,
473                 ))
474             });
475 
476             debug!("instantiate_method_substs: user_type_annotation={:?}", user_type_annotation);
477 
478             if !self.skip_record_for_diagnostics {
479                 self.fcx.write_user_type_annotation(self.call_expr.hir_id, user_type_annotation);
480             }
481         }
482 
483         self.normalize(self.span, substs)
484     }
485 
unify_receivers( &mut self, self_ty: Ty<'tcx>, method_self_ty: Ty<'tcx>, pick: &probe::Pick<'tcx>, substs: SubstsRef<'tcx>, )486     fn unify_receivers(
487         &mut self,
488         self_ty: Ty<'tcx>,
489         method_self_ty: Ty<'tcx>,
490         pick: &probe::Pick<'tcx>,
491         substs: SubstsRef<'tcx>,
492     ) {
493         debug!(
494             "unify_receivers: self_ty={:?} method_self_ty={:?} span={:?} pick={:?}",
495             self_ty, method_self_ty, self.span, pick
496         );
497         let cause = self.cause(
498             self.self_expr.span,
499             ObligationCauseCode::UnifyReceiver(Box::new(UnifyReceiverContext {
500                 assoc_item: pick.item,
501                 param_env: self.param_env,
502                 substs,
503             })),
504         );
505         match self.at(&cause, self.param_env).sup(DefineOpaqueTypes::No, method_self_ty, self_ty) {
506             Ok(InferOk { obligations, value: () }) => {
507                 self.register_predicates(obligations);
508             }
509             Err(terr) => {
510                 // FIXME(arbitrary_self_types): We probably should limit the
511                 // situations where this can occur by adding additional restrictions
512                 // to the feature, like the self type can't reference method substs.
513                 if self.tcx.features().arbitrary_self_types {
514                     self.err_ctxt()
515                         .report_mismatched_types(&cause, method_self_ty, self_ty, terr)
516                         .emit();
517                 } else {
518                     span_bug!(
519                         self.span,
520                         "{} was a subtype of {} but now is not?",
521                         self_ty,
522                         method_self_ty
523                     );
524                 }
525             }
526         }
527     }
528 
529     // NOTE: this returns the *unnormalized* predicates and method sig. Because of
530     // inference guessing, the predicates and method signature can't be normalized
531     // until we unify the `Self` type.
instantiate_method_sig( &mut self, pick: &probe::Pick<'tcx>, all_substs: SubstsRef<'tcx>, ) -> (ty::FnSig<'tcx>, ty::InstantiatedPredicates<'tcx>)532     fn instantiate_method_sig(
533         &mut self,
534         pick: &probe::Pick<'tcx>,
535         all_substs: SubstsRef<'tcx>,
536     ) -> (ty::FnSig<'tcx>, ty::InstantiatedPredicates<'tcx>) {
537         debug!("instantiate_method_sig(pick={:?}, all_substs={:?})", pick, all_substs);
538 
539         // Instantiate the bounds on the method with the
540         // type/early-bound-regions substitutions performed. There can
541         // be no late-bound regions appearing here.
542         let def_id = pick.item.def_id;
543         let method_predicates = self.tcx.predicates_of(def_id).instantiate(self.tcx, all_substs);
544 
545         debug!("method_predicates after subst = {:?}", method_predicates);
546 
547         let sig = self.tcx.fn_sig(def_id).subst(self.tcx, all_substs);
548         debug!("type scheme substituted, sig={:?}", sig);
549 
550         let sig = self.instantiate_binder_with_fresh_vars(sig);
551         debug!("late-bound lifetimes from method instantiated, sig={:?}", sig);
552 
553         (sig, method_predicates)
554     }
555 
add_obligations( &mut self, fty: Ty<'tcx>, all_substs: SubstsRef<'tcx>, method_predicates: ty::InstantiatedPredicates<'tcx>, def_id: DefId, )556     fn add_obligations(
557         &mut self,
558         fty: Ty<'tcx>,
559         all_substs: SubstsRef<'tcx>,
560         method_predicates: ty::InstantiatedPredicates<'tcx>,
561         def_id: DefId,
562     ) {
563         debug!(
564             "add_obligations: fty={:?} all_substs={:?} method_predicates={:?} def_id={:?}",
565             fty, all_substs, method_predicates, def_id
566         );
567 
568         // FIXME: could replace with the following, but we already calculated `method_predicates`,
569         // so we just call `predicates_for_generics` directly to avoid redoing work.
570         // `self.add_required_obligations(self.span, def_id, &all_substs);`
571         for obligation in traits::predicates_for_generics(
572             |idx, span| {
573                 let code = if span.is_dummy() {
574                     ObligationCauseCode::ExprItemObligation(def_id, self.call_expr.hir_id, idx)
575                 } else {
576                     ObligationCauseCode::ExprBindingObligation(
577                         def_id,
578                         span,
579                         self.call_expr.hir_id,
580                         idx,
581                     )
582                 };
583                 traits::ObligationCause::new(self.span, self.body_id, code)
584             },
585             self.param_env,
586             method_predicates,
587         ) {
588             self.register_predicate(obligation);
589         }
590 
591         // this is a projection from a trait reference, so we have to
592         // make sure that the trait reference inputs are well-formed.
593         self.add_wf_bounds(all_substs, self.call_expr);
594 
595         // the function type must also be well-formed (this is not
596         // implied by the substs being well-formed because of inherent
597         // impls and late-bound regions - see issue #28609).
598         self.register_wf_obligation(fty.into(), self.span, traits::WellFormed(None));
599     }
600 
601     ///////////////////////////////////////////////////////////////////////////
602     // MISCELLANY
603 
predicates_require_illegal_sized_bound( &self, predicates: ty::InstantiatedPredicates<'tcx>, ) -> Option<Span>604     fn predicates_require_illegal_sized_bound(
605         &self,
606         predicates: ty::InstantiatedPredicates<'tcx>,
607     ) -> Option<Span> {
608         let sized_def_id = self.tcx.lang_items().sized_trait()?;
609 
610         traits::elaborate(self.tcx, predicates.predicates.iter().copied())
611             // We don't care about regions here.
612             .filter_map(|pred| match pred.kind().skip_binder() {
613                 ty::ClauseKind::Trait(trait_pred) if trait_pred.def_id() == sized_def_id => {
614                     let span = predicates
615                         .iter()
616                         .find_map(|(p, span)| if p == pred { Some(span) } else { None })
617                         .unwrap_or(rustc_span::DUMMY_SP);
618                     Some((trait_pred, span))
619                 }
620                 _ => None,
621             })
622             .find_map(|(trait_pred, span)| match trait_pred.self_ty().kind() {
623                 ty::Dynamic(..) => Some(span),
624                 _ => None,
625             })
626     }
627 
enforce_illegal_method_limitations(&self, pick: &probe::Pick<'_>)628     fn enforce_illegal_method_limitations(&self, pick: &probe::Pick<'_>) {
629         // Disallow calls to the method `drop` defined in the `Drop` trait.
630         if let Some(trait_def_id) = pick.item.trait_container(self.tcx) {
631             callee::check_legal_trait_for_method_call(
632                 self.tcx,
633                 self.span,
634                 Some(self.self_expr.span),
635                 self.call_expr.span,
636                 trait_def_id,
637             )
638         }
639     }
640 
upcast( &mut self, source_trait_ref: ty::PolyTraitRef<'tcx>, target_trait_def_id: DefId, ) -> ty::PolyTraitRef<'tcx>641     fn upcast(
642         &mut self,
643         source_trait_ref: ty::PolyTraitRef<'tcx>,
644         target_trait_def_id: DefId,
645     ) -> ty::PolyTraitRef<'tcx> {
646         let upcast_trait_refs =
647             traits::upcast_choices(self.tcx, source_trait_ref, target_trait_def_id);
648 
649         // must be exactly one trait ref or we'd get an ambig error etc
650         if upcast_trait_refs.len() != 1 {
651             span_bug!(
652                 self.span,
653                 "cannot uniquely upcast `{:?}` to `{:?}`: `{:?}`",
654                 source_trait_ref,
655                 target_trait_def_id,
656                 upcast_trait_refs
657             );
658         }
659 
660         upcast_trait_refs.into_iter().next().unwrap()
661     }
662 
instantiate_binder_with_fresh_vars<T>(&self, value: ty::Binder<'tcx, T>) -> T where T: TypeFoldable<TyCtxt<'tcx>> + Copy,663     fn instantiate_binder_with_fresh_vars<T>(&self, value: ty::Binder<'tcx, T>) -> T
664     where
665         T: TypeFoldable<TyCtxt<'tcx>> + Copy,
666     {
667         self.fcx.instantiate_binder_with_fresh_vars(self.span, infer::FnCall, value)
668     }
669 }
670