]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/check/method/confirm.rs
Rollup merge of #43287 - kennytm:travis-ci-adjustments, r=alexcrichton
[rust.git] / src / librustc_typeck / check / method / confirm.rs
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.
4 //
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.
10
11 use super::{probe, MethodCallee};
12
13 use astconv::AstConv;
14 use check::{FnCtxt, LvalueOp, callee};
15 use hir::def_id::DefId;
16 use rustc::ty::subst::Substs;
17 use rustc::traits;
18 use rustc::ty::{self, LvaluePreference, NoPreference, PreferMutLvalue, Ty};
19 use rustc::ty::adjustment::{Adjustment, Adjust, AutoBorrow, OverloadedDeref};
20 use rustc::ty::fold::TypeFoldable;
21 use rustc::infer::{self, InferOk};
22 use syntax_pos::Span;
23 use rustc::hir;
24
25 use std::ops::Deref;
26
27 struct ConfirmContext<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
28     fcx: &'a FnCtxt<'a, 'gcx, 'tcx>,
29     span: Span,
30     self_expr: &'gcx hir::Expr,
31     call_expr: &'gcx hir::Expr,
32 }
33
34 impl<'a, 'gcx, 'tcx> Deref for ConfirmContext<'a, 'gcx, 'tcx> {
35     type Target = FnCtxt<'a, 'gcx, 'tcx>;
36     fn deref(&self) -> &Self::Target {
37         &self.fcx
38     }
39 }
40
41 impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
42     pub fn confirm_method(&self,
43                           span: Span,
44                           self_expr: &'gcx hir::Expr,
45                           call_expr: &'gcx hir::Expr,
46                           unadjusted_self_ty: Ty<'tcx>,
47                           pick: probe::Pick<'tcx>,
48                           segment: &hir::PathSegment)
49                           -> MethodCallee<'tcx> {
50         debug!("confirm(unadjusted_self_ty={:?}, pick={:?}, generic_args={:?})",
51                unadjusted_self_ty,
52                pick,
53                segment.parameters);
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, 'gcx, 'tcx> ConfirmContext<'a, 'gcx, 'tcx> {
61     fn new(fcx: &'a FnCtxt<'a, 'gcx, 'tcx>,
62            span: Span,
63            self_expr: &'gcx hir::Expr,
64            call_expr: &'gcx hir::Expr)
65            -> ConfirmContext<'a, 'gcx, 'tcx> {
66         ConfirmContext {
67             fcx: fcx,
68             span: span,
69             self_expr: self_expr,
70             call_expr: call_expr,
71         }
72     }
73
74     fn confirm(&mut self,
75                unadjusted_self_ty: Ty<'tcx>,
76                pick: probe::Pick<'tcx>,
77                segment: &hir::PathSegment)
78                -> MethodCallee<'tcx> {
79         // Adjust the self expression the user provided and obtain the adjusted type.
80         let self_ty = self.adjust_self_ty(unadjusted_self_ty, &pick);
81
82         // Make sure nobody calls `drop()` explicitly.
83         self.enforce_illegal_method_limitations(&pick);
84
85         // Create substitutions for the method's type parameters.
86         let rcvr_substs = self.fresh_receiver_substs(self_ty, &pick);
87         let all_substs = self.instantiate_method_substs(&pick, segment, rcvr_substs);
88
89         debug!("all_substs={:?}", all_substs);
90
91         // Create the final signature for the method, replacing late-bound regions.
92         let (method_sig, method_predicates) = self.instantiate_method_sig(&pick, all_substs);
93
94         // Unify the (adjusted) self type with what the method expects.
95         self.unify_receivers(self_ty, method_sig.inputs()[0]);
96
97         // Add any trait/regions obligations specified on the method's type parameters.
98         let method_ty = self.tcx.mk_fn_ptr(ty::Binder(method_sig));
99         self.add_obligations(method_ty, all_substs, &method_predicates);
100
101         // Create the final `MethodCallee`.
102         let callee = MethodCallee {
103             def_id: pick.item.def_id,
104             substs: all_substs,
105             sig: method_sig,
106         };
107
108         if let Some(hir::MutMutable) = pick.autoref {
109             self.convert_lvalue_derefs_to_mutable();
110         }
111
112         callee
113     }
114
115     ///////////////////////////////////////////////////////////////////////////
116     // ADJUSTMENTS
117
118     fn adjust_self_ty(&mut self,
119                       unadjusted_self_ty: Ty<'tcx>,
120                       pick: &probe::Pick<'tcx>)
121                       -> Ty<'tcx> {
122         // Commit the autoderefs by calling `autoderef` again, but this
123         // time writing the results into the various tables.
124         let mut autoderef = self.autoderef(self.span, unadjusted_self_ty);
125         let (_, n) = autoderef.nth(pick.autoderefs).unwrap();
126         assert_eq!(n, pick.autoderefs);
127
128         let mut adjustments = autoderef.adjust_steps(LvaluePreference::NoPreference);
129
130         let mut target = autoderef.unambiguous_final_ty();
131
132         if let Some(mutbl) = pick.autoref {
133             let region = self.next_region_var(infer::Autoref(self.span));
134             target = self.tcx.mk_ref(region, ty::TypeAndMut {
135                 mutbl,
136                 ty: target
137             });
138             adjustments.push(Adjustment {
139                 kind: Adjust::Borrow(AutoBorrow::Ref(region, mutbl)),
140                 target
141             });
142
143             if let Some(unsize_target) = pick.unsize {
144                 target = self.tcx.mk_ref(region, ty::TypeAndMut {
145                     mutbl,
146                     ty: unsize_target
147                 });
148                 adjustments.push(Adjustment {
149                     kind: Adjust::Unsize,
150                     target
151                 });
152             }
153         } else {
154             // No unsizing should be performed without autoref (at
155             // least during method dispach). This is because we
156             // currently only unsize `[T;N]` to `[T]`, and naturally
157             // that must occur being a reference.
158             assert!(pick.unsize.is_none());
159         }
160
161         autoderef.finalize();
162
163         // Write out the final adjustments.
164         self.apply_adjustments(self.self_expr, adjustments);
165
166         target
167     }
168
169     ///////////////////////////////////////////////////////////////////////////
170     //
171
172     /// Returns a set of substitutions for the method *receiver* where all type and region
173     /// parameters are instantiated with fresh variables. This substitution does not include any
174     /// parameters declared on the method itself.
175     ///
176     /// Note that this substitution may include late-bound regions from the impl level. If so,
177     /// these are instantiated later in the `instantiate_method_sig` routine.
178     fn fresh_receiver_substs(&mut self,
179                              self_ty: Ty<'tcx>,
180                              pick: &probe::Pick<'tcx>)
181                              -> &'tcx Substs<'tcx> {
182         match pick.kind {
183             probe::InherentImplPick => {
184                 let impl_def_id = pick.item.container.id();
185                 assert!(self.tcx.impl_trait_ref(impl_def_id).is_none(),
186                         "impl {:?} is not an inherent impl",
187                         impl_def_id);
188                 self.impl_self_ty(self.span, impl_def_id).substs
189             }
190
191             probe::ObjectPick => {
192                 let trait_def_id = pick.item.container.id();
193                 self.extract_existential_trait_ref(self_ty, |this, object_ty, principal| {
194                     // The object data has no entry for the Self
195                     // Type. For the purposes of this method call, we
196                     // substitute the object type itself. This
197                     // wouldn't be a sound substitution in all cases,
198                     // since each instance of the object type is a
199                     // different existential and hence could match
200                     // distinct types (e.g., if `Self` appeared as an
201                     // argument type), but those cases have already
202                     // been ruled out when we deemed the trait to be
203                     // "object safe".
204                     let original_poly_trait_ref = principal.with_self_ty(this.tcx, object_ty);
205                     let upcast_poly_trait_ref = this.upcast(original_poly_trait_ref, trait_def_id);
206                     let upcast_trait_ref =
207                         this.replace_late_bound_regions_with_fresh_var(&upcast_poly_trait_ref);
208                     debug!("original_poly_trait_ref={:?} upcast_trait_ref={:?} target_trait={:?}",
209                            original_poly_trait_ref,
210                            upcast_trait_ref,
211                            trait_def_id);
212                     upcast_trait_ref.substs
213                 })
214             }
215
216             probe::ExtensionImplPick(impl_def_id) => {
217                 // The method being invoked is the method as defined on the trait,
218                 // so return the substitutions from the trait. Consider:
219                 //
220                 //     impl<A,B,C> Trait<A,B> for Foo<C> { ... }
221                 //
222                 // If we instantiate A, B, and C with $A, $B, and $C
223                 // respectively, then we want to return the type
224                 // parameters from the trait ([$A,$B]), not those from
225                 // the impl ([$A,$B,$C]) not the receiver type ([$C]).
226                 let impl_polytype = self.impl_self_ty(self.span, impl_def_id);
227                 let impl_trait_ref =
228                     self.instantiate_type_scheme(self.span,
229                                                  impl_polytype.substs,
230                                                  &self.tcx.impl_trait_ref(impl_def_id).unwrap());
231                 impl_trait_ref.substs
232             }
233
234             probe::TraitPick => {
235                 let trait_def_id = pick.item.container.id();
236
237                 // Make a trait reference `$0 : Trait<$1...$n>`
238                 // consisting entirely of type variables. Later on in
239                 // the process we will unify the transformed-self-type
240                 // of the method with the actual type in order to
241                 // unify some of these variables.
242                 self.fresh_substs_for_item(self.span, trait_def_id)
243             }
244
245             probe::WhereClausePick(ref poly_trait_ref) => {
246                 // Where clauses can have bound regions in them. We need to instantiate
247                 // those to convert from a poly-trait-ref to a trait-ref.
248                 self.replace_late_bound_regions_with_fresh_var(&poly_trait_ref).substs
249             }
250         }
251     }
252
253     fn extract_existential_trait_ref<R, F>(&mut self, self_ty: Ty<'tcx>, mut closure: F) -> R
254         where F: FnMut(&mut ConfirmContext<'a, 'gcx, 'tcx>,
255                        Ty<'tcx>,
256                        ty::PolyExistentialTraitRef<'tcx>)
257                        -> R
258     {
259         // If we specified that this is an object method, then the
260         // self-type ought to be something that can be dereferenced to
261         // yield an object-type (e.g., `&Object` or `Box<Object>`
262         // etc).
263
264         // FIXME: this feels, like, super dubious
265         self.fcx
266             .autoderef(self.span, self_ty)
267             .filter_map(|(ty, _)| {
268                 match ty.sty {
269                     ty::TyDynamic(ref data, ..) => data.principal().map(|p| closure(self, ty, p)),
270                     _ => None,
271                 }
272             })
273             .next()
274             .unwrap_or_else(|| {
275                 span_bug!(self.span,
276                           "self-type `{}` for ObjectPick never dereferenced to an object",
277                           self_ty)
278             })
279     }
280
281     fn instantiate_method_substs(&mut self,
282                                  pick: &probe::Pick<'tcx>,
283                                  segment: &hir::PathSegment,
284                                  parent_substs: &Substs<'tcx>)
285                                  -> &'tcx Substs<'tcx> {
286         // Determine the values for the generic parameters of the method.
287         // If they were not explicitly supplied, just construct fresh
288         // variables.
289         let method_generics = self.tcx.generics_of(pick.item.def_id);
290         let mut fn_segment = Some((segment, method_generics));
291         self.fcx.check_path_parameter_count(self.span, &mut fn_segment, true);
292
293         // Create subst for early-bound lifetime parameters, combining
294         // parameters from the type and those from the method.
295         let (supplied_types, supplied_lifetimes) = match segment.parameters {
296             hir::AngleBracketedParameters(ref data) => (&data.types, &data.lifetimes),
297             _ => bug!("unexpected generic arguments: {:?}", segment.parameters),
298         };
299         assert_eq!(method_generics.parent_count(), parent_substs.len());
300         Substs::for_item(self.tcx, pick.item.def_id, |def, _| {
301             let i = def.index as usize;
302             if i < parent_substs.len() {
303                 parent_substs.region_at(i)
304             } else if let Some(lifetime) =
305                     supplied_lifetimes.get(i - parent_substs.len()) {
306                 AstConv::ast_region_to_region(self.fcx, lifetime, Some(def))
307             } else {
308                 self.region_var_for_def(self.span, def)
309             }
310         }, |def, cur_substs| {
311             let i = def.index as usize;
312             if i < parent_substs.len() {
313                 parent_substs.type_at(i)
314             } else if let Some(ast_ty) =
315                     supplied_types.get(i - parent_substs.len() - method_generics.regions.len()) {
316                 self.to_ty(ast_ty)
317             } else {
318                 self.type_var_for_def(self.span, def, cur_substs)
319             }
320         })
321     }
322
323     fn unify_receivers(&mut self, self_ty: Ty<'tcx>, method_self_ty: Ty<'tcx>) {
324         match self.at(&self.misc(self.span), self.param_env).sup(method_self_ty, self_ty) {
325             Ok(InferOk { obligations, value: () }) => {
326                 self.register_predicates(obligations);
327             }
328             Err(_) => {
329                 span_bug!(self.span,
330                           "{} was a subtype of {} but now is not?",
331                           self_ty,
332                           method_self_ty);
333             }
334         }
335     }
336
337     ///////////////////////////////////////////////////////////////////////////
338     //
339
340     fn instantiate_method_sig(&mut self,
341                               pick: &probe::Pick<'tcx>,
342                               all_substs: &'tcx Substs<'tcx>)
343                               -> (ty::FnSig<'tcx>, ty::InstantiatedPredicates<'tcx>) {
344         debug!("instantiate_method_sig(pick={:?}, all_substs={:?})",
345                pick,
346                all_substs);
347
348         // Instantiate the bounds on the method with the
349         // type/early-bound-regions substitutions performed. There can
350         // be no late-bound regions appearing here.
351         let def_id = pick.item.def_id;
352         let method_predicates = self.tcx.predicates_of(def_id)
353                                     .instantiate(self.tcx, all_substs);
354         let method_predicates = self.normalize_associated_types_in(self.span,
355                                                                    &method_predicates);
356
357         debug!("method_predicates after subst = {:?}", method_predicates);
358
359         let sig = self.tcx.fn_sig(def_id);
360
361         // Instantiate late-bound regions and substitute the trait
362         // parameters into the method type to get the actual method type.
363         //
364         // NB: Instantiate late-bound regions first so that
365         // `instantiate_type_scheme` can normalize associated types that
366         // may reference those regions.
367         let method_sig = self.replace_late_bound_regions_with_fresh_var(&sig);
368         debug!("late-bound lifetimes from method instantiated, method_sig={:?}",
369                method_sig);
370
371         let method_sig = self.instantiate_type_scheme(self.span, all_substs, &method_sig);
372         debug!("type scheme substituted, method_sig={:?}", method_sig);
373
374         (method_sig, method_predicates)
375     }
376
377     fn add_obligations(&mut self,
378                        fty: Ty<'tcx>,
379                        all_substs: &Substs<'tcx>,
380                        method_predicates: &ty::InstantiatedPredicates<'tcx>) {
381         debug!("add_obligations: fty={:?} all_substs={:?} method_predicates={:?}",
382                fty,
383                all_substs,
384                method_predicates);
385
386         self.add_obligations_for_parameters(traits::ObligationCause::misc(self.span, self.body_id),
387                                             method_predicates);
388
389         // this is a projection from a trait reference, so we have to
390         // make sure that the trait reference inputs are well-formed.
391         self.add_wf_bounds(all_substs, self.call_expr);
392
393         // the function type must also be well-formed (this is not
394         // implied by the substs being well-formed because of inherent
395         // impls and late-bound regions - see issue #28609).
396         self.register_wf_obligation(fty, self.span, traits::MiscObligation);
397     }
398
399     ///////////////////////////////////////////////////////////////////////////
400     // RECONCILIATION
401
402     /// When we select a method with a mutable autoref, we have to go convert any
403     /// auto-derefs, indices, etc from `Deref` and `Index` into `DerefMut` and `IndexMut`
404     /// respectively.
405     fn convert_lvalue_derefs_to_mutable(&self) {
406         // Gather up expressions we want to munge.
407         let mut exprs = Vec::new();
408         exprs.push(self.self_expr);
409         loop {
410             let last = exprs[exprs.len() - 1];
411             match last.node {
412                 hir::ExprField(ref expr, _) |
413                 hir::ExprTupField(ref expr, _) |
414                 hir::ExprIndex(ref expr, _) |
415                 hir::ExprUnary(hir::UnDeref, ref expr) => exprs.push(&expr),
416                 _ => break,
417             }
418         }
419
420         debug!("convert_lvalue_derefs_to_mutable: exprs={:?}", exprs);
421
422         // Fix up autoderefs and derefs.
423         for (i, &expr) in exprs.iter().rev().enumerate() {
424             debug!("convert_lvalue_derefs_to_mutable: i={} expr={:?}", i, expr);
425
426             // Fix up the autoderefs. Autorefs can only occur immediately preceding
427             // overloaded lvalue ops, and will be fixed by them in order to get
428             // the correct region.
429             let mut source = self.node_ty(expr.id);
430             // Do not mutate adjustments in place, but rather take them,
431             // and replace them after mutating them, to avoid having the
432             // tables borrowed during (`deref_mut`) method resolution.
433             let previous_adjustments = self.tables.borrow_mut().adjustments.remove(&expr.id);
434             if let Some(mut adjustments) = previous_adjustments {
435                 let pref = LvaluePreference::PreferMutLvalue;
436                 for adjustment in &mut adjustments {
437                     if let Adjust::Deref(Some(ref mut deref)) = adjustment.kind {
438                         if let Some(ok) = self.try_overloaded_deref(expr.span, source, pref) {
439                             let method = self.register_infer_ok_obligations(ok);
440                             if let ty::TyRef(region, mt) = method.sig.output().sty {
441                                 *deref = OverloadedDeref {
442                                     region,
443                                     mutbl: mt.mutbl
444                                 };
445                             }
446                         }
447                     }
448                     source = adjustment.target;
449                 }
450                 self.tables.borrow_mut().adjustments.insert(expr.id, adjustments);
451             }
452
453             match expr.node {
454                 hir::ExprIndex(ref base_expr, ref index_expr) => {
455                     let index_expr_ty = self.node_ty(index_expr.id);
456                     self.convert_lvalue_op_to_mutable(
457                         LvalueOp::Index, expr, base_expr, &[index_expr_ty]);
458                 }
459                 hir::ExprUnary(hir::UnDeref, ref base_expr) => {
460                     self.convert_lvalue_op_to_mutable(
461                         LvalueOp::Deref, expr, base_expr, &[]);
462                 }
463                 _ => {}
464             }
465         }
466     }
467
468     fn convert_lvalue_op_to_mutable(&self,
469                                     op: LvalueOp,
470                                     expr: &hir::Expr,
471                                     base_expr: &hir::Expr,
472                                     arg_tys: &[Ty<'tcx>])
473     {
474         debug!("convert_lvalue_op_to_mutable({:?}, {:?}, {:?}, {:?})",
475                op, expr, base_expr, arg_tys);
476         if !self.tables.borrow().is_method_call(expr) {
477             debug!("convert_lvalue_op_to_mutable - builtin, nothing to do");
478             return
479         }
480
481         let base_ty = self.tables.borrow().expr_adjustments(base_expr).last()
482             .map_or_else(|| self.node_ty(expr.id), |adj| adj.target);
483         let base_ty = self.resolve_type_vars_if_possible(&base_ty);
484
485         // Need to deref because overloaded lvalue ops take self by-reference.
486         let base_ty = base_ty.builtin_deref(false, NoPreference)
487             .expect("lvalue op takes something that is not a ref")
488             .ty;
489
490         let method = self.try_overloaded_lvalue_op(
491             expr.span, base_ty, arg_tys, PreferMutLvalue, op);
492         let method = match method {
493             Some(ok) => self.register_infer_ok_obligations(ok),
494             None => return self.tcx.sess.delay_span_bug(expr.span, "re-trying op failed")
495         };
496         debug!("convert_lvalue_op_to_mutable: method={:?}", method);
497         self.write_method_call(expr.id, method);
498
499         let (region, mutbl) = if let ty::TyRef(r, mt) = method.sig.inputs()[0].sty {
500             (r, mt.mutbl)
501         } else {
502             span_bug!(expr.span, "input to lvalue op is not a ref?");
503         };
504
505         // Convert the autoref in the base expr to mutable with the correct
506         // region and mutability.
507         let base_expr_ty = self.node_ty(base_expr.id);
508         if let Some(adjustments) = self.tables.borrow_mut().adjustments.get_mut(&base_expr.id) {
509             let mut source = base_expr_ty;
510             for adjustment in &mut adjustments[..] {
511                 if let Adjust::Borrow(AutoBorrow::Ref(..)) = adjustment.kind {
512                     debug!("convert_lvalue_op_to_mutable: converting autoref {:?}", adjustment);
513                     adjustment.kind = Adjust::Borrow(AutoBorrow::Ref(region, mutbl));
514                     adjustment.target = self.tcx.mk_ref(region, ty::TypeAndMut {
515                         ty: source,
516                         mutbl
517                     });
518                 }
519                 source = adjustment.target;
520             }
521
522             // If we have an autoref followed by unsizing at the end, fix the unsize target.
523             match adjustments[..] {
524                 [.., Adjustment { kind: Adjust::Borrow(AutoBorrow::Ref(..)), .. },
525                  Adjustment { kind: Adjust::Unsize, ref mut target }] => {
526                     *target = method.sig.inputs()[0];
527                 }
528                 _ => {}
529             }
530         }
531     }
532
533     ///////////////////////////////////////////////////////////////////////////
534     // MISCELLANY
535
536     fn enforce_illegal_method_limitations(&self, pick: &probe::Pick) {
537         // Disallow calls to the method `drop` defined in the `Drop` trait.
538         match pick.item.container {
539             ty::TraitContainer(trait_def_id) => {
540                 callee::check_legal_trait_for_method_call(self.tcx, self.span, trait_def_id)
541             }
542             ty::ImplContainer(..) => {}
543         }
544     }
545
546     fn upcast(&mut self,
547               source_trait_ref: ty::PolyTraitRef<'tcx>,
548               target_trait_def_id: DefId)
549               -> ty::PolyTraitRef<'tcx> {
550         let upcast_trait_refs = self.tcx
551             .upcast_choices(source_trait_ref.clone(), target_trait_def_id);
552
553         // must be exactly one trait ref or we'd get an ambig error etc
554         if upcast_trait_refs.len() != 1 {
555             span_bug!(self.span,
556                       "cannot uniquely upcast `{:?}` to `{:?}`: `{:?}`",
557                       source_trait_ref,
558                       target_trait_def_id,
559                       upcast_trait_refs);
560         }
561
562         upcast_trait_refs.into_iter().next().unwrap()
563     }
564
565     fn replace_late_bound_regions_with_fresh_var<T>(&self, value: &ty::Binder<T>) -> T
566         where T: TypeFoldable<'tcx>
567     {
568         self.fcx
569             .replace_late_bound_regions_with_fresh_var(self.span, infer::FnCall, value)
570             .0
571     }
572 }