]> git.proxmox.com Git - rustc.git/blob - src/librustc_typeck/check/autoderef.rs
New upstream version 1.15.0+dfsg1
[rustc.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;
14
15 use rustc::traits;
16 use rustc::ty::{self, Ty, TraitRef};
17 use rustc::ty::{ToPredicate, TypeFoldable};
18 use rustc::ty::{MethodCall, MethodCallee};
19 use rustc::ty::{LvaluePreference, NoPreference, PreferMutLvalue};
20 use rustc::hir;
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 struct_span_err!(tcx.sess,
58 self.span,
59 E0055,
60 "reached the recursion limit while auto-dereferencing {:?}",
61 self.cur_ty)
62 .span_label(self.span, &format!("deref recursion limit reached"))
63 .emit();
64 return None;
65 }
66
67 if self.cur_ty.is_ty_var() {
68 return None;
69 }
70
71 // Otherwise, deref if type is derefable:
72 let (kind, new_ty) = if let Some(mt) = self.cur_ty.builtin_deref(false, NoPreference) {
73 (AutoderefKind::Builtin, mt.ty)
74 } else {
75 match self.overloaded_deref_ty(self.cur_ty) {
76 Some(ty) => (AutoderefKind::Overloaded, ty),
77 _ => return None,
78 }
79 };
80
81 if new_ty.references_error() {
82 return None;
83 }
84
85 self.steps.push((self.cur_ty, kind));
86 debug!("autoderef stage #{:?} is {:?} from {:?}",
87 self.steps.len(),
88 new_ty,
89 (self.cur_ty, kind));
90 self.cur_ty = new_ty;
91
92 Some((self.cur_ty, self.steps.len()))
93 }
94 }
95
96 impl<'a, 'gcx, 'tcx> Autoderef<'a, 'gcx, 'tcx> {
97 fn overloaded_deref_ty(&mut self, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
98 debug!("overloaded_deref_ty({:?})", ty);
99
100 let tcx = self.fcx.tcx();
101
102 // <cur_ty as Deref>
103 let trait_ref = TraitRef {
104 def_id: match tcx.lang_items.deref_trait() {
105 Some(f) => f,
106 None => return None,
107 },
108 substs: tcx.mk_substs_trait(self.cur_ty, &[]),
109 };
110
111 let cause = traits::ObligationCause::misc(self.span, self.fcx.body_id);
112
113 let mut selcx = traits::SelectionContext::new(self.fcx);
114 let obligation = traits::Obligation::new(cause.clone(), trait_ref.to_predicate());
115 if !selcx.evaluate_obligation(&obligation) {
116 debug!("overloaded_deref_ty: cannot match obligation");
117 return None;
118 }
119
120 let normalized = traits::normalize_projection_type(&mut selcx,
121 ty::ProjectionTy {
122 trait_ref: trait_ref,
123 item_name: Symbol::intern("Target"),
124 },
125 cause,
126 0);
127
128 debug!("overloaded_deref_ty({:?}) = {:?}", ty, normalized);
129 self.obligations.extend(normalized.obligations);
130
131 Some(self.fcx.resolve_type_vars_if_possible(&normalized.value))
132 }
133
134 pub fn unambiguous_final_ty(&self) -> Ty<'tcx> {
135 self.fcx.structurally_resolved_type(self.span, self.cur_ty)
136 }
137
138 pub fn finalize<'b, I>(self, pref: LvaluePreference, exprs: I)
139 where I: IntoIterator<Item = &'b hir::Expr>
140 {
141 let methods: Vec<_> = self.steps
142 .iter()
143 .map(|&(ty, kind)| {
144 if let AutoderefKind::Overloaded = kind {
145 self.fcx.try_overloaded_deref(self.span, None, ty, pref)
146 } else {
147 None
148 }
149 })
150 .collect();
151
152 debug!("finalize({:?}) - {:?},{:?}",
153 pref,
154 methods,
155 self.obligations);
156
157 for expr in exprs {
158 debug!("finalize - finalizing #{} - {:?}", expr.id, expr);
159 for (n, method) in methods.iter().enumerate() {
160 if let &Some(method) = method {
161 let method_call = MethodCall::autoderef(expr.id, n as u32);
162 self.fcx.tables.borrow_mut().method_map.insert(method_call, method);
163 }
164 }
165 }
166
167 for obligation in self.obligations {
168 self.fcx.register_predicate(obligation);
169 }
170 }
171 }
172
173 impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
174 pub fn autoderef(&'a self, span: Span, base_ty: Ty<'tcx>) -> Autoderef<'a, 'gcx, 'tcx> {
175 Autoderef {
176 fcx: self,
177 steps: vec![],
178 cur_ty: self.resolve_type_vars_if_possible(&base_ty),
179 obligations: vec![],
180 at_start: true,
181 span: span,
182 }
183 }
184
185 pub fn try_overloaded_deref(&self,
186 span: Span,
187 base_expr: Option<&hir::Expr>,
188 base_ty: Ty<'tcx>,
189 lvalue_pref: LvaluePreference)
190 -> Option<MethodCallee<'tcx>> {
191 debug!("try_overloaded_deref({:?},{:?},{:?},{:?})",
192 span,
193 base_expr,
194 base_ty,
195 lvalue_pref);
196 // Try DerefMut first, if preferred.
197 let method = match (lvalue_pref, self.tcx.lang_items.deref_mut_trait()) {
198 (PreferMutLvalue, Some(trait_did)) => {
199 self.lookup_method_in_trait(span,
200 base_expr,
201 Symbol::intern("deref_mut"),
202 trait_did,
203 base_ty,
204 None)
205 }
206 _ => None,
207 };
208
209 // Otherwise, fall back to Deref.
210 let method = match (method, self.tcx.lang_items.deref_trait()) {
211 (None, Some(trait_did)) => {
212 self.lookup_method_in_trait(span,
213 base_expr,
214 Symbol::intern("deref"),
215 trait_did,
216 base_ty,
217 None)
218 }
219 (method, _) => method,
220 };
221
222 method
223 }
224 }