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