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.
13 use check::{FnCtxt, callee};
14 use hir::def_id::DefId;
15 use rustc::ty::subst::Substs;
17 use rustc::ty::{self, LvaluePreference, NoPreference, PreferMutLvalue, Ty};
18 use rustc::ty::adjustment::{Adjustment, Adjust, AutoBorrow};
19 use rustc::ty::fold::TypeFoldable;
20 use rustc::infer::{self, InferOk};
26 struct ConfirmContext<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
27 fcx: &'a FnCtxt<'a, 'gcx, 'tcx>,
29 self_expr: &'gcx hir::Expr,
30 call_expr: &'gcx hir::Expr,
33 impl<'a, 'gcx, 'tcx> Deref for ConfirmContext<'a, 'gcx, 'tcx> {
34 type Target = FnCtxt<'a, 'gcx, 'tcx>;
35 fn deref(&self) -> &Self::Target {
40 impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
41 pub fn confirm_method(&self,
43 self_expr: &'gcx hir::Expr,
44 call_expr: &'gcx hir::Expr,
45 unadjusted_self_ty: Ty<'tcx>,
46 pick: probe::Pick<'tcx>,
47 supplied_method_types: Vec<Ty<'tcx>>)
48 -> ty::MethodCallee<'tcx> {
49 debug!("confirm(unadjusted_self_ty={:?}, pick={:?}, supplied_method_types={:?})",
52 supplied_method_types);
54 let mut confirm_cx = ConfirmContext::new(self, span, self_expr, call_expr);
55 confirm_cx.confirm(unadjusted_self_ty, pick, supplied_method_types)
59 impl<'a, 'gcx, 'tcx> ConfirmContext<'a, 'gcx, 'tcx> {
60 fn new(fcx: &'a FnCtxt<'a, 'gcx, 'tcx>,
62 self_expr: &'gcx hir::Expr,
63 call_expr: &'gcx hir::Expr)
64 -> ConfirmContext<'a, 'gcx, 'tcx> {
74 unadjusted_self_ty: Ty<'tcx>,
75 pick: probe::Pick<'tcx>,
76 supplied_method_types: Vec<Ty<'tcx>>)
77 -> ty::MethodCallee<'tcx> {
78 // Adjust the self expression the user provided and obtain the adjusted type.
79 let self_ty = self.adjust_self_ty(unadjusted_self_ty, &pick);
81 // Make sure nobody calls `drop()` explicitly.
82 self.enforce_illegal_method_limitations(&pick);
84 // Create substitutions for the method's type parameters.
85 let rcvr_substs = self.fresh_receiver_substs(self_ty, &pick);
86 let all_substs = self.instantiate_method_substs(&pick, supplied_method_types, rcvr_substs);
88 debug!("all_substs={:?}", all_substs);
90 // Create the final signature for the method, replacing late-bound regions.
91 let (method_ty, method_predicates) = self.instantiate_method_sig(&pick, all_substs);
93 // Unify the (adjusted) self type with what the method expects.
94 self.unify_receivers(self_ty, method_ty.fn_sig().input(0).skip_binder());
96 // Add any trait/regions obligations specified on the method's type parameters.
97 self.add_obligations(method_ty, all_substs, &method_predicates);
99 // Create the final `MethodCallee`.
100 let callee = ty::MethodCallee {
101 def_id: pick.item.def_id,
106 if let Some(hir::MutMutable) = pick.autoref {
107 self.convert_lvalue_derefs_to_mutable();
113 ///////////////////////////////////////////////////////////////////////////
116 fn adjust_self_ty(&mut self,
117 unadjusted_self_ty: Ty<'tcx>,
118 pick: &probe::Pick<'tcx>)
120 let autoref = if let Some(mutbl) = pick.autoref {
121 let region = self.next_region_var(infer::Autoref(self.span));
122 Some(AutoBorrow::Ref(region, mutbl))
124 // No unsizing should be performed without autoref (at
125 // least during method dispach). This is because we
126 // currently only unsize `[T;N]` to `[T]`, and naturally
127 // that must occur being a reference.
128 assert!(pick.unsize.is_none());
133 // Commit the autoderefs by calling `autoderef` again, but this
134 // time writing the results into the various tables.
135 let mut autoderef = self.autoderef(self.span, unadjusted_self_ty);
136 let (autoderefd_ty, n) = autoderef.nth(pick.autoderefs).unwrap();
137 assert_eq!(n, pick.autoderefs);
139 autoderef.unambiguous_final_ty();
140 autoderef.finalize(LvaluePreference::NoPreference, self.self_expr);
142 let target = pick.unsize.unwrap_or(autoderefd_ty);
143 let target = target.adjust_for_autoref(self.tcx, autoref);
145 // Write out the final adjustment.
146 self.apply_adjustment(self.self_expr.id, Adjustment {
147 kind: Adjust::DerefRef {
148 autoderefs: pick.autoderefs,
150 unsize: pick.unsize.is_some(),
158 ///////////////////////////////////////////////////////////////////////////
161 /// Returns a set of substitutions for the method *receiver* where all type and region
162 /// parameters are instantiated with fresh variables. This substitution does not include any
163 /// parameters declared on the method itself.
165 /// Note that this substitution may include late-bound regions from the impl level. If so,
166 /// these are instantiated later in the `instantiate_method_sig` routine.
167 fn fresh_receiver_substs(&mut self,
169 pick: &probe::Pick<'tcx>)
170 -> &'tcx Substs<'tcx> {
172 probe::InherentImplPick => {
173 let impl_def_id = pick.item.container.id();
174 assert!(self.tcx.impl_trait_ref(impl_def_id).is_none(),
175 "impl {:?} is not an inherent impl",
177 self.impl_self_ty(self.span, impl_def_id).substs
180 probe::ObjectPick => {
181 let trait_def_id = pick.item.container.id();
182 self.extract_existential_trait_ref(self_ty, |this, object_ty, principal| {
183 // The object data has no entry for the Self
184 // Type. For the purposes of this method call, we
185 // substitute the object type itself. This
186 // wouldn't be a sound substitution in all cases,
187 // since each instance of the object type is a
188 // different existential and hence could match
189 // distinct types (e.g., if `Self` appeared as an
190 // argument type), but those cases have already
191 // been ruled out when we deemed the trait to be
193 let original_poly_trait_ref = principal.with_self_ty(this.tcx, object_ty);
194 let upcast_poly_trait_ref = this.upcast(original_poly_trait_ref, trait_def_id);
195 let upcast_trait_ref =
196 this.replace_late_bound_regions_with_fresh_var(&upcast_poly_trait_ref);
197 debug!("original_poly_trait_ref={:?} upcast_trait_ref={:?} target_trait={:?}",
198 original_poly_trait_ref,
201 upcast_trait_ref.substs
205 probe::ExtensionImplPick(impl_def_id) => {
206 // The method being invoked is the method as defined on the trait,
207 // so return the substitutions from the trait. Consider:
209 // impl<A,B,C> Trait<A,B> for Foo<C> { ... }
211 // If we instantiate A, B, and C with $A, $B, and $C
212 // respectively, then we want to return the type
213 // parameters from the trait ([$A,$B]), not those from
214 // the impl ([$A,$B,$C]) not the receiver type ([$C]).
215 let impl_polytype = self.impl_self_ty(self.span, impl_def_id);
217 self.instantiate_type_scheme(self.span,
218 impl_polytype.substs,
219 &self.tcx.impl_trait_ref(impl_def_id).unwrap());
220 impl_trait_ref.substs
223 probe::TraitPick => {
224 let trait_def_id = pick.item.container.id();
226 // Make a trait reference `$0 : Trait<$1...$n>`
227 // consisting entirely of type variables. Later on in
228 // the process we will unify the transformed-self-type
229 // of the method with the actual type in order to
230 // unify some of these variables.
231 self.fresh_substs_for_item(self.span, trait_def_id)
234 probe::WhereClausePick(ref poly_trait_ref) => {
235 // Where clauses can have bound regions in them. We need to instantiate
236 // those to convert from a poly-trait-ref to a trait-ref.
237 self.replace_late_bound_regions_with_fresh_var(&poly_trait_ref).substs
242 fn extract_existential_trait_ref<R, F>(&mut self, self_ty: Ty<'tcx>, mut closure: F) -> R
243 where F: FnMut(&mut ConfirmContext<'a, 'gcx, 'tcx>,
245 ty::PolyExistentialTraitRef<'tcx>)
248 // If we specified that this is an object method, then the
249 // self-type ought to be something that can be dereferenced to
250 // yield an object-type (e.g., `&Object` or `Box<Object>`
253 // FIXME: this feels, like, super dubious
255 .autoderef(self.span, self_ty)
256 .filter_map(|(ty, _)| {
258 ty::TyDynamic(ref data, ..) => data.principal().map(|p| closure(self, ty, p)),
265 "self-type `{}` for ObjectPick never dereferenced to an object",
270 fn instantiate_method_substs(&mut self,
271 pick: &probe::Pick<'tcx>,
272 mut supplied_method_types: Vec<Ty<'tcx>>,
273 substs: &Substs<'tcx>)
274 -> &'tcx Substs<'tcx> {
275 // Determine the values for the generic parameters of the method.
276 // If they were not explicitly supplied, just construct fresh
278 let num_supplied_types = supplied_method_types.len();
279 let method_generics = self.tcx.generics_of(pick.item.def_id);
280 let num_method_types = method_generics.types.len();
282 if num_supplied_types > 0 && num_supplied_types != num_method_types {
283 if num_method_types == 0 {
284 struct_span_err!(self.tcx.sess,
287 "does not take type parameters")
288 .span_label(self.span, &"called with unneeded type parameters")
291 struct_span_err!(self.tcx.sess,
294 "incorrect number of type parameters given for this method: \
295 expected {}, found {}",
298 .span_label(self.span,
299 &format!("Passed {} type argument{}, expected {}",
301 if num_supplied_types != 1 { "s" } else { "" },
305 supplied_method_types = vec![self.tcx.types.err; num_method_types];
308 // Create subst for early-bound lifetime parameters, combining
309 // parameters from the type and those from the method.
311 // FIXME -- permit users to manually specify lifetimes
312 let supplied_start = substs.len() + method_generics.regions.len();
313 Substs::for_item(self.tcx, pick.item.def_id, |def, _| {
314 let i = def.index as usize;
315 if i < substs.len() {
318 self.region_var_for_def(self.span, def)
320 }, |def, cur_substs| {
321 let i = def.index as usize;
322 if i < substs.len() {
324 } else if supplied_method_types.is_empty() {
325 self.type_var_for_def(self.span, def, cur_substs)
327 supplied_method_types[i - supplied_start]
332 fn unify_receivers(&mut self, self_ty: Ty<'tcx>, method_self_ty: Ty<'tcx>) {
333 match self.sub_types(false, &self.misc(self.span), self_ty, method_self_ty) {
334 Ok(InferOk { obligations, value: () }) => {
335 self.register_predicates(obligations);
339 "{} was a subtype of {} but now is not?",
346 ///////////////////////////////////////////////////////////////////////////
349 fn instantiate_method_sig(&mut self,
350 pick: &probe::Pick<'tcx>,
351 all_substs: &'tcx Substs<'tcx>)
352 -> (Ty<'tcx>, ty::InstantiatedPredicates<'tcx>) {
353 debug!("instantiate_method_sig(pick={:?}, all_substs={:?})",
357 // Instantiate the bounds on the method with the
358 // type/early-bound-regions substitutions performed. There can
359 // be no late-bound regions appearing here.
360 let def_id = pick.item.def_id;
361 let method_predicates = self.tcx.predicates_of(def_id)
362 .instantiate(self.tcx, all_substs);
363 let method_predicates = self.normalize_associated_types_in(self.span,
366 debug!("method_predicates after subst = {:?}", method_predicates);
368 let sig = self.tcx.type_of(def_id).fn_sig();
370 // Instantiate late-bound regions and substitute the trait
371 // parameters into the method type to get the actual method type.
373 // NB: Instantiate late-bound regions first so that
374 // `instantiate_type_scheme` can normalize associated types that
375 // may reference those regions.
376 let method_sig = self.replace_late_bound_regions_with_fresh_var(&sig);
377 debug!("late-bound lifetimes from method instantiated, method_sig={:?}",
380 let method_sig = self.instantiate_type_scheme(self.span, all_substs, &method_sig);
381 debug!("type scheme substituted, method_sig={:?}", method_sig);
383 (self.tcx.mk_fn_def(def_id, all_substs, ty::Binder(method_sig)),
387 fn add_obligations(&mut self,
389 all_substs: &Substs<'tcx>,
390 method_predicates: &ty::InstantiatedPredicates<'tcx>) {
391 debug!("add_obligations: fty={:?} all_substs={:?} method_predicates={:?}",
396 self.add_obligations_for_parameters(traits::ObligationCause::misc(self.span, self.body_id),
399 // this is a projection from a trait reference, so we have to
400 // make sure that the trait reference inputs are well-formed.
401 self.add_wf_bounds(all_substs, self.call_expr);
403 // the function type must also be well-formed (this is not
404 // implied by the substs being well-formed because of inherent
405 // impls and late-bound regions - see issue #28609).
406 self.register_wf_obligation(fty, self.span, traits::MiscObligation);
409 ///////////////////////////////////////////////////////////////////////////
412 /// When we select a method with a mutable autoref, we have to go convert any
413 /// auto-derefs, indices, etc from `Deref` and `Index` into `DerefMut` and `IndexMut`
415 fn convert_lvalue_derefs_to_mutable(&self) {
416 // Gather up expressions we want to munge.
417 let mut exprs = Vec::new();
418 exprs.push(self.self_expr);
420 let last = exprs[exprs.len() - 1];
422 hir::ExprField(ref expr, _) |
423 hir::ExprTupField(ref expr, _) |
424 hir::ExprIndex(ref expr, _) |
425 hir::ExprUnary(hir::UnDeref, ref expr) => exprs.push(&expr),
430 debug!("convert_lvalue_derefs_to_mutable: exprs={:?}", exprs);
432 // Fix up autoderefs and derefs.
433 for (i, &expr) in exprs.iter().rev().enumerate() {
434 debug!("convert_lvalue_derefs_to_mutable: i={} expr={:?}", i, expr);
436 // Count autoderefs. We don't need to fix up the autoref - the parent
437 // expression will fix them up for us.
438 let adjustment = self.tables.borrow().adjustments.get(&expr.id).cloned();
440 Some(Adjustment { kind: Adjust::DerefRef { autoderefs, .. }, .. }) => {
442 let mut autoderef = self.autoderef(expr.span, self.node_ty(expr.id));
443 autoderef.nth(autoderefs).unwrap_or_else(|| {
445 "expr was deref-able {} times but now isn't?",
448 autoderef.finalize(PreferMutLvalue, expr);
454 // Don't retry the first one or we might infinite loop!
459 hir::ExprIndex(ref base_expr, ref index_expr) => {
460 // If this is an overloaded index, the
461 // adjustment will include an extra layer of
462 // autoref because the method is an &self/&mut
463 // self method. We have to peel it off to get
464 // the raw adjustment that `try_index_step`
465 // expects. This is annoying and horrible. We
466 // ought to recode this routine so it doesn't
467 // (ab)use the normal type checking paths.
468 let adj = self.tables.borrow_mut().adjustments.remove(&base_expr.id);
469 let (autoderefs, unsize, adjusted_base_ty) = match adj {
471 kind: Adjust::DerefRef { autoderefs, autoref, unsize },
478 Some(AutoBorrow::Ref(..)) => {}
480 span_bug!(base_expr.span,
481 "unexpected adjustment autoref {:?}",
486 (autoderefs, unsize, if unsize {
487 target.builtin_deref(false, NoPreference)
488 .expect("fixup: AutoBorrow::Ref is not &T")
491 let ty = self.node_ty(base_expr.id);
492 let mut ty = self.shallow_resolve(ty);
493 let mut method_type = |method_call: ty::MethodCall| {
494 self.tables.borrow().method_map.get(&method_call).map(|m| {
495 self.resolve_type_vars_if_possible(&m.ty)
499 if !ty.references_error() {
500 for i in 0..autoderefs {
501 ty = ty.adjust_for_autoderef(self.tcx,
512 None => (0, false, self.node_ty(base_expr.id)),
514 span_bug!(base_expr.span, "unexpected adjustment type");
518 let index_expr_ty = self.node_ty(index_expr.id);
520 let result = self.try_index_step(ty::MethodCall::expr(expr.id),
529 if let Some((input_ty, return_ty)) = result {
530 self.demand_suptype(index_expr.span, input_ty, index_expr_ty);
532 let expr_ty = self.node_ty(expr.id);
533 self.demand_suptype(expr.span, expr_ty, return_ty);
536 hir::ExprUnary(hir::UnDeref, ref base_expr) => {
537 // if this is an overloaded deref, then re-evaluate with
538 // a preference for mut
539 let method_call = ty::MethodCall::expr(expr.id);
540 if self.tables.borrow().method_map.contains_key(&method_call) {
541 self.tables.borrow_mut().adjustments.remove(&base_expr.id);
542 let method = self.try_overloaded_deref(expr.span,
544 self.node_ty(base_expr.id),
546 let ok = method.expect("re-trying deref failed");
547 let method = self.register_infer_ok_obligations(ok);
548 self.tables.borrow_mut().method_map.insert(method_call, method);
556 ///////////////////////////////////////////////////////////////////////////
559 fn enforce_illegal_method_limitations(&self, pick: &probe::Pick) {
560 // Disallow calls to the method `drop` defined in the `Drop` trait.
561 match pick.item.container {
562 ty::TraitContainer(trait_def_id) => {
563 callee::check_legal_trait_for_method_call(self.tcx, self.span, trait_def_id)
565 ty::ImplContainer(..) => {}
570 source_trait_ref: ty::PolyTraitRef<'tcx>,
571 target_trait_def_id: DefId)
572 -> ty::PolyTraitRef<'tcx> {
573 let upcast_trait_refs = self.tcx
574 .upcast_choices(source_trait_ref.clone(), target_trait_def_id);
576 // must be exactly one trait ref or we'd get an ambig error etc
577 if upcast_trait_refs.len() != 1 {
579 "cannot uniquely upcast `{:?}` to `{:?}`: `{:?}`",
585 upcast_trait_refs.into_iter().next().unwrap()
588 fn replace_late_bound_regions_with_fresh_var<T>(&self, value: &ty::Binder<T>) -> T
589 where T: TypeFoldable<'tcx>
592 .replace_late_bound_regions_with_fresh_var(self.span, infer::FnCall, value)