]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/check/method/confirm.rs
Fix invalid associated type rendering in rustdoc
[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;
12
13 use check::{FnCtxt, 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};
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                           -> ty::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                -> 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);
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_ty, 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_ty.fn_sig().input(0).skip_binder());
95
96         // Add any trait/regions obligations specified on the method's type parameters.
97         self.add_obligations(method_ty, all_substs, &method_predicates);
98
99         // Create the final `MethodCallee`.
100         let callee = ty::MethodCallee {
101             def_id: pick.item.def_id,
102             ty: method_ty,
103             substs: all_substs,
104         };
105
106         if let Some(hir::MutMutable) = pick.autoref {
107             self.convert_lvalue_derefs_to_mutable();
108         }
109
110         callee
111     }
112
113     ///////////////////////////////////////////////////////////////////////////
114     // ADJUSTMENTS
115
116     fn adjust_self_ty(&mut self,
117                       unadjusted_self_ty: Ty<'tcx>,
118                       pick: &probe::Pick<'tcx>)
119                       -> Ty<'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))
123         } else {
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());
129             None
130         };
131
132
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);
138
139         autoderef.unambiguous_final_ty();
140         autoderef.finalize(LvaluePreference::NoPreference, &[self.self_expr]);
141
142         let target = pick.unsize.unwrap_or(autoderefd_ty);
143         let target = target.adjust_for_autoref(self.tcx, autoref);
144
145         // Write out the final adjustment.
146         self.write_adjustment(self.self_expr.id, Adjustment {
147             kind: Adjust::DerefRef {
148                 autoderefs: pick.autoderefs,
149                 autoref: autoref,
150                 unsize: pick.unsize.is_some(),
151             },
152             target: target
153         });
154
155         target
156     }
157
158     ///////////////////////////////////////////////////////////////////////////
159     //
160
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.
164     ///
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,
168                              self_ty: Ty<'tcx>,
169                              pick: &probe::Pick<'tcx>)
170                              -> &'tcx Substs<'tcx> {
171         match pick.kind {
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",
176                         impl_def_id);
177                 self.impl_self_ty(self.span, impl_def_id).substs
178             }
179
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
192                     // "object safe".
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,
199                            upcast_trait_ref,
200                            trait_def_id);
201                     upcast_trait_ref.substs
202                 })
203             }
204
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:
208                 //
209                 //     impl<A,B,C> Trait<A,B> for Foo<C> { ... }
210                 //
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);
216                 let impl_trait_ref =
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
221             }
222
223             probe::TraitPick => {
224                 let trait_def_id = pick.item.container.id();
225
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)
232             }
233
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
238             }
239         }
240     }
241
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>,
244                        Ty<'tcx>,
245                        ty::PolyExistentialTraitRef<'tcx>)
246                        -> R
247     {
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>`
251         // etc).
252
253         // FIXME: this feels, like, super dubious
254         self.fcx
255             .autoderef(self.span, self_ty)
256             .filter_map(|(ty, _)| {
257                 match ty.sty {
258                     ty::TyDynamic(ref data, ..) => data.principal().map(|p| closure(self, ty, p)),
259                     _ => None,
260                 }
261             })
262             .next()
263             .unwrap_or_else(|| {
264                 span_bug!(self.span,
265                           "self-type `{}` for ObjectPick never dereferenced to an object",
266                           self_ty)
267             })
268     }
269
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
277         // variables.
278         let num_supplied_types = supplied_method_types.len();
279         let method_generics = self.tcx.item_generics(pick.item.def_id);
280         let num_method_types = method_generics.types.len();
281
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,
285                                  self.span,
286                                  E0035,
287                                  "does not take type parameters")
288                     .span_label(self.span, &"called with unneeded type parameters")
289                     .emit();
290             } else {
291                 struct_span_err!(self.tcx.sess,
292                                  self.span,
293                                  E0036,
294                                  "incorrect number of type parameters given for this method: \
295                                   expected {}, found {}",
296                                  num_method_types,
297                                  num_supplied_types)
298                     .span_label(self.span,
299                                 &format!("Passed {} type argument{}, expected {}",
300                                          num_supplied_types,
301                                          if num_supplied_types != 1 { "s" } else { "" },
302                                          num_method_types))
303                     .emit();
304             }
305             supplied_method_types = vec![self.tcx.types.err; num_method_types];
306         }
307
308         // Create subst for early-bound lifetime parameters, combining
309         // parameters from the type and those from the method.
310         //
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() {
316                 substs.region_at(i)
317             } else {
318                 self.region_var_for_def(self.span, def)
319             }
320         }, |def, cur_substs| {
321             let i = def.index as usize;
322             if i < substs.len() {
323                 substs.type_at(i)
324             } else if supplied_method_types.is_empty() {
325                 self.type_var_for_def(self.span, def, cur_substs)
326             } else {
327                 supplied_method_types[i - supplied_start]
328             }
329         })
330     }
331
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);
336             }
337             Err(_) => {
338                 span_bug!(self.span,
339                           "{} was a subtype of {} but now is not?",
340                           self_ty,
341                           method_self_ty);
342             }
343         }
344     }
345
346     ///////////////////////////////////////////////////////////////////////////
347     //
348
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={:?})",
354                pick,
355                all_substs);
356
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.item_predicates(def_id)
362                                     .instantiate(self.tcx, all_substs);
363         let method_predicates = self.normalize_associated_types_in(self.span,
364                                                                    &method_predicates);
365
366         debug!("method_predicates after subst = {:?}", method_predicates);
367
368         let sig = self.tcx.item_type(def_id).fn_sig();
369
370         // Instantiate late-bound regions and substitute the trait
371         // parameters into the method type to get the actual method type.
372         //
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={:?}",
378                method_sig);
379
380         let method_sig = self.instantiate_type_scheme(self.span, all_substs, &method_sig);
381         debug!("type scheme substituted, method_sig={:?}", method_sig);
382
383         (self.tcx.mk_fn_def(def_id, all_substs, ty::Binder(method_sig)),
384          method_predicates)
385     }
386
387     fn add_obligations(&mut self,
388                        fty: Ty<'tcx>,
389                        all_substs: &Substs<'tcx>,
390                        method_predicates: &ty::InstantiatedPredicates<'tcx>) {
391         debug!("add_obligations: fty={:?} all_substs={:?} method_predicates={:?}",
392                fty,
393                all_substs,
394                method_predicates);
395
396         self.add_obligations_for_parameters(traits::ObligationCause::misc(self.span, self.body_id),
397                                             method_predicates);
398
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);
402
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);
407     }
408
409     ///////////////////////////////////////////////////////////////////////////
410     // RECONCILIATION
411
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`
414     /// respectively.
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);
419         loop {
420             let last = exprs[exprs.len() - 1];
421             match last.node {
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),
426                 _ => break,
427             }
428         }
429
430         debug!("convert_lvalue_derefs_to_mutable: exprs={:?}", exprs);
431
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);
435
436             // Count autoderefs.
437             let adjustment = self.tables.borrow().adjustments.get(&expr.id).cloned();
438             match adjustment {
439                 Some(Adjustment { kind: Adjust::DerefRef { autoderefs, .. }, .. }) => {
440                     if autoderefs > 0 {
441                         let mut autoderef = self.autoderef(expr.span, self.node_ty(expr.id));
442                         autoderef.nth(autoderefs).unwrap_or_else(|| {
443                             span_bug!(expr.span,
444                                       "expr was deref-able {} times but now isn't?",
445                                       autoderefs);
446                         });
447                         autoderef.finalize(PreferMutLvalue, &[expr]);
448                     }
449                 }
450                 Some(_) | None => {}
451             }
452
453             // Don't retry the first one or we might infinite loop!
454             if i == 0 {
455                 continue;
456             }
457             match expr.node {
458                 hir::ExprIndex(ref base_expr, ref index_expr) => {
459                     // If this is an overloaded index, the
460                     // adjustment will include an extra layer of
461                     // autoref because the method is an &self/&mut
462                     // self method. We have to peel it off to get
463                     // the raw adjustment that `try_index_step`
464                     // expects. This is annoying and horrible. We
465                     // ought to recode this routine so it doesn't
466                     // (ab)use the normal type checking paths.
467                     let adj = self.tables.borrow().adjustments.get(&base_expr.id).cloned();
468                     let (autoderefs, unsize, adjusted_base_ty) = match adj {
469                         Some(Adjustment {
470                             kind: Adjust::DerefRef { autoderefs, autoref, unsize },
471                             target
472                         }) => {
473                             match autoref {
474                                 None => {
475                                     assert!(!unsize);
476                                 }
477                                 Some(AutoBorrow::Ref(..)) => {}
478                                 Some(_) => {
479                                     span_bug!(base_expr.span,
480                                               "unexpected adjustment autoref {:?}",
481                                               adj);
482                                 }
483                             }
484
485                             (autoderefs, unsize, if unsize {
486                                 target.builtin_deref(false, NoPreference)
487                                       .expect("fixup: AutoBorrow::Ref is not &T")
488                                       .ty
489                             } else {
490                                 let ty = self.node_ty(base_expr.id);
491                                 let mut ty = self.shallow_resolve(ty);
492                                 let mut method_type = |method_call: ty::MethodCall| {
493                                     self.tables.borrow().method_map.get(&method_call).map(|m| {
494                                         self.resolve_type_vars_if_possible(&m.ty)
495                                     })
496                                 };
497
498                                 if !ty.references_error() {
499                                     for i in 0..autoderefs {
500                                         ty = ty.adjust_for_autoderef(self.tcx,
501                                                                      base_expr.id,
502                                                                      base_expr.span,
503                                                                      i as u32,
504                                                                      &mut method_type);
505                                     }
506                                 }
507
508                                 ty
509                             })
510                         }
511                         None => (0, false, self.node_ty(base_expr.id)),
512                         Some(_) => {
513                             span_bug!(base_expr.span, "unexpected adjustment type");
514                         }
515                     };
516
517                     let index_expr_ty = self.node_ty(index_expr.id);
518
519                     let result = self.try_index_step(ty::MethodCall::expr(expr.id),
520                                                      expr,
521                                                      &base_expr,
522                                                      adjusted_base_ty,
523                                                      autoderefs,
524                                                      unsize,
525                                                      PreferMutLvalue,
526                                                      index_expr_ty);
527
528                     if let Some((input_ty, return_ty)) = result {
529                         self.demand_suptype(index_expr.span, input_ty, index_expr_ty);
530
531                         let expr_ty = self.node_ty(expr.id);
532                         self.demand_suptype(expr.span, expr_ty, return_ty);
533                     }
534                 }
535                 hir::ExprUnary(hir::UnDeref, ref base_expr) => {
536                     // if this is an overloaded deref, then re-evaluate with
537                     // a preference for mut
538                     let method_call = ty::MethodCall::expr(expr.id);
539                     if self.tables.borrow().method_map.contains_key(&method_call) {
540                         let method = self.try_overloaded_deref(expr.span,
541                                                                Some(&base_expr),
542                                                                self.node_ty(base_expr.id),
543                                                                PreferMutLvalue);
544                         let method = method.expect("re-trying deref failed");
545                         self.tables.borrow_mut().method_map.insert(method_call, method);
546                     }
547                 }
548                 _ => {}
549             }
550         }
551     }
552
553     ///////////////////////////////////////////////////////////////////////////
554     // MISCELLANY
555
556     fn enforce_illegal_method_limitations(&self, pick: &probe::Pick) {
557         // Disallow calls to the method `drop` defined in the `Drop` trait.
558         match pick.item.container {
559             ty::TraitContainer(trait_def_id) => {
560                 callee::check_legal_trait_for_method_call(self.tcx, self.span, trait_def_id)
561             }
562             ty::ImplContainer(..) => {}
563         }
564     }
565
566     fn upcast(&mut self,
567               source_trait_ref: ty::PolyTraitRef<'tcx>,
568               target_trait_def_id: DefId)
569               -> ty::PolyTraitRef<'tcx> {
570         let upcast_trait_refs = self.tcx
571             .upcast_choices(source_trait_ref.clone(), target_trait_def_id);
572
573         // must be exactly one trait ref or we'd get an ambig error etc
574         if upcast_trait_refs.len() != 1 {
575             span_bug!(self.span,
576                       "cannot uniquely upcast `{:?}` to `{:?}`: `{:?}`",
577                       source_trait_ref,
578                       target_trait_def_id,
579                       upcast_trait_refs);
580         }
581
582         upcast_trait_refs.into_iter().next().unwrap()
583     }
584
585     fn replace_late_bound_regions_with_fresh_var<T>(&self, value: &ty::Binder<T>) -> T
586         where T: TypeFoldable<'tcx>
587     {
588         self.fcx
589             .replace_late_bound_regions_with_fresh_var(self.span, infer::FnCall, value)
590             .0
591     }
592 }