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