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