]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/check/autoderef.rs
remove implementation detail from doc
[rust.git] / src / librustc_typeck / check / autoderef.rs
1 // Copyright 2016 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 astconv::AstConv;
12
13 use super::{FnCtxt, LvalueOp};
14 use super::method::MethodCallee;
15
16 use rustc::infer::InferOk;
17 use rustc::traits;
18 use rustc::ty::{self, Ty, TraitRef};
19 use rustc::ty::{ToPredicate, TypeFoldable};
20 use rustc::ty::{LvaluePreference, NoPreference};
21 use rustc::ty::adjustment::{Adjustment, Adjust, OverloadedDeref};
22
23 use syntax_pos::Span;
24 use syntax::symbol::Symbol;
25
26 use std::iter;
27
28 #[derive(Copy, Clone, Debug)]
29 enum AutoderefKind {
30     Builtin,
31     Overloaded,
32 }
33
34 pub struct Autoderef<'a, 'gcx: 'tcx, 'tcx: 'a> {
35     fcx: &'a FnCtxt<'a, 'gcx, 'tcx>,
36     steps: Vec<(Ty<'tcx>, AutoderefKind)>,
37     cur_ty: Ty<'tcx>,
38     obligations: Vec<traits::PredicateObligation<'tcx>>,
39     at_start: bool,
40     include_raw_pointers: bool,
41     span: Span,
42 }
43
44 impl<'a, 'gcx, 'tcx> Iterator for Autoderef<'a, 'gcx, 'tcx> {
45     type Item = (Ty<'tcx>, usize);
46
47     fn next(&mut self) -> Option<Self::Item> {
48         let tcx = self.fcx.tcx;
49
50         debug!("autoderef: steps={:?}, cur_ty={:?}",
51                self.steps,
52                self.cur_ty);
53         if self.at_start {
54             self.at_start = false;
55             debug!("autoderef stage #0 is {:?}", self.cur_ty);
56             return Some((self.cur_ty, 0));
57         }
58
59         if self.steps.len() == tcx.sess.recursion_limit.get() {
60             // We've reached the recursion limit, error gracefully.
61             let suggested_limit = tcx.sess.recursion_limit.get() * 2;
62             struct_span_err!(tcx.sess,
63                              self.span,
64                              E0055,
65                              "reached the recursion limit while auto-dereferencing {:?}",
66                              self.cur_ty)
67                 .span_label(self.span, "deref recursion limit reached")
68                 .help(&format!(
69                         "consider adding a `#[recursion_limit=\"{}\"]` attribute to your crate",
70                         suggested_limit))
71                 .emit();
72             return None;
73         }
74
75         if self.cur_ty.is_ty_var() {
76             return None;
77         }
78
79         // Otherwise, deref if type is derefable:
80         let (kind, new_ty) =
81             if let Some(mt) = self.cur_ty.builtin_deref(self.include_raw_pointers, NoPreference) {
82                 (AutoderefKind::Builtin, mt.ty)
83             } else {
84                 let ty = self.overloaded_deref_ty(self.cur_ty)?;
85                 (AutoderefKind::Overloaded, ty)
86             };
87
88         if new_ty.references_error() {
89             return None;
90         }
91
92         self.steps.push((self.cur_ty, kind));
93         debug!("autoderef stage #{:?} is {:?} from {:?}",
94                self.steps.len(),
95                new_ty,
96                (self.cur_ty, kind));
97         self.cur_ty = new_ty;
98
99         Some((self.cur_ty, self.steps.len()))
100     }
101 }
102
103 impl<'a, 'gcx, 'tcx> Autoderef<'a, 'gcx, 'tcx> {
104     fn overloaded_deref_ty(&mut self, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
105         debug!("overloaded_deref_ty({:?})", ty);
106
107         let tcx = self.fcx.tcx();
108
109         // <cur_ty as Deref>
110         let trait_ref = TraitRef {
111             def_id: tcx.lang_items().deref_trait()?,
112             substs: tcx.mk_substs_trait(self.cur_ty, &[]),
113         };
114
115         let cause = traits::ObligationCause::misc(self.span, self.fcx.body_id);
116
117         let mut selcx = traits::SelectionContext::new(self.fcx);
118         let obligation = traits::Obligation::new(cause.clone(),
119                                                  self.fcx.param_env,
120                                                  trait_ref.to_predicate());
121         if !selcx.evaluate_obligation(&obligation) {
122             debug!("overloaded_deref_ty: cannot match obligation");
123             return None;
124         }
125
126         let normalized = traits::normalize_projection_type(&mut selcx,
127                                                            self.fcx.param_env,
128                                                            ty::ProjectionTy::from_ref_and_name(
129                                                                tcx,
130                                                                trait_ref,
131                                                                Symbol::intern("Target"),
132                                                            ),
133                                                            cause,
134                                                            0);
135
136         debug!("overloaded_deref_ty({:?}) = {:?}", ty, normalized);
137         self.obligations.extend(normalized.obligations);
138
139         Some(self.fcx.resolve_type_vars_if_possible(&normalized.value))
140     }
141
142     /// Returns the final type, generating an error if it is an
143     /// unresolved inference variable.
144     pub fn unambiguous_final_ty(&self) -> Ty<'tcx> {
145         self.fcx.structurally_resolved_type(self.span, self.cur_ty)
146     }
147
148     /// Returns the final type we ended up with, which may well be an
149     /// inference variable (we will resolve it first, if possible).
150     pub fn maybe_ambiguous_final_ty(&self) -> Ty<'tcx> {
151         self.fcx.resolve_type_vars_if_possible(&self.cur_ty)
152     }
153
154     pub fn step_count(&self) -> usize {
155         self.steps.len()
156     }
157
158     /// Returns the adjustment steps.
159     pub fn adjust_steps(&self, pref: LvaluePreference)
160                         -> Vec<Adjustment<'tcx>> {
161         self.fcx.register_infer_ok_obligations(self.adjust_steps_as_infer_ok(pref))
162     }
163
164     pub fn adjust_steps_as_infer_ok(&self, pref: LvaluePreference)
165                                     -> InferOk<'tcx, Vec<Adjustment<'tcx>>> {
166         let mut obligations = vec![];
167         let targets = self.steps.iter().skip(1).map(|&(ty, _)| ty)
168             .chain(iter::once(self.cur_ty));
169         let steps: Vec<_> = self.steps.iter().map(|&(source, kind)| {
170             if let AutoderefKind::Overloaded = kind {
171                 self.fcx.try_overloaded_deref(self.span, source, pref)
172                     .and_then(|InferOk { value: method, obligations: o }| {
173                         obligations.extend(o);
174                         if let ty::TyRef(region, mt) = method.sig.output().sty {
175                             Some(OverloadedDeref {
176                                 region,
177                                 mutbl: mt.mutbl,
178                             })
179                         } else {
180                             None
181                         }
182                     })
183             } else {
184                 None
185             }
186         }).zip(targets).map(|(autoderef, target)| {
187             Adjustment {
188                 kind: Adjust::Deref(autoderef),
189                 target
190             }
191         }).collect();
192
193         InferOk {
194             obligations,
195             value: steps
196         }
197     }
198
199     /// also dereference through raw pointer types
200     /// e.g. assuming ptr_to_Foo is the type `*const Foo`
201     /// fcx.autoderef(span, ptr_to_Foo)  => [*const Foo]
202     /// fcx.autoderef(span, ptr_to_Foo).include_raw_ptrs() => [*const Foo, Foo]
203     pub fn include_raw_pointers(mut self) -> Self {
204         self.include_raw_pointers = true;
205         self
206     }
207
208     pub fn finalize(self) {
209         let fcx = self.fcx;
210         fcx.register_predicates(self.into_obligations());
211     }
212
213     pub fn into_obligations(self) -> Vec<traits::PredicateObligation<'tcx>> {
214         self.obligations
215     }
216 }
217
218 impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
219     pub fn autoderef(&'a self, span: Span, base_ty: Ty<'tcx>) -> Autoderef<'a, 'gcx, 'tcx> {
220         Autoderef {
221             fcx: self,
222             steps: vec![],
223             cur_ty: self.resolve_type_vars_if_possible(&base_ty),
224             obligations: vec![],
225             at_start: true,
226             include_raw_pointers: false,
227             span,
228         }
229     }
230
231     pub fn try_overloaded_deref(&self,
232                                 span: Span,
233                                 base_ty: Ty<'tcx>,
234                                 pref: LvaluePreference)
235                                 -> Option<InferOk<'tcx, MethodCallee<'tcx>>> {
236         self.try_overloaded_lvalue_op(span, base_ty, &[], pref, LvalueOp::Deref)
237     }
238 }