]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/check/method/confirm.rs
Rollup merge of #73762 - poliorcetics:trait-keyword, r=KodrAus
[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};
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, 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         ConfirmResult { callee, illegal_sized_bound }
123     }
124
125     ///////////////////////////////////////////////////////////////////////////
126     // ADJUSTMENTS
127
128     fn adjust_self_ty(
129         &mut self,
130         unadjusted_self_ty: Ty<'tcx>,
131         pick: &probe::Pick<'tcx>,
132     ) -> Ty<'tcx> {
133         // Commit the autoderefs by calling `autoderef` again, but this
134         // time writing the results into the various typeck results.
135         let mut autoderef = self.autoderef(self.span, unadjusted_self_ty);
136         let (_, n) = match autoderef.nth(pick.autoderefs) {
137             Some(n) => n,
138             None => {
139                 return self.tcx.ty_error_with_message(
140                     rustc_span::DUMMY_SP,
141                     &format!("failed autoderef {}", pick.autoderefs),
142                 );
143             }
144         };
145         assert_eq!(n, pick.autoderefs);
146
147         let mut adjustments = self.adjust_steps(&autoderef);
148
149         let mut target =
150             self.structurally_resolved_type(autoderef.span(), autoderef.final_ty(false));
151
152         if let Some(mutbl) = pick.autoref {
153             let region = self.next_region_var(infer::Autoref(self.span));
154             target = self.tcx.mk_ref(region, ty::TypeAndMut { mutbl, ty: target });
155             let mutbl = match mutbl {
156                 hir::Mutability::Not => AutoBorrowMutability::Not,
157                 hir::Mutability::Mut => AutoBorrowMutability::Mut {
158                     // Method call receivers are the primary use case
159                     // for two-phase borrows.
160                     allow_two_phase_borrow: AllowTwoPhase::Yes,
161                 },
162             };
163             adjustments
164                 .push(Adjustment { kind: Adjust::Borrow(AutoBorrow::Ref(region, mutbl)), target });
165
166             if let Some(unsize_target) = pick.unsize {
167                 target = self
168                     .tcx
169                     .mk_ref(region, ty::TypeAndMut { mutbl: mutbl.into(), ty: unsize_target });
170                 adjustments.push(Adjustment { kind: Adjust::Pointer(PointerCast::Unsize), target });
171             }
172         } else {
173             // No unsizing should be performed without autoref (at
174             // least during method dispach). This is because we
175             // currently only unsize `[T;N]` to `[T]`, and naturally
176             // that must occur being a reference.
177             assert!(pick.unsize.is_none());
178         }
179
180         self.register_predicates(autoderef.into_obligations());
181
182         // Write out the final adjustments.
183         self.apply_adjustments(self.self_expr, adjustments);
184
185         target
186     }
187
188     /// Returns a set of substitutions for the method *receiver* where all type and region
189     /// parameters are instantiated with fresh variables. This substitution does not include any
190     /// parameters declared on the method itself.
191     ///
192     /// Note that this substitution may include late-bound regions from the impl level. If so,
193     /// these are instantiated later in the `instantiate_method_sig` routine.
194     fn fresh_receiver_substs(
195         &mut self,
196         self_ty: Ty<'tcx>,
197         pick: &probe::Pick<'tcx>,
198     ) -> SubstsRef<'tcx> {
199         match pick.kind {
200             probe::InherentImplPick => {
201                 let impl_def_id = pick.item.container.id();
202                 assert!(
203                     self.tcx.impl_trait_ref(impl_def_id).is_none(),
204                     "impl {:?} is not an inherent impl",
205                     impl_def_id
206                 );
207                 self.fresh_substs_for_item(self.span, impl_def_id)
208             }
209
210             probe::ObjectPick => {
211                 let trait_def_id = pick.item.container.id();
212                 self.extract_existential_trait_ref(self_ty, |this, object_ty, principal| {
213                     // The object data has no entry for the Self
214                     // Type. For the purposes of this method call, we
215                     // substitute the object type itself. This
216                     // wouldn't be a sound substitution in all cases,
217                     // since each instance of the object type is a
218                     // different existential and hence could match
219                     // distinct types (e.g., if `Self` appeared as an
220                     // argument type), but those cases have already
221                     // been ruled out when we deemed the trait to be
222                     // "object safe".
223                     let original_poly_trait_ref = principal.with_self_ty(this.tcx, object_ty);
224                     let upcast_poly_trait_ref = this.upcast(original_poly_trait_ref, trait_def_id);
225                     let upcast_trait_ref =
226                         this.replace_bound_vars_with_fresh_vars(&upcast_poly_trait_ref);
227                     debug!(
228                         "original_poly_trait_ref={:?} upcast_trait_ref={:?} target_trait={:?}",
229                         original_poly_trait_ref, upcast_trait_ref, trait_def_id
230                     );
231                     upcast_trait_ref.substs
232                 })
233             }
234
235             probe::TraitPick => {
236                 let trait_def_id = pick.item.container.id();
237
238                 // Make a trait reference `$0 : Trait<$1...$n>`
239                 // consisting entirely of type variables. Later on in
240                 // the process we will unify the transformed-self-type
241                 // of the method with the actual type in order to
242                 // unify some of these variables.
243                 self.fresh_substs_for_item(self.span, trait_def_id)
244             }
245
246             probe::WhereClausePick(ref poly_trait_ref) => {
247                 // Where clauses can have bound regions in them. We need to instantiate
248                 // those to convert from a poly-trait-ref to a trait-ref.
249                 self.replace_bound_vars_with_fresh_vars(&poly_trait_ref).substs
250             }
251         }
252     }
253
254     fn extract_existential_trait_ref<R, F>(&mut self, self_ty: Ty<'tcx>, mut closure: F) -> R
255     where
256         F: FnMut(&mut ConfirmContext<'a, 'tcx>, Ty<'tcx>, ty::PolyExistentialTraitRef<'tcx>) -> R,
257     {
258         // If we specified that this is an object method, then the
259         // self-type ought to be something that can be dereferenced to
260         // yield an object-type (e.g., `&Object` or `Box<Object>`
261         // etc).
262
263         // FIXME: this feels, like, super dubious
264         self.fcx
265             .autoderef(self.span, self_ty)
266             .include_raw_pointers()
267             .find_map(|(ty, _)| match ty.kind {
268                 ty::Dynamic(ref data, ..) => Some(closure(
269                     self,
270                     ty,
271                     data.principal().unwrap_or_else(|| {
272                         span_bug!(self.span, "calling trait method on empty object?")
273                     }),
274                 )),
275                 _ => None,
276             })
277             .unwrap_or_else(|| {
278                 span_bug!(
279                     self.span,
280                     "self-type `{}` for ObjectPick never dereferenced to an object",
281                     self_ty
282                 )
283             })
284     }
285
286     fn instantiate_method_substs(
287         &mut self,
288         pick: &probe::Pick<'tcx>,
289         seg: &hir::PathSegment<'_>,
290         parent_substs: SubstsRef<'tcx>,
291     ) -> SubstsRef<'tcx> {
292         // Determine the values for the generic parameters of the method.
293         // If they were not explicitly supplied, just construct fresh
294         // variables.
295         let generics = self.tcx.generics_of(pick.item.def_id);
296         let arg_count_correct = AstConv::check_generic_arg_count_for_call(
297             self.tcx, self.span, &generics, &seg, true, // `is_method_call`
298         );
299
300         // Create subst for early-bound lifetime parameters, combining
301         // parameters from the type and those from the method.
302         assert_eq!(generics.parent_count, parent_substs.len());
303
304         AstConv::create_substs_for_generic_args(
305             self.tcx,
306             pick.item.def_id,
307             parent_substs,
308             false,
309             None,
310             arg_count_correct,
311             // Provide the generic args, and whether types should be inferred.
312             |def_id| {
313                 // The last component of the returned tuple here is unimportant.
314                 if def_id == pick.item.def_id {
315                     if let Some(ref data) = seg.args {
316                         return (Some(data), false);
317                     }
318                 }
319                 (None, false)
320             },
321             // Provide substitutions for parameters for which (valid) arguments have been provided.
322             |param, arg| match (&param.kind, arg) {
323                 (GenericParamDefKind::Lifetime, GenericArg::Lifetime(lt)) => {
324                     AstConv::ast_region_to_region(self.fcx, lt, Some(param)).into()
325                 }
326                 (GenericParamDefKind::Type { .. }, GenericArg::Type(ty)) => self.to_ty(ty).into(),
327                 (GenericParamDefKind::Const, GenericArg::Const(ct)) => {
328                     self.const_arg_to_const(&ct.value, param.def_id).into()
329                 }
330                 _ => unreachable!(),
331             },
332             // Provide substitutions for parameters for which arguments are inferred.
333             |_, param, _| self.var_for_def(self.span, param),
334         )
335     }
336
337     fn unify_receivers(&mut self, self_ty: Ty<'tcx>, method_self_ty: Ty<'tcx>) {
338         match self.at(&self.misc(self.span), self.param_env).sup(method_self_ty, self_ty) {
339             Ok(InferOk { obligations, value: () }) => {
340                 self.register_predicates(obligations);
341             }
342             Err(_) => {
343                 span_bug!(
344                     self.span,
345                     "{} was a subtype of {} but now is not?",
346                     self_ty,
347                     method_self_ty
348                 );
349             }
350         }
351     }
352
353     // NOTE: this returns the *unnormalized* predicates and method sig. Because of
354     // inference guessing, the predicates and method signature can't be normalized
355     // until we unify the `Self` type.
356     fn instantiate_method_sig(
357         &mut self,
358         pick: &probe::Pick<'tcx>,
359         all_substs: SubstsRef<'tcx>,
360     ) -> (ty::FnSig<'tcx>, ty::InstantiatedPredicates<'tcx>) {
361         debug!("instantiate_method_sig(pick={:?}, all_substs={:?})", pick, all_substs);
362
363         // Instantiate the bounds on the method with the
364         // type/early-bound-regions substitutions performed. There can
365         // be no late-bound regions appearing here.
366         let def_id = pick.item.def_id;
367         let method_predicates = self.tcx.predicates_of(def_id).instantiate(self.tcx, all_substs);
368
369         debug!("method_predicates after subst = {:?}", method_predicates);
370
371         let sig = self.tcx.fn_sig(def_id);
372
373         // Instantiate late-bound regions and substitute the trait
374         // parameters into the method type to get the actual method type.
375         //
376         // N.B., instantiate late-bound regions first so that
377         // `instantiate_type_scheme` can normalize associated types that
378         // may reference those regions.
379         let method_sig = self.replace_bound_vars_with_fresh_vars(&sig);
380         debug!("late-bound lifetimes from method instantiated, method_sig={:?}", method_sig);
381
382         let method_sig = method_sig.subst(self.tcx, all_substs);
383         debug!("type scheme substituted, method_sig={:?}", method_sig);
384
385         (method_sig, method_predicates)
386     }
387
388     fn add_obligations(
389         &mut self,
390         fty: Ty<'tcx>,
391         all_substs: SubstsRef<'tcx>,
392         method_predicates: ty::InstantiatedPredicates<'tcx>,
393     ) {
394         debug!(
395             "add_obligations: fty={:?} all_substs={:?} method_predicates={:?}",
396             fty, all_substs, method_predicates
397         );
398
399         self.add_obligations_for_parameters(
400             traits::ObligationCause::misc(self.span, self.body_id),
401             method_predicates,
402         );
403
404         // this is a projection from a trait reference, so we have to
405         // make sure that the trait reference inputs are well-formed.
406         self.add_wf_bounds(all_substs, self.call_expr);
407
408         // the function type must also be well-formed (this is not
409         // implied by the substs being well-formed because of inherent
410         // impls and late-bound regions - see issue #28609).
411         self.register_wf_obligation(fty.into(), self.span, traits::MiscObligation);
412     }
413
414     ///////////////////////////////////////////////////////////////////////////
415     // MISCELLANY
416
417     fn predicates_require_illegal_sized_bound(
418         &self,
419         predicates: &ty::InstantiatedPredicates<'tcx>,
420     ) -> Option<Span> {
421         let sized_def_id = match self.tcx.lang_items().sized_trait() {
422             Some(def_id) => def_id,
423             None => return None,
424         };
425
426         traits::elaborate_predicates(self.tcx, predicates.predicates.iter().copied())
427             .filter_map(|obligation| match obligation.predicate.kind() {
428                 ty::PredicateKind::Trait(trait_pred, _) if trait_pred.def_id() == sized_def_id => {
429                     let span = predicates
430                         .predicates
431                         .iter()
432                         .zip(predicates.spans.iter())
433                         .find_map(
434                             |(p, span)| if *p == obligation.predicate { Some(*span) } else { None },
435                         )
436                         .unwrap_or(rustc_span::DUMMY_SP);
437                     Some((trait_pred, span))
438                 }
439                 _ => None,
440             })
441             .find_map(|(trait_pred, span)| match trait_pred.skip_binder().self_ty().kind {
442                 ty::Dynamic(..) => Some(span),
443                 _ => None,
444             })
445     }
446
447     fn enforce_illegal_method_limitations(&self, pick: &probe::Pick<'_>) {
448         // Disallow calls to the method `drop` defined in the `Drop` trait.
449         match pick.item.container {
450             ty::TraitContainer(trait_def_id) => callee::check_legal_trait_for_method_call(
451                 self.tcx,
452                 self.span,
453                 Some(self.self_expr.span),
454                 trait_def_id,
455             ),
456             ty::ImplContainer(..) => {}
457         }
458     }
459
460     fn upcast(
461         &mut self,
462         source_trait_ref: ty::PolyTraitRef<'tcx>,
463         target_trait_def_id: DefId,
464     ) -> ty::PolyTraitRef<'tcx> {
465         let upcast_trait_refs =
466             traits::upcast_choices(self.tcx, source_trait_ref, target_trait_def_id);
467
468         // must be exactly one trait ref or we'd get an ambig error etc
469         if upcast_trait_refs.len() != 1 {
470             span_bug!(
471                 self.span,
472                 "cannot uniquely upcast `{:?}` to `{:?}`: `{:?}`",
473                 source_trait_ref,
474                 target_trait_def_id,
475                 upcast_trait_refs
476             );
477         }
478
479         upcast_trait_refs.into_iter().next().unwrap()
480     }
481
482     fn replace_bound_vars_with_fresh_vars<T>(&self, value: &ty::Binder<T>) -> T
483     where
484         T: TypeFoldable<'tcx>,
485     {
486         self.fcx.replace_bound_vars_with_fresh_vars(self.span, infer::FnCall, value).0
487     }
488 }