]> git.proxmox.com Git - rustc.git/blob - src/librustc_typeck/check/autoderef.rs
New upstream version 1.19.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, 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 span: Span,
41 }
42
43 impl<'a, 'gcx, 'tcx> Iterator for Autoderef<'a, 'gcx, 'tcx> {
44 type Item = (Ty<'tcx>, usize);
45
46 fn next(&mut self) -> Option<Self::Item> {
47 let tcx = self.fcx.tcx;
48
49 debug!("autoderef: steps={:?}, cur_ty={:?}",
50 self.steps,
51 self.cur_ty);
52 if self.at_start {
53 self.at_start = false;
54 debug!("autoderef stage #0 is {:?}", self.cur_ty);
55 return Some((self.cur_ty, 0));
56 }
57
58 if self.steps.len() == tcx.sess.recursion_limit.get() {
59 // We've reached the recursion limit, error gracefully.
60 let suggested_limit = tcx.sess.recursion_limit.get() * 2;
61 struct_span_err!(tcx.sess,
62 self.span,
63 E0055,
64 "reached the recursion limit while auto-dereferencing {:?}",
65 self.cur_ty)
66 .span_label(self.span, "deref recursion limit reached")
67 .help(&format!(
68 "consider adding a `#[recursion_limit=\"{}\"]` attribute to your crate",
69 suggested_limit))
70 .emit();
71 return None;
72 }
73
74 if self.cur_ty.is_ty_var() {
75 return None;
76 }
77
78 // Otherwise, deref if type is derefable:
79 let (kind, new_ty) = if let Some(mt) = self.cur_ty.builtin_deref(false, NoPreference) {
80 (AutoderefKind::Builtin, mt.ty)
81 } else {
82 match self.overloaded_deref_ty(self.cur_ty) {
83 Some(ty) => (AutoderefKind::Overloaded, ty),
84 _ => return None,
85 }
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: match tcx.lang_items.deref_trait() {
112 Some(f) => f,
113 None => return None,
114 },
115 substs: tcx.mk_substs_trait(self.cur_ty, &[]),
116 };
117
118 let cause = traits::ObligationCause::misc(self.span, self.fcx.body_id);
119
120 let mut selcx = traits::SelectionContext::new(self.fcx);
121 let obligation = traits::Obligation::new(cause.clone(),
122 self.fcx.param_env,
123 trait_ref.to_predicate());
124 if !selcx.evaluate_obligation(&obligation) {
125 debug!("overloaded_deref_ty: cannot match obligation");
126 return None;
127 }
128
129 let normalized = traits::normalize_projection_type(&mut selcx,
130 self.fcx.param_env,
131 ty::ProjectionTy::from_ref_and_name(
132 tcx,
133 trait_ref,
134 Symbol::intern("Target"),
135 ),
136 cause,
137 0);
138
139 debug!("overloaded_deref_ty({:?}) = {:?}", ty, normalized);
140 self.obligations.extend(normalized.obligations);
141
142 Some(self.fcx.resolve_type_vars_if_possible(&normalized.value))
143 }
144
145 /// Returns the final type, generating an error if it is an
146 /// unresolved inference variable.
147 pub fn unambiguous_final_ty(&self) -> Ty<'tcx> {
148 self.fcx.structurally_resolved_type(self.span, self.cur_ty)
149 }
150
151 /// Returns the final type we ended up with, which may well be an
152 /// inference variable (we will resolve it first, if possible).
153 pub fn maybe_ambiguous_final_ty(&self) -> Ty<'tcx> {
154 self.fcx.resolve_type_vars_if_possible(&self.cur_ty)
155 }
156
157 pub fn step_count(&self) -> usize {
158 self.steps.len()
159 }
160
161 /// Returns the adjustment steps.
162 pub fn adjust_steps(&self, pref: LvaluePreference)
163 -> Vec<Adjustment<'tcx>> {
164 self.fcx.register_infer_ok_obligations(self.adjust_steps_as_infer_ok(pref))
165 }
166
167 pub fn adjust_steps_as_infer_ok(&self, pref: LvaluePreference)
168 -> InferOk<'tcx, Vec<Adjustment<'tcx>>> {
169 let mut obligations = vec![];
170 let targets = self.steps.iter().skip(1).map(|&(ty, _)| ty)
171 .chain(iter::once(self.cur_ty));
172 let steps: Vec<_> = self.steps.iter().map(|&(source, kind)| {
173 if let AutoderefKind::Overloaded = kind {
174 self.fcx.try_overloaded_deref(self.span, source, pref)
175 .and_then(|InferOk { value: method, obligations: o }| {
176 obligations.extend(o);
177 if let ty::TyRef(region, mt) = method.sig.output().sty {
178 Some(OverloadedDeref {
179 region,
180 mutbl: mt.mutbl,
181 })
182 } else {
183 None
184 }
185 })
186 } else {
187 None
188 }
189 }).zip(targets).map(|(autoderef, target)| {
190 Adjustment {
191 kind: Adjust::Deref(autoderef),
192 target
193 }
194 }).collect();
195
196 InferOk {
197 obligations,
198 value: steps
199 }
200 }
201
202 pub fn finalize(self) {
203 let fcx = self.fcx;
204 fcx.register_predicates(self.into_obligations());
205 }
206
207 pub fn into_obligations(self) -> Vec<traits::PredicateObligation<'tcx>> {
208 self.obligations
209 }
210 }
211
212 impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
213 pub fn autoderef(&'a self, span: Span, base_ty: Ty<'tcx>) -> Autoderef<'a, 'gcx, 'tcx> {
214 Autoderef {
215 fcx: self,
216 steps: vec![],
217 cur_ty: self.resolve_type_vars_if_possible(&base_ty),
218 obligations: vec![],
219 at_start: true,
220 span: span,
221 }
222 }
223
224 pub fn try_overloaded_deref(&self,
225 span: Span,
226 base_ty: Ty<'tcx>,
227 pref: LvaluePreference)
228 -> Option<InferOk<'tcx, MethodCallee<'tcx>>> {
229 self.try_overloaded_lvalue_op(span, base_ty, &[], pref, LvalueOp::Deref)
230 }
231 }