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, LvalueOp, callee};
15 use hir::def_id::DefId;
16 use rustc::ty::subst::Substs;
18 use rustc::ty::{self, LvaluePreference, NoPreference, PreferMutLvalue, Ty};
19 use rustc::ty::subst::Subst;
20 use rustc::ty::adjustment::{Adjustment, Adjust, AutoBorrow, OverloadedDeref};
21 use rustc::ty::fold::TypeFoldable;
22 use rustc::infer::{self, InferOk};
28 struct ConfirmContext<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
29 fcx: &'a FnCtxt<'a, 'gcx, 'tcx>,
31 self_expr: &'gcx hir::Expr,
32 call_expr: &'gcx hir::Expr,
35 impl<'a, 'gcx, 'tcx> Deref for ConfirmContext<'a, 'gcx, 'tcx> {
36 type Target = FnCtxt<'a, 'gcx, 'tcx>;
37 fn deref(&self) -> &Self::Target {
42 pub struct ConfirmResult<'tcx> {
43 pub callee: MethodCallee<'tcx>,
44 pub illegal_sized_bound: bool,
47 impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
48 pub fn confirm_method(&self,
50 self_expr: &'gcx hir::Expr,
51 call_expr: &'gcx hir::Expr,
52 unadjusted_self_ty: Ty<'tcx>,
53 pick: probe::Pick<'tcx>,
54 segment: &hir::PathSegment)
55 -> ConfirmResult<'tcx> {
56 debug!("confirm(unadjusted_self_ty={:?}, pick={:?}, generic_args={:?})",
61 let mut confirm_cx = ConfirmContext::new(self, span, self_expr, call_expr);
62 confirm_cx.confirm(unadjusted_self_ty, pick, segment)
66 impl<'a, 'gcx, 'tcx> ConfirmContext<'a, 'gcx, 'tcx> {
67 fn new(fcx: &'a FnCtxt<'a, 'gcx, 'tcx>,
69 self_expr: &'gcx hir::Expr,
70 call_expr: &'gcx hir::Expr)
71 -> ConfirmContext<'a, 'gcx, 'tcx> {
81 unadjusted_self_ty: Ty<'tcx>,
82 pick: probe::Pick<'tcx>,
83 segment: &hir::PathSegment)
84 -> ConfirmResult<'tcx> {
85 // Adjust the self expression the user provided and obtain the adjusted type.
86 let self_ty = self.adjust_self_ty(unadjusted_self_ty, &pick);
88 // Create substitutions for the method's type parameters.
89 let rcvr_substs = self.fresh_receiver_substs(self_ty, &pick);
90 let all_substs = self.instantiate_method_substs(&pick, segment, rcvr_substs);
92 debug!("all_substs={:?}", all_substs);
94 // Create the final signature for the method, replacing late-bound regions.
95 let (method_sig, method_predicates) = self.instantiate_method_sig(&pick, all_substs);
97 // Unify the (adjusted) self type with what the method expects.
99 // SUBTLE: if we want good error messages, because of "guessing" while matching
100 // traits, no trait system method can be called before this point because they
101 // could alter our Self-type, except for normalizing the receiver from the
102 // signature (which is also done during probing).
103 let method_sig_rcvr =
104 self.normalize_associated_types_in(self.span, &method_sig.inputs()[0]);
105 self.unify_receivers(self_ty, method_sig_rcvr);
107 let (method_sig, method_predicates) =
108 self.normalize_associated_types_in(self.span, &(method_sig, method_predicates));
110 // Make sure nobody calls `drop()` explicitly.
111 self.enforce_illegal_method_limitations(&pick);
113 // If there is a `Self: Sized` bound and `Self` is a trait object, it is possible that
114 // something which derefs to `Self` actually implements the trait and the caller
115 // wanted to make a static dispatch on it but forgot to import the trait.
116 // See test `src/test/ui/issue-35976.rs`.
118 // In that case, we'll error anyway, but we'll also re-run the search with all traits
119 // in scope, and if we find another method which can be used, we'll output an
120 // appropriate hint suggesting to import the trait.
121 let illegal_sized_bound = self.predicates_require_illegal_sized_bound(&method_predicates);
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 {
127 let method_ty = self.tcx.mk_fn_ptr(ty::Binder(method_sig));
128 self.add_obligations(method_ty, all_substs, &method_predicates);
131 // Create the final `MethodCallee`.
132 let callee = MethodCallee {
133 def_id: pick.item.def_id,
138 if let Some(hir::MutMutable) = pick.autoref {
139 self.convert_lvalue_derefs_to_mutable();
142 ConfirmResult { callee, illegal_sized_bound }
145 ///////////////////////////////////////////////////////////////////////////
148 fn adjust_self_ty(&mut self,
149 unadjusted_self_ty: Ty<'tcx>,
150 pick: &probe::Pick<'tcx>)
152 // Commit the autoderefs by calling `autoderef` again, but this
153 // time writing the results into the various tables.
154 let mut autoderef = self.autoderef(self.span, unadjusted_self_ty);
155 let (_, n) = autoderef.nth(pick.autoderefs).unwrap();
156 assert_eq!(n, pick.autoderefs);
158 let mut adjustments = autoderef.adjust_steps(LvaluePreference::NoPreference);
160 let mut target = autoderef.unambiguous_final_ty();
162 if let Some(mutbl) = pick.autoref {
163 let region = self.next_region_var(infer::Autoref(self.span));
164 target = self.tcx.mk_ref(region, ty::TypeAndMut {
168 adjustments.push(Adjustment {
169 kind: Adjust::Borrow(AutoBorrow::Ref(region, mutbl)),
173 if let Some(unsize_target) = pick.unsize {
174 target = self.tcx.mk_ref(region, ty::TypeAndMut {
178 adjustments.push(Adjustment {
179 kind: Adjust::Unsize,
184 // No unsizing should be performed without autoref (at
185 // least during method dispach). This is because we
186 // currently only unsize `[T;N]` to `[T]`, and naturally
187 // that must occur being a reference.
188 assert!(pick.unsize.is_none());
191 autoderef.finalize();
193 // Write out the final adjustments.
194 self.apply_adjustments(self.self_expr, adjustments);
199 ///////////////////////////////////////////////////////////////////////////
202 /// Returns a set of substitutions for the method *receiver* where all type and region
203 /// parameters are instantiated with fresh variables. This substitution does not include any
204 /// parameters declared on the method itself.
206 /// Note that this substitution may include late-bound regions from the impl level. If so,
207 /// these are instantiated later in the `instantiate_method_sig` routine.
208 fn fresh_receiver_substs(&mut self,
210 pick: &probe::Pick<'tcx>)
211 -> &'tcx Substs<'tcx> {
213 probe::InherentImplPick => {
214 let impl_def_id = pick.item.container.id();
215 assert!(self.tcx.impl_trait_ref(impl_def_id).is_none(),
216 "impl {:?} is not an inherent impl",
218 self.impl_self_ty(self.span, impl_def_id).substs
221 probe::ObjectPick => {
222 let trait_def_id = pick.item.container.id();
223 self.extract_existential_trait_ref(self_ty, |this, object_ty, principal| {
224 // The object data has no entry for the Self
225 // Type. For the purposes of this method call, we
226 // substitute the object type itself. This
227 // wouldn't be a sound substitution in all cases,
228 // since each instance of the object type is a
229 // different existential and hence could match
230 // distinct types (e.g., if `Self` appeared as an
231 // argument type), but those cases have already
232 // been ruled out when we deemed the trait to be
234 let original_poly_trait_ref = principal.with_self_ty(this.tcx, object_ty);
235 let upcast_poly_trait_ref = this.upcast(original_poly_trait_ref, trait_def_id);
236 let upcast_trait_ref =
237 this.replace_late_bound_regions_with_fresh_var(&upcast_poly_trait_ref);
238 debug!("original_poly_trait_ref={:?} upcast_trait_ref={:?} target_trait={:?}",
239 original_poly_trait_ref,
242 upcast_trait_ref.substs
246 probe::TraitPick => {
247 let trait_def_id = pick.item.container.id();
249 // Make a trait reference `$0 : Trait<$1...$n>`
250 // consisting entirely of type variables. Later on in
251 // the process we will unify the transformed-self-type
252 // of the method with the actual type in order to
253 // unify some of these variables.
254 self.fresh_substs_for_item(self.span, trait_def_id)
257 probe::WhereClausePick(ref poly_trait_ref) => {
258 // Where clauses can have bound regions in them. We need to instantiate
259 // those to convert from a poly-trait-ref to a trait-ref.
260 self.replace_late_bound_regions_with_fresh_var(&poly_trait_ref).substs
265 fn extract_existential_trait_ref<R, F>(&mut self, self_ty: Ty<'tcx>, mut closure: F) -> R
266 where F: FnMut(&mut ConfirmContext<'a, 'gcx, 'tcx>,
268 ty::PolyExistentialTraitRef<'tcx>)
271 // If we specified that this is an object method, then the
272 // self-type ought to be something that can be dereferenced to
273 // yield an object-type (e.g., `&Object` or `Box<Object>`
276 // FIXME: this feels, like, super dubious
278 .autoderef(self.span, self_ty)
279 .include_raw_pointers()
280 .filter_map(|(ty, _)| {
282 ty::TyDynamic(ref data, ..) => data.principal().map(|p| closure(self, ty, p)),
289 "self-type `{}` for ObjectPick never dereferenced to an object",
294 fn instantiate_method_substs(&mut self,
295 pick: &probe::Pick<'tcx>,
296 segment: &hir::PathSegment,
297 parent_substs: &Substs<'tcx>)
298 -> &'tcx Substs<'tcx> {
299 // Determine the values for the generic parameters of the method.
300 // If they were not explicitly supplied, just construct fresh
302 let method_generics = self.tcx.generics_of(pick.item.def_id);
303 let mut fn_segment = Some((segment, method_generics));
304 self.fcx.check_path_parameter_count(self.span, &mut fn_segment, true);
306 // Create subst for early-bound lifetime parameters, combining
307 // parameters from the type and those from the method.
308 assert_eq!(method_generics.parent_count(), parent_substs.len());
309 let provided = &segment.parameters;
310 Substs::for_item(self.tcx, pick.item.def_id, |def, _| {
311 let i = def.index as usize;
312 if i < parent_substs.len() {
313 parent_substs.region_at(i)
314 } else if let Some(lifetime)
315 = provided.as_ref().and_then(|p| p.lifetimes.get(i - parent_substs.len())) {
316 AstConv::ast_region_to_region(self.fcx, lifetime, Some(def))
318 self.region_var_for_def(self.span, def)
320 }, |def, cur_substs| {
321 let i = def.index as usize;
322 if i < parent_substs.len() {
323 parent_substs.type_at(i)
324 } else if let Some(ast_ty)
325 = provided.as_ref().and_then(|p| {
326 p.types.get(i - parent_substs.len() - method_generics.regions.len())
331 self.type_var_for_def(self.span, def, cur_substs)
336 fn unify_receivers(&mut self, self_ty: Ty<'tcx>, method_self_ty: Ty<'tcx>) {
337 match self.at(&self.misc(self.span), self.param_env).sup(method_self_ty, self_ty) {
338 Ok(InferOk { obligations, value: () }) => {
339 self.register_predicates(obligations);
343 "{} was a subtype of {} but now is not?",
350 ///////////////////////////////////////////////////////////////////////////
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(&mut self,
357 pick: &probe::Pick<'tcx>,
358 all_substs: &'tcx Substs<'tcx>)
359 -> (ty::FnSig<'tcx>, ty::InstantiatedPredicates<'tcx>) {
360 debug!("instantiate_method_sig(pick={:?}, all_substs={:?})",
364 // Instantiate the bounds on the method with the
365 // type/early-bound-regions substitutions performed. There can
366 // be no late-bound regions appearing here.
367 let def_id = pick.item.def_id;
368 let method_predicates = self.tcx.predicates_of(def_id)
369 .instantiate(self.tcx, all_substs);
371 debug!("method_predicates after subst = {:?}", method_predicates);
373 let sig = self.tcx.fn_sig(def_id);
375 // Instantiate late-bound regions and substitute the trait
376 // parameters into the method type to get the actual method type.
378 // NB: Instantiate late-bound regions first so that
379 // `instantiate_type_scheme` can normalize associated types that
380 // may reference those regions.
381 let method_sig = self.replace_late_bound_regions_with_fresh_var(&sig);
382 debug!("late-bound lifetimes from method instantiated, method_sig={:?}",
385 let method_sig = method_sig.subst(self.tcx, all_substs);
386 debug!("type scheme substituted, method_sig={:?}", method_sig);
388 (method_sig, method_predicates)
391 fn add_obligations(&mut self,
393 all_substs: &Substs<'tcx>,
394 method_predicates: &ty::InstantiatedPredicates<'tcx>) {
395 debug!("add_obligations: fty={:?} all_substs={:?} method_predicates={:?}",
400 self.add_obligations_for_parameters(traits::ObligationCause::misc(self.span, self.body_id),
403 // this is a projection from a trait reference, so we have to
404 // make sure that the trait reference inputs are well-formed.
405 self.add_wf_bounds(all_substs, self.call_expr);
407 // the function type must also be well-formed (this is not
408 // implied by the substs being well-formed because of inherent
409 // impls and late-bound regions - see issue #28609).
410 self.register_wf_obligation(fty, self.span, traits::MiscObligation);
413 ///////////////////////////////////////////////////////////////////////////
416 /// When we select a method with a mutable autoref, we have to go convert any
417 /// auto-derefs, indices, etc from `Deref` and `Index` into `DerefMut` and `IndexMut`
419 fn convert_lvalue_derefs_to_mutable(&self) {
420 // Gather up expressions we want to munge.
421 let mut exprs = Vec::new();
422 exprs.push(self.self_expr);
424 let last = exprs[exprs.len() - 1];
426 hir::ExprField(ref expr, _) |
427 hir::ExprTupField(ref expr, _) |
428 hir::ExprIndex(ref expr, _) |
429 hir::ExprUnary(hir::UnDeref, ref expr) => exprs.push(&expr),
434 debug!("convert_lvalue_derefs_to_mutable: exprs={:?}", exprs);
436 // Fix up autoderefs and derefs.
437 for (i, &expr) in exprs.iter().rev().enumerate() {
438 debug!("convert_lvalue_derefs_to_mutable: i={} expr={:?}", i, expr);
440 // Fix up the autoderefs. Autorefs can only occur immediately preceding
441 // overloaded lvalue ops, and will be fixed by them in order to get
442 // the correct region.
443 let mut source = self.node_ty(expr.hir_id);
444 // Do not mutate adjustments in place, but rather take them,
445 // and replace them after mutating them, to avoid having the
446 // tables borrowed during (`deref_mut`) method resolution.
447 let previous_adjustments = self.tables
450 .remove(expr.hir_id);
451 if let Some(mut adjustments) = previous_adjustments {
452 let pref = LvaluePreference::PreferMutLvalue;
453 for adjustment in &mut adjustments {
454 if let Adjust::Deref(Some(ref mut deref)) = adjustment.kind {
455 if let Some(ok) = self.try_overloaded_deref(expr.span, source, pref) {
456 let method = self.register_infer_ok_obligations(ok);
457 if let ty::TyRef(region, mt) = method.sig.output().sty {
458 *deref = OverloadedDeref {
465 source = adjustment.target;
467 self.tables.borrow_mut().adjustments_mut().insert(expr.hir_id, adjustments);
471 hir::ExprIndex(ref base_expr, ref index_expr) => {
472 let index_expr_ty = self.node_ty(index_expr.hir_id);
473 self.convert_lvalue_op_to_mutable(
474 LvalueOp::Index, expr, base_expr, &[index_expr_ty]);
476 hir::ExprUnary(hir::UnDeref, ref base_expr) => {
477 self.convert_lvalue_op_to_mutable(
478 LvalueOp::Deref, expr, base_expr, &[]);
485 fn convert_lvalue_op_to_mutable(&self,
488 base_expr: &hir::Expr,
489 arg_tys: &[Ty<'tcx>])
491 debug!("convert_lvalue_op_to_mutable({:?}, {:?}, {:?}, {:?})",
492 op, expr, base_expr, arg_tys);
493 if !self.tables.borrow().is_method_call(expr) {
494 debug!("convert_lvalue_op_to_mutable - builtin, nothing to do");
498 let base_ty = self.tables.borrow().expr_adjustments(base_expr).last()
499 .map_or_else(|| self.node_ty(expr.hir_id), |adj| adj.target);
500 let base_ty = self.resolve_type_vars_if_possible(&base_ty);
502 // Need to deref because overloaded lvalue ops take self by-reference.
503 let base_ty = base_ty.builtin_deref(false, NoPreference)
504 .expect("lvalue op takes something that is not a ref")
507 let method = self.try_overloaded_lvalue_op(
508 expr.span, base_ty, arg_tys, PreferMutLvalue, op);
509 let method = match method {
510 Some(ok) => self.register_infer_ok_obligations(ok),
511 None => return self.tcx.sess.delay_span_bug(expr.span, "re-trying op failed")
513 debug!("convert_lvalue_op_to_mutable: method={:?}", method);
514 self.write_method_call(expr.hir_id, method);
516 let (region, mutbl) = if let ty::TyRef(r, mt) = method.sig.inputs()[0].sty {
519 span_bug!(expr.span, "input to lvalue op is not a ref?");
522 // Convert the autoref in the base expr to mutable with the correct
523 // region and mutability.
524 let base_expr_ty = self.node_ty(base_expr.hir_id);
525 if let Some(adjustments) = self.tables
528 .get_mut(base_expr.hir_id) {
529 let mut source = base_expr_ty;
530 for adjustment in &mut adjustments[..] {
531 if let Adjust::Borrow(AutoBorrow::Ref(..)) = adjustment.kind {
532 debug!("convert_lvalue_op_to_mutable: converting autoref {:?}", adjustment);
533 adjustment.kind = Adjust::Borrow(AutoBorrow::Ref(region, mutbl));
534 adjustment.target = self.tcx.mk_ref(region, ty::TypeAndMut {
539 source = adjustment.target;
542 // If we have an autoref followed by unsizing at the end, fix the unsize target.
543 match adjustments[..] {
544 [.., Adjustment { kind: Adjust::Borrow(AutoBorrow::Ref(..)), .. },
545 Adjustment { kind: Adjust::Unsize, ref mut target }] => {
546 *target = method.sig.inputs()[0];
553 ///////////////////////////////////////////////////////////////////////////
556 fn predicates_require_illegal_sized_bound(&self,
557 predicates: &ty::InstantiatedPredicates<'tcx>)
559 let sized_def_id = match self.tcx.lang_items().sized_trait() {
560 Some(def_id) => def_id,
561 None => return false,
564 traits::elaborate_predicates(self.tcx, predicates.predicates.clone())
565 .filter_map(|predicate| {
567 ty::Predicate::Trait(trait_pred) if trait_pred.def_id() == sized_def_id =>
573 match trait_pred.0.self_ty().sty {
574 ty::TyDynamic(..) => true,
580 fn enforce_illegal_method_limitations(&self, pick: &probe::Pick) {
581 // Disallow calls to the method `drop` defined in the `Drop` trait.
582 match pick.item.container {
583 ty::TraitContainer(trait_def_id) => {
584 callee::check_legal_trait_for_method_call(self.tcx, self.span, trait_def_id)
586 ty::ImplContainer(..) => {}
591 source_trait_ref: ty::PolyTraitRef<'tcx>,
592 target_trait_def_id: DefId)
593 -> ty::PolyTraitRef<'tcx> {
594 let upcast_trait_refs = self.tcx
595 .upcast_choices(source_trait_ref.clone(), target_trait_def_id);
597 // must be exactly one trait ref or we'd get an ambig error etc
598 if upcast_trait_refs.len() != 1 {
600 "cannot uniquely upcast `{:?}` to `{:?}`: `{:?}`",
606 upcast_trait_refs.into_iter().next().unwrap()
609 fn replace_late_bound_regions_with_fresh_var<T>(&self, value: &ty::Binder<T>) -> T
610 where T: TypeFoldable<'tcx>
613 .replace_late_bound_regions_with_fresh_var(self.span, infer::FnCall, value)