1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use super::{probe, MethodCallee};
14 use check::{FnCtxt, PlaceOp, callee, Needs};
16 use hir::def_id::DefId;
17 use rustc::ty::subst::Substs;
19 use rustc::ty::{self, Ty, GenericParamDefKind};
20 use rustc::ty::subst::Subst;
21 use rustc::ty::adjustment::{Adjustment, Adjust, OverloadedDeref};
22 use rustc::ty::adjustment::{AllowTwoPhase, AutoBorrow, AutoBorrowMutability};
23 use rustc::ty::fold::TypeFoldable;
24 use rustc::infer::{self, InferOk};
30 struct ConfirmContext<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
31 fcx: &'a FnCtxt<'a, 'gcx, 'tcx>,
33 self_expr: &'gcx hir::Expr,
34 call_expr: &'gcx hir::Expr,
37 impl<'a, 'gcx, 'tcx> Deref for ConfirmContext<'a, 'gcx, 'tcx> {
38 type Target = FnCtxt<'a, 'gcx, 'tcx>;
39 fn deref(&self) -> &Self::Target {
44 pub struct ConfirmResult<'tcx> {
45 pub callee: MethodCallee<'tcx>,
46 pub illegal_sized_bound: bool,
49 impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
50 pub fn confirm_method(
53 self_expr: &'gcx hir::Expr,
54 call_expr: &'gcx hir::Expr,
55 unadjusted_self_ty: Ty<'tcx>,
56 pick: probe::Pick<'tcx>,
57 segment: &hir::PathSegment,
58 ) -> ConfirmResult<'tcx> {
60 "confirm(unadjusted_self_ty={:?}, pick={:?}, generic_args={:?})",
66 let mut confirm_cx = ConfirmContext::new(self, span, self_expr, call_expr);
67 confirm_cx.confirm(unadjusted_self_ty, pick, segment)
71 impl<'a, 'gcx, 'tcx> ConfirmContext<'a, 'gcx, 'tcx> {
72 fn new(fcx: &'a FnCtxt<'a, 'gcx, 'tcx>,
74 self_expr: &'gcx hir::Expr,
75 call_expr: &'gcx hir::Expr)
76 -> ConfirmContext<'a, 'gcx, 'tcx> {
87 unadjusted_self_ty: Ty<'tcx>,
88 pick: probe::Pick<'tcx>,
89 segment: &hir::PathSegment,
90 ) -> ConfirmResult<'tcx> {
91 // Adjust the self expression the user provided and obtain the adjusted type.
92 let self_ty = self.adjust_self_ty(unadjusted_self_ty, &pick);
94 // Create substitutions for the method's type parameters.
95 let rcvr_substs = self.fresh_receiver_substs(self_ty, &pick);
96 let all_substs = self.instantiate_method_substs(&pick, segment, rcvr_substs);
98 debug!("all_substs={:?}", all_substs);
100 // Create the final signature for the method, replacing late-bound regions.
101 let (method_sig, method_predicates) = self.instantiate_method_sig(&pick, all_substs);
103 // Unify the (adjusted) self type with what the method expects.
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 =
110 self.normalize_associated_types_in(self.span, &method_sig.inputs()[0]);
111 self.unify_receivers(self_ty, method_sig_rcvr);
113 let (method_sig, method_predicates) =
114 self.normalize_associated_types_in(self.span, &(method_sig, method_predicates));
116 // Make sure nobody calls `drop()` explicitly.
117 self.enforce_illegal_method_limitations(&pick);
119 // If there is a `Self: Sized` bound and `Self` is a trait object, it is possible that
120 // something which derefs to `Self` actually implements the trait and the caller
121 // wanted to make a static dispatch on it but forgot to import the trait.
122 // See test `src/test/ui/issue-35976.rs`.
124 // In that case, we'll error anyway, but we'll also re-run the search with all traits
125 // in scope, and if we find another method which can be used, we'll output an
126 // appropriate hint suggesting to import the trait.
127 let illegal_sized_bound = self.predicates_require_illegal_sized_bound(&method_predicates);
129 // Add any trait/regions obligations specified on the method's type parameters.
130 // We won't add these if we encountered an illegal sized bound, so that we can use
131 // a custom error in that case.
132 if !illegal_sized_bound {
133 let method_ty = self.tcx.mk_fn_ptr(ty::Binder::bind(method_sig));
134 self.add_obligations(method_ty, all_substs, &method_predicates);
137 // Create the final `MethodCallee`.
138 let callee = MethodCallee {
139 def_id: pick.item.def_id,
144 if let Some(hir::MutMutable) = pick.autoref {
145 self.convert_place_derefs_to_mutable();
148 ConfirmResult { callee, illegal_sized_bound }
151 ///////////////////////////////////////////////////////////////////////////
154 fn adjust_self_ty(&mut self,
155 unadjusted_self_ty: Ty<'tcx>,
156 pick: &probe::Pick<'tcx>)
158 // Commit the autoderefs by calling `autoderef` again, but this
159 // time writing the results into the various tables.
160 let mut autoderef = self.autoderef(self.span, unadjusted_self_ty);
161 let (_, n) = autoderef.nth(pick.autoderefs).unwrap();
162 assert_eq!(n, pick.autoderefs);
164 let mut adjustments = autoderef.adjust_steps(Needs::None);
166 let mut target = autoderef.unambiguous_final_ty();
168 if let Some(mutbl) = pick.autoref {
169 let region = self.next_region_var(infer::Autoref(self.span));
170 target = self.tcx.mk_ref(region, ty::TypeAndMut {
174 let mutbl = match mutbl {
175 hir::MutImmutable => AutoBorrowMutability::Immutable,
176 hir::MutMutable => AutoBorrowMutability::Mutable {
177 // Method call receivers are the primary use case
178 // for two-phase borrows.
179 allow_two_phase_borrow: AllowTwoPhase::Yes,
182 adjustments.push(Adjustment {
183 kind: Adjust::Borrow(AutoBorrow::Ref(region, mutbl)),
187 if let Some(unsize_target) = pick.unsize {
188 target = self.tcx.mk_ref(region, ty::TypeAndMut {
192 adjustments.push(Adjustment {
193 kind: Adjust::Unsize,
198 // No unsizing should be performed without autoref (at
199 // least during method dispach). This is because we
200 // currently only unsize `[T;N]` to `[T]`, and naturally
201 // that must occur being a reference.
202 assert!(pick.unsize.is_none());
205 autoderef.finalize();
207 // Write out the final adjustments.
208 self.apply_adjustments(self.self_expr, adjustments);
213 /// Returns a set of substitutions for the method *receiver* where all type and region
214 /// parameters are instantiated with fresh variables. This substitution does not include any
215 /// parameters declared on the method itself.
217 /// Note that this substitution may include late-bound regions from the impl level. If so,
218 /// these are instantiated later in the `instantiate_method_sig` routine.
219 fn fresh_receiver_substs(&mut self,
221 pick: &probe::Pick<'tcx>)
222 -> &'tcx Substs<'tcx> {
224 probe::InherentImplPick => {
225 let impl_def_id = pick.item.container.id();
226 assert!(self.tcx.impl_trait_ref(impl_def_id).is_none(),
227 "impl {:?} is not an inherent impl",
229 self.impl_self_ty(self.span, impl_def_id).substs
232 probe::ObjectPick => {
233 let trait_def_id = pick.item.container.id();
234 self.extract_existential_trait_ref(self_ty, |this, object_ty, principal| {
235 // The object data has no entry for the Self
236 // Type. For the purposes of this method call, we
237 // substitute the object type itself. This
238 // wouldn't be a sound substitution in all cases,
239 // since each instance of the object type is a
240 // different existential and hence could match
241 // distinct types (e.g., if `Self` appeared as an
242 // argument type), but those cases have already
243 // been ruled out when we deemed the trait to be
245 let original_poly_trait_ref = principal.with_self_ty(this.tcx, object_ty);
246 let upcast_poly_trait_ref = this.upcast(original_poly_trait_ref, trait_def_id);
247 let upcast_trait_ref =
248 this.replace_bound_vars_with_fresh_vars(&upcast_poly_trait_ref);
249 debug!("original_poly_trait_ref={:?} upcast_trait_ref={:?} target_trait={:?}",
250 original_poly_trait_ref,
253 upcast_trait_ref.substs
257 probe::TraitPick => {
258 let trait_def_id = pick.item.container.id();
260 // Make a trait reference `$0 : Trait<$1...$n>`
261 // consisting entirely of type variables. Later on in
262 // the process we will unify the transformed-self-type
263 // of the method with the actual type in order to
264 // unify some of these variables.
265 self.fresh_substs_for_item(self.span, trait_def_id)
268 probe::WhereClausePick(ref poly_trait_ref) => {
269 // Where clauses can have bound regions in them. We need to instantiate
270 // those to convert from a poly-trait-ref to a trait-ref.
271 self.replace_bound_vars_with_fresh_vars(&poly_trait_ref).substs
276 fn extract_existential_trait_ref<R, F>(&mut self, self_ty: Ty<'tcx>, mut closure: F) -> R
277 where F: FnMut(&mut ConfirmContext<'a, 'gcx, 'tcx>,
279 ty::PolyExistentialTraitRef<'tcx>)
282 // If we specified that this is an object method, then the
283 // self-type ought to be something that can be dereferenced to
284 // yield an object-type (e.g., `&Object` or `Box<Object>`
287 // FIXME: this feels, like, super dubious
289 .autoderef(self.span, self_ty)
290 .include_raw_pointers()
291 .filter_map(|(ty, _)|
293 ty::Dynamic(ref data, ..) => Some(closure(self, ty, data.principal())),
300 "self-type `{}` for ObjectPick never dereferenced to an object",
305 fn instantiate_method_substs(
307 pick: &probe::Pick<'tcx>,
308 seg: &hir::PathSegment,
309 parent_substs: &Substs<'tcx>,
310 ) -> &'tcx Substs<'tcx> {
311 // Determine the values for the generic parameters of the method.
312 // If they were not explicitly supplied, just construct fresh
314 let generics = self.tcx.generics_of(pick.item.def_id);
315 AstConv::check_generic_arg_count_for_call(
320 true, // `is_method_call`
323 // Create subst for early-bound lifetime parameters, combining
324 // parameters from the type and those from the method.
325 assert_eq!(generics.parent_count, parent_substs.len());
327 AstConv::create_substs_for_generic_args(
333 // Provide the generic args, and whether types should be inferred.
335 // The last argument of the returned tuple here is unimportant.
336 if let Some(ref data) = seg.args {
342 // Provide substitutions for parameters for which (valid) arguments have been provided.
344 match (¶m.kind, arg) {
345 (GenericParamDefKind::Lifetime, GenericArg::Lifetime(lt)) => {
346 AstConv::ast_region_to_region(self.fcx, lt, Some(param)).into()
348 (GenericParamDefKind::Type { .. }, GenericArg::Type(ty)) => {
349 self.to_ty(ty).into()
354 // Provide substitutions for parameters for which arguments are inferred.
355 |_, param, _| self.var_for_def(self.span, param),
359 fn unify_receivers(&mut self, self_ty: Ty<'tcx>, method_self_ty: Ty<'tcx>) {
360 match self.at(&self.misc(self.span), self.param_env).sup(method_self_ty, self_ty) {
361 Ok(InferOk { obligations, value: () }) => {
362 self.register_predicates(obligations);
366 "{} was a subtype of {} but now is not?",
373 // NOTE: this returns the *unnormalized* predicates and method sig. Because of
374 // inference guessing, the predicates and method signature can't be normalized
375 // until we unify the `Self` type.
376 fn instantiate_method_sig(&mut self,
377 pick: &probe::Pick<'tcx>,
378 all_substs: &'tcx Substs<'tcx>)
379 -> (ty::FnSig<'tcx>, ty::InstantiatedPredicates<'tcx>) {
380 debug!("instantiate_method_sig(pick={:?}, all_substs={:?})",
384 // Instantiate the bounds on the method with the
385 // type/early-bound-regions substitutions performed. There can
386 // be no late-bound regions appearing here.
387 let def_id = pick.item.def_id;
388 let method_predicates = self.tcx.predicates_of(def_id)
389 .instantiate(self.tcx, all_substs);
391 debug!("method_predicates after subst = {:?}", method_predicates);
393 let sig = self.tcx.fn_sig(def_id);
395 // Instantiate late-bound regions and substitute the trait
396 // parameters into the method type to get the actual method type.
398 // NB: Instantiate late-bound regions first so that
399 // `instantiate_type_scheme` can normalize associated types that
400 // may reference those regions.
401 let method_sig = self.replace_bound_vars_with_fresh_vars(&sig);
402 debug!("late-bound lifetimes from method instantiated, method_sig={:?}",
405 let method_sig = method_sig.subst(self.tcx, all_substs);
406 debug!("type scheme substituted, method_sig={:?}", method_sig);
408 (method_sig, method_predicates)
411 fn add_obligations(&mut self,
413 all_substs: &Substs<'tcx>,
414 method_predicates: &ty::InstantiatedPredicates<'tcx>) {
415 debug!("add_obligations: fty={:?} all_substs={:?} method_predicates={:?}",
420 self.add_obligations_for_parameters(traits::ObligationCause::misc(self.span, self.body_id),
423 // this is a projection from a trait reference, so we have to
424 // make sure that the trait reference inputs are well-formed.
425 self.add_wf_bounds(all_substs, self.call_expr);
427 // the function type must also be well-formed (this is not
428 // implied by the substs being well-formed because of inherent
429 // impls and late-bound regions - see issue #28609).
430 self.register_wf_obligation(fty, self.span, traits::MiscObligation);
433 ///////////////////////////////////////////////////////////////////////////
436 /// When we select a method with a mutable autoref, we have to go convert any
437 /// auto-derefs, indices, etc from `Deref` and `Index` into `DerefMut` and `IndexMut`
439 fn convert_place_derefs_to_mutable(&self) {
440 // Gather up expressions we want to munge.
441 let mut exprs = vec![self.self_expr];
444 match exprs.last().unwrap().node {
445 hir::ExprKind::Field(ref expr, _) |
446 hir::ExprKind::Index(ref expr, _) |
447 hir::ExprKind::Unary(hir::UnDeref, ref expr) => exprs.push(&expr),
452 debug!("convert_place_derefs_to_mutable: exprs={:?}", exprs);
454 // Fix up autoderefs and derefs.
455 for (i, &expr) in exprs.iter().rev().enumerate() {
456 debug!("convert_place_derefs_to_mutable: i={} expr={:?}", i, expr);
458 // Fix up the autoderefs. Autorefs can only occur immediately preceding
459 // overloaded place ops, and will be fixed by them in order to get
460 // the correct region.
461 let mut source = self.node_ty(expr.hir_id);
462 // Do not mutate adjustments in place, but rather take them,
463 // and replace them after mutating them, to avoid having the
464 // tables borrowed during (`deref_mut`) method resolution.
465 let previous_adjustments = self.tables
468 .remove(expr.hir_id);
469 if let Some(mut adjustments) = previous_adjustments {
470 let needs = Needs::MutPlace;
471 for adjustment in &mut adjustments {
472 if let Adjust::Deref(Some(ref mut deref)) = adjustment.kind {
473 if let Some(ok) = self.try_overloaded_deref(expr.span, source, needs) {
474 let method = self.register_infer_ok_obligations(ok);
475 if let ty::Ref(region, _, mutbl) = method.sig.output().sty {
476 *deref = OverloadedDeref {
483 source = adjustment.target;
485 self.tables.borrow_mut().adjustments_mut().insert(expr.hir_id, adjustments);
489 hir::ExprKind::Index(ref base_expr, ref index_expr) => {
490 let index_expr_ty = self.node_ty(index_expr.hir_id);
491 self.convert_place_op_to_mutable(
492 PlaceOp::Index, expr, base_expr, &[index_expr_ty]);
494 hir::ExprKind::Unary(hir::UnDeref, ref base_expr) => {
495 self.convert_place_op_to_mutable(
496 PlaceOp::Deref, expr, base_expr, &[]);
503 fn convert_place_op_to_mutable(&self,
506 base_expr: &hir::Expr,
507 arg_tys: &[Ty<'tcx>])
509 debug!("convert_place_op_to_mutable({:?}, {:?}, {:?}, {:?})",
510 op, expr, base_expr, arg_tys);
511 if !self.tables.borrow().is_method_call(expr) {
512 debug!("convert_place_op_to_mutable - builtin, nothing to do");
516 let base_ty = self.tables.borrow().expr_adjustments(base_expr).last()
517 .map_or_else(|| self.node_ty(expr.hir_id), |adj| adj.target);
518 let base_ty = self.resolve_type_vars_if_possible(&base_ty);
520 // Need to deref because overloaded place ops take self by-reference.
521 let base_ty = base_ty.builtin_deref(false)
522 .expect("place op takes something that is not a ref")
525 let method = self.try_overloaded_place_op(
526 expr.span, base_ty, arg_tys, Needs::MutPlace, op);
527 let method = match method {
528 Some(ok) => self.register_infer_ok_obligations(ok),
529 None => return self.tcx.sess.delay_span_bug(expr.span, "re-trying op failed")
531 debug!("convert_place_op_to_mutable: method={:?}", method);
532 self.write_method_call(expr.hir_id, method);
534 let (region, mutbl) = if let ty::Ref(r, _, mutbl) = method.sig.inputs()[0].sty {
537 span_bug!(expr.span, "input to place op is not a ref?");
540 // Convert the autoref in the base expr to mutable with the correct
541 // region and mutability.
542 let base_expr_ty = self.node_ty(base_expr.hir_id);
543 if let Some(adjustments) = self.tables
546 .get_mut(base_expr.hir_id) {
547 let mut source = base_expr_ty;
548 for adjustment in &mut adjustments[..] {
549 if let Adjust::Borrow(AutoBorrow::Ref(..)) = adjustment.kind {
550 debug!("convert_place_op_to_mutable: converting autoref {:?}", adjustment);
551 let mutbl = match mutbl {
552 hir::MutImmutable => AutoBorrowMutability::Immutable,
553 hir::MutMutable => AutoBorrowMutability::Mutable {
554 // For initial two-phase borrow
555 // deployment, conservatively omit
556 // overloaded operators.
557 allow_two_phase_borrow: AllowTwoPhase::No,
560 adjustment.kind = Adjust::Borrow(AutoBorrow::Ref(region, mutbl));
561 adjustment.target = self.tcx.mk_ref(region, ty::TypeAndMut {
566 source = adjustment.target;
569 // If we have an autoref followed by unsizing at the end, fix the unsize target.
570 match adjustments[..] {
571 [.., Adjustment { kind: Adjust::Borrow(AutoBorrow::Ref(..)), .. },
572 Adjustment { kind: Adjust::Unsize, ref mut target }] => {
573 *target = method.sig.inputs()[0];
580 ///////////////////////////////////////////////////////////////////////////
583 fn predicates_require_illegal_sized_bound(&self,
584 predicates: &ty::InstantiatedPredicates<'tcx>)
586 let sized_def_id = match self.tcx.lang_items().sized_trait() {
587 Some(def_id) => def_id,
588 None => return false,
591 traits::elaborate_predicates(self.tcx, predicates.predicates.clone())
592 .filter_map(|predicate| {
594 ty::Predicate::Trait(trait_pred) if trait_pred.def_id() == sized_def_id =>
600 match trait_pred.skip_binder().self_ty().sty {
601 ty::Dynamic(..) => true,
607 fn enforce_illegal_method_limitations(&self, pick: &probe::Pick) {
608 // Disallow calls to the method `drop` defined in the `Drop` trait.
609 match pick.item.container {
610 ty::TraitContainer(trait_def_id) => {
611 callee::check_legal_trait_for_method_call(self.tcx, self.span, trait_def_id)
613 ty::ImplContainer(..) => {}
618 source_trait_ref: ty::PolyTraitRef<'tcx>,
619 target_trait_def_id: DefId)
620 -> ty::PolyTraitRef<'tcx> {
621 let upcast_trait_refs = self.tcx
622 .upcast_choices(source_trait_ref.clone(), target_trait_def_id);
624 // must be exactly one trait ref or we'd get an ambig error etc
625 if upcast_trait_refs.len() != 1 {
627 "cannot uniquely upcast `{:?}` to `{:?}`: `{:?}`",
633 upcast_trait_refs.into_iter().next().unwrap()
636 fn replace_bound_vars_with_fresh_vars<T>(&self, value: &ty::Binder<T>) -> T
637 where T: TypeFoldable<'tcx>
639 self.fcx.replace_bound_vars_with_fresh_vars(self.span, infer::FnCall, value).0