]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_typeck/src/check/method/confirm.rs
Auto merge of #82127 - tgnottingham:tune-ahead-of-time-codegen, r=varkor
[rust.git] / compiler / rustc_typeck / src / check / method / confirm.rs
1 use super::{probe, MethodCallee};
2
3 use crate::astconv::{AstConv, CreateSubstsForGenericArgsCtxt, IsMethodCall};
4 use crate::check::{callee, FnCtxt};
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::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, Subst, SubstsRef};
14 use rustc_middle::ty::{self, GenericParamDefKind, Ty};
15 use rustc_span::Span;
16 use rustc_trait_selection::traits;
17
18 use std::ops::Deref;
19
20 struct ConfirmContext<'a, 'tcx> {
21     fcx: &'a FnCtxt<'a, 'tcx>,
22     span: Span,
23     self_expr: &'tcx hir::Expr<'tcx>,
24     call_expr: &'tcx hir::Expr<'tcx>,
25 }
26
27 impl<'a, 'tcx> Deref for ConfirmContext<'a, 'tcx> {
28     type Target = FnCtxt<'a, 'tcx>;
29     fn deref(&self) -> &Self::Target {
30         &self.fcx
31     }
32 }
33
34 #[derive(Debug)]
35 pub struct ConfirmResult<'tcx> {
36     pub callee: MethodCallee<'tcx>,
37     pub illegal_sized_bound: Option<Span>,
38 }
39
40 impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
41     pub fn confirm_method(
42         &self,
43         span: Span,
44         self_expr: &'tcx hir::Expr<'tcx>,
45         call_expr: &'tcx hir::Expr<'tcx>,
46         unadjusted_self_ty: Ty<'tcx>,
47         pick: probe::Pick<'tcx>,
48         segment: &hir::PathSegment<'_>,
49     ) -> ConfirmResult<'tcx> {
50         debug!(
51             "confirm(unadjusted_self_ty={:?}, pick={:?}, generic_args={:?})",
52             unadjusted_self_ty, pick, segment.args,
53         );
54
55         let mut confirm_cx = ConfirmContext::new(self, span, self_expr, call_expr);
56         confirm_cx.confirm(unadjusted_self_ty, pick, segment)
57     }
58 }
59
60 impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
61     fn new(
62         fcx: &'a FnCtxt<'a, 'tcx>,
63         span: Span,
64         self_expr: &'tcx hir::Expr<'tcx>,
65         call_expr: &'tcx hir::Expr<'tcx>,
66     ) -> ConfirmContext<'a, 'tcx> {
67         ConfirmContext { fcx, span, self_expr, call_expr }
68     }
69
70     fn confirm(
71         &mut self,
72         unadjusted_self_ty: Ty<'tcx>,
73         pick: probe::Pick<'tcx>,
74         segment: &hir::PathSegment<'_>,
75     ) -> ConfirmResult<'tcx> {
76         // Adjust the self expression the user provided and obtain the adjusted type.
77         let self_ty = self.adjust_self_ty(unadjusted_self_ty, &pick);
78
79         // Create substitutions for the method's type parameters.
80         let rcvr_substs = self.fresh_receiver_substs(self_ty, &pick);
81         let all_substs = self.instantiate_method_substs(&pick, segment, rcvr_substs);
82
83         debug!("all_substs={:?}", all_substs);
84
85         // Create the final signature for the method, replacing late-bound regions.
86         let (method_sig, method_predicates) = self.instantiate_method_sig(&pick, all_substs);
87
88         // Unify the (adjusted) self type with what the method expects.
89         //
90         // SUBTLE: if we want good error messages, because of "guessing" while matching
91         // traits, no trait system method can be called before this point because they
92         // could alter our Self-type, except for normalizing the receiver from the
93         // signature (which is also done during probing).
94         let method_sig_rcvr = self.normalize_associated_types_in(self.span, method_sig.inputs()[0]);
95         debug!(
96             "confirm: self_ty={:?} method_sig_rcvr={:?} method_sig={:?} method_predicates={:?}",
97             self_ty, method_sig_rcvr, method_sig, method_predicates
98         );
99         self.unify_receivers(self_ty, method_sig_rcvr, &pick, all_substs);
100
101         let (method_sig, method_predicates) =
102             self.normalize_associated_types_in(self.span, (method_sig, method_predicates));
103
104         // Make sure nobody calls `drop()` explicitly.
105         self.enforce_illegal_method_limitations(&pick);
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 `src/test/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 illegal_sized_bound = self.predicates_require_illegal_sized_bound(&method_predicates);
116
117         // Add any trait/regions obligations specified on the method's type parameters.
118         // We won't add these if we encountered an illegal sized bound, so that we can use
119         // a custom error in that case.
120         if illegal_sized_bound.is_none() {
121             let method_ty = self.tcx.mk_fn_ptr(ty::Binder::bind(method_sig));
122             self.add_obligations(method_ty, all_substs, method_predicates);
123         }
124
125         // Create the final `MethodCallee`.
126         let callee = MethodCallee { def_id: pick.item.def_id, substs: all_substs, sig: method_sig };
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 typeck results.
140         let mut autoderef =
141             self.autoderef_overloaded_span(self.span, unadjusted_self_ty, self.call_expr.span);
142         let (_, n) = match autoderef.nth(pick.autoderefs) {
143             Some(n) => n,
144             None => {
145                 return self.tcx.ty_error_with_message(
146                     rustc_span::DUMMY_SP,
147                     &format!("failed autoderef {}", pick.autoderefs),
148                 );
149             }
150         };
151         assert_eq!(n, pick.autoderefs);
152
153         let mut adjustments = self.adjust_steps(&autoderef);
154
155         let mut target =
156             self.structurally_resolved_type(autoderef.span(), autoderef.final_ty(false));
157
158         if let Some(mutbl) = pick.autoref {
159             let region = self.next_region_var(infer::Autoref(self.span, pick.item));
160             target = self.tcx.mk_ref(region, ty::TypeAndMut { mutbl, ty: target });
161             let mutbl = match mutbl {
162                 hir::Mutability::Not => AutoBorrowMutability::Not,
163                 hir::Mutability::Mut => AutoBorrowMutability::Mut {
164                     // Method call receivers are the primary use case
165                     // for two-phase borrows.
166                     allow_two_phase_borrow: AllowTwoPhase::Yes,
167                 },
168             };
169             adjustments
170                 .push(Adjustment { kind: Adjust::Borrow(AutoBorrow::Ref(region, mutbl)), target });
171
172             if let Some(unsize_target) = pick.unsize {
173                 target = self
174                     .tcx
175                     .mk_ref(region, ty::TypeAndMut { mutbl: mutbl.into(), ty: unsize_target });
176                 adjustments.push(Adjustment { kind: Adjust::Pointer(PointerCast::Unsize), target });
177             }
178         } else {
179             // No unsizing should be performed without autoref (at
180             // least during method dispach). This is because we
181             // currently only unsize `[T;N]` to `[T]`, and naturally
182             // that must occur being a reference.
183             assert!(pick.unsize.is_none());
184         }
185
186         self.register_predicates(autoderef.into_obligations());
187
188         // Write out the final adjustments.
189         self.apply_adjustments(self.self_expr, adjustments);
190
191         target
192     }
193
194     /// Returns a set of substitutions for the method *receiver* where all type and region
195     /// parameters are instantiated with fresh variables. This substitution does not include any
196     /// parameters declared on the method itself.
197     ///
198     /// Note that this substitution may include late-bound regions from the impl level. If so,
199     /// these are instantiated later in the `instantiate_method_sig` routine.
200     fn fresh_receiver_substs(
201         &mut self,
202         self_ty: Ty<'tcx>,
203         pick: &probe::Pick<'tcx>,
204     ) -> SubstsRef<'tcx> {
205         match pick.kind {
206             probe::InherentImplPick => {
207                 let impl_def_id = pick.item.container.id();
208                 assert!(
209                     self.tcx.impl_trait_ref(impl_def_id).is_none(),
210                     "impl {:?} is not an inherent impl",
211                     impl_def_id
212                 );
213                 self.fresh_substs_for_item(self.span, impl_def_id)
214             }
215
216             probe::ObjectPick => {
217                 let trait_def_id = pick.item.container.id();
218                 self.extract_existential_trait_ref(self_ty, |this, object_ty, principal| {
219                     // The object data has no entry for the Self
220                     // Type. For the purposes of this method call, we
221                     // substitute the object type itself. This
222                     // wouldn't be a sound substitution in all cases,
223                     // since each instance of the object type is a
224                     // different existential and hence could match
225                     // distinct types (e.g., if `Self` appeared as an
226                     // argument type), but those cases have already
227                     // been ruled out when we deemed the trait to be
228                     // "object safe".
229                     let original_poly_trait_ref = principal.with_self_ty(this.tcx, object_ty);
230                     let upcast_poly_trait_ref = this.upcast(original_poly_trait_ref, trait_def_id);
231                     let upcast_trait_ref =
232                         this.replace_bound_vars_with_fresh_vars(upcast_poly_trait_ref);
233                     debug!(
234                         "original_poly_trait_ref={:?} upcast_trait_ref={:?} target_trait={:?}",
235                         original_poly_trait_ref, upcast_trait_ref, trait_def_id
236                     );
237                     upcast_trait_ref.substs
238                 })
239             }
240
241             probe::TraitPick => {
242                 let trait_def_id = pick.item.container.id();
243
244                 // Make a trait reference `$0 : Trait<$1...$n>`
245                 // consisting entirely of type variables. Later on in
246                 // the process we will unify the transformed-self-type
247                 // of the method with the actual type in order to
248                 // unify some of these variables.
249                 self.fresh_substs_for_item(self.span, trait_def_id)
250             }
251
252             probe::WhereClausePick(poly_trait_ref) => {
253                 // Where clauses can have bound regions in them. We need to instantiate
254                 // those to convert from a poly-trait-ref to a trait-ref.
255                 self.replace_bound_vars_with_fresh_vars(poly_trait_ref).substs
256             }
257         }
258     }
259
260     fn extract_existential_trait_ref<R, F>(&mut self, self_ty: Ty<'tcx>, mut closure: F) -> R
261     where
262         F: FnMut(&mut ConfirmContext<'a, 'tcx>, Ty<'tcx>, ty::PolyExistentialTraitRef<'tcx>) -> R,
263     {
264         // If we specified that this is an object method, then the
265         // self-type ought to be something that can be dereferenced to
266         // yield an object-type (e.g., `&Object` or `Box<Object>`
267         // etc).
268
269         // FIXME: this feels, like, super dubious
270         self.fcx
271             .autoderef(self.span, self_ty)
272             .include_raw_pointers()
273             .find_map(|(ty, _)| match ty.kind() {
274                 ty::Dynamic(ref data, ..) => Some(closure(
275                     self,
276                     ty,
277                     data.principal().unwrap_or_else(|| {
278                         span_bug!(self.span, "calling trait method on empty object?")
279                     }),
280                 )),
281                 _ => None,
282             })
283             .unwrap_or_else(|| {
284                 span_bug!(
285                     self.span,
286                     "self-type `{}` for ObjectPick never dereferenced to an object",
287                     self_ty
288                 )
289             })
290     }
291
292     fn instantiate_method_substs(
293         &mut self,
294         pick: &probe::Pick<'tcx>,
295         seg: &hir::PathSegment<'_>,
296         parent_substs: SubstsRef<'tcx>,
297     ) -> SubstsRef<'tcx> {
298         // Determine the values for the generic parameters of the method.
299         // If they were not explicitly supplied, just construct fresh
300         // variables.
301         let generics = self.tcx.generics_of(pick.item.def_id);
302
303         let arg_count_correct = AstConv::check_generic_arg_count_for_call(
304             self.tcx,
305             self.span,
306             pick.item.def_id,
307             &generics,
308             seg,
309             IsMethodCall::Yes,
310         );
311
312         // Create subst for early-bound lifetime parameters, combining
313         // parameters from the type and those from the method.
314         assert_eq!(generics.parent_count, parent_substs.len());
315
316         struct MethodSubstsCtxt<'a, 'tcx> {
317             cfcx: &'a ConfirmContext<'a, 'tcx>,
318             pick: &'a probe::Pick<'tcx>,
319             seg: &'a hir::PathSegment<'a>,
320         }
321         impl<'a, 'tcx> CreateSubstsForGenericArgsCtxt<'a, 'tcx> for MethodSubstsCtxt<'a, 'tcx> {
322             fn args_for_def_id(
323                 &mut self,
324                 def_id: DefId,
325             ) -> (Option<&'a hir::GenericArgs<'a>>, bool) {
326                 if def_id == self.pick.item.def_id {
327                     if let Some(ref data) = self.seg.args {
328                         return (Some(data), false);
329                     }
330                 }
331                 (None, false)
332             }
333
334             fn provided_kind(
335                 &mut self,
336                 param: &ty::GenericParamDef,
337                 arg: &GenericArg<'_>,
338             ) -> subst::GenericArg<'tcx> {
339                 match (&param.kind, arg) {
340                     (GenericParamDefKind::Lifetime, GenericArg::Lifetime(lt)) => {
341                         AstConv::ast_region_to_region(self.cfcx.fcx, lt, Some(param)).into()
342                     }
343                     (GenericParamDefKind::Type { .. }, GenericArg::Type(ty)) => {
344                         self.cfcx.to_ty(ty).into()
345                     }
346                     (GenericParamDefKind::Const, GenericArg::Const(ct)) => {
347                         self.cfcx.const_arg_to_const(&ct.value, param.def_id).into()
348                     }
349                     _ => unreachable!(),
350                 }
351             }
352
353             fn inferred_kind(
354                 &mut self,
355                 _substs: Option<&[subst::GenericArg<'tcx>]>,
356                 param: &ty::GenericParamDef,
357                 _infer_args: bool,
358             ) -> subst::GenericArg<'tcx> {
359                 self.cfcx.var_for_def(self.cfcx.span, param)
360             }
361         }
362         AstConv::create_substs_for_generic_args(
363             self.tcx,
364             pick.item.def_id,
365             parent_substs,
366             false,
367             None,
368             arg_count_correct,
369             &mut MethodSubstsCtxt { cfcx: self, pick, seg },
370         )
371     }
372
373     fn unify_receivers(
374         &mut self,
375         self_ty: Ty<'tcx>,
376         method_self_ty: Ty<'tcx>,
377         pick: &probe::Pick<'tcx>,
378         substs: SubstsRef<'tcx>,
379     ) {
380         debug!(
381             "unify_receivers: self_ty={:?} method_self_ty={:?} span={:?} pick={:?}",
382             self_ty, method_self_ty, self.span, pick
383         );
384         let cause = self.cause(
385             self.span,
386             ObligationCauseCode::UnifyReceiver(Box::new(UnifyReceiverContext {
387                 assoc_item: pick.item,
388                 param_env: self.param_env,
389                 substs,
390             })),
391         );
392         match self.at(&cause, self.param_env).sup(method_self_ty, self_ty) {
393             Ok(InferOk { obligations, value: () }) => {
394                 self.register_predicates(obligations);
395             }
396             Err(_) => {
397                 span_bug!(
398                     self.span,
399                     "{} was a subtype of {} but now is not?",
400                     self_ty,
401                     method_self_ty
402                 );
403             }
404         }
405     }
406
407     // NOTE: this returns the *unnormalized* predicates and method sig. Because of
408     // inference guessing, the predicates and method signature can't be normalized
409     // until we unify the `Self` type.
410     fn instantiate_method_sig(
411         &mut self,
412         pick: &probe::Pick<'tcx>,
413         all_substs: SubstsRef<'tcx>,
414     ) -> (ty::FnSig<'tcx>, ty::InstantiatedPredicates<'tcx>) {
415         debug!("instantiate_method_sig(pick={:?}, all_substs={:?})", pick, all_substs);
416
417         // Instantiate the bounds on the method with the
418         // type/early-bound-regions substitutions performed. There can
419         // be no late-bound regions appearing here.
420         let def_id = pick.item.def_id;
421         let method_predicates = self.tcx.predicates_of(def_id).instantiate(self.tcx, all_substs);
422
423         debug!("method_predicates after subst = {:?}", method_predicates);
424
425         let sig = self.tcx.fn_sig(def_id);
426
427         // Instantiate late-bound regions and substitute the trait
428         // parameters into the method type to get the actual method type.
429         //
430         // N.B., instantiate late-bound regions first so that
431         // `instantiate_type_scheme` can normalize associated types that
432         // may reference those regions.
433         let method_sig = self.replace_bound_vars_with_fresh_vars(sig);
434         debug!("late-bound lifetimes from method instantiated, method_sig={:?}", method_sig);
435
436         let method_sig = method_sig.subst(self.tcx, all_substs);
437         debug!("type scheme substituted, method_sig={:?}", method_sig);
438
439         (method_sig, method_predicates)
440     }
441
442     fn add_obligations(
443         &mut self,
444         fty: Ty<'tcx>,
445         all_substs: SubstsRef<'tcx>,
446         method_predicates: ty::InstantiatedPredicates<'tcx>,
447     ) {
448         debug!(
449             "add_obligations: fty={:?} all_substs={:?} method_predicates={:?}",
450             fty, all_substs, method_predicates
451         );
452
453         self.add_obligations_for_parameters(
454             traits::ObligationCause::misc(self.span, self.body_id),
455             method_predicates,
456         );
457
458         // this is a projection from a trait reference, so we have to
459         // make sure that the trait reference inputs are well-formed.
460         self.add_wf_bounds(all_substs, self.call_expr);
461
462         // the function type must also be well-formed (this is not
463         // implied by the substs being well-formed because of inherent
464         // impls and late-bound regions - see issue #28609).
465         self.register_wf_obligation(fty.into(), self.span, traits::MiscObligation);
466     }
467
468     ///////////////////////////////////////////////////////////////////////////
469     // MISCELLANY
470
471     fn predicates_require_illegal_sized_bound(
472         &self,
473         predicates: &ty::InstantiatedPredicates<'tcx>,
474     ) -> Option<Span> {
475         let sized_def_id = match self.tcx.lang_items().sized_trait() {
476             Some(def_id) => def_id,
477             None => return None,
478         };
479
480         traits::elaborate_predicates(self.tcx, predicates.predicates.iter().copied())
481             // We don't care about regions here.
482             .filter_map(|obligation| match obligation.predicate.kind().skip_binder() {
483                 ty::PredicateKind::Trait(trait_pred, _) if trait_pred.def_id() == sized_def_id => {
484                     let span = predicates
485                         .predicates
486                         .iter()
487                         .zip(predicates.spans.iter())
488                         .find_map(
489                             |(p, span)| {
490                                 if *p == obligation.predicate { Some(*span) } else { None }
491                             },
492                         )
493                         .unwrap_or(rustc_span::DUMMY_SP);
494                     Some((trait_pred, span))
495                 }
496                 _ => None,
497             })
498             .find_map(|(trait_pred, span)| match trait_pred.self_ty().kind() {
499                 ty::Dynamic(..) => Some(span),
500                 _ => None,
501             })
502     }
503
504     fn enforce_illegal_method_limitations(&self, pick: &probe::Pick<'_>) {
505         // Disallow calls to the method `drop` defined in the `Drop` trait.
506         match pick.item.container {
507             ty::TraitContainer(trait_def_id) => callee::check_legal_trait_for_method_call(
508                 self.tcx,
509                 self.span,
510                 Some(self.self_expr.span),
511                 self.call_expr.span,
512                 trait_def_id,
513             ),
514             ty::ImplContainer(..) => {}
515         }
516     }
517
518     fn upcast(
519         &mut self,
520         source_trait_ref: ty::PolyTraitRef<'tcx>,
521         target_trait_def_id: DefId,
522     ) -> ty::PolyTraitRef<'tcx> {
523         let upcast_trait_refs =
524             traits::upcast_choices(self.tcx, source_trait_ref, target_trait_def_id);
525
526         // must be exactly one trait ref or we'd get an ambig error etc
527         if upcast_trait_refs.len() != 1 {
528             span_bug!(
529                 self.span,
530                 "cannot uniquely upcast `{:?}` to `{:?}`: `{:?}`",
531                 source_trait_ref,
532                 target_trait_def_id,
533                 upcast_trait_refs
534             );
535         }
536
537         upcast_trait_refs.into_iter().next().unwrap()
538     }
539
540     fn replace_bound_vars_with_fresh_vars<T>(&self, value: ty::Binder<T>) -> T
541     where
542         T: TypeFoldable<'tcx>,
543     {
544         self.fcx.replace_bound_vars_with_fresh_vars(self.span, infer::FnCall, value).0
545     }
546 }