]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/check/autoderef.rs
Remove rustc_hir reexports in rustc::hir.
[rust.git] / src / librustc_typeck / check / autoderef.rs
1 use super::method::MethodCallee;
2 use super::{FnCtxt, Needs, PlaceOp};
3
4 use rustc::infer::{InferCtxt, InferOk};
5 use rustc::session::DiagnosticMessageId;
6 use rustc::traits::{self, TraitEngine};
7 use rustc::ty::adjustment::{Adjust, Adjustment, OverloadedDeref};
8 use rustc::ty::{self, TraitRef, Ty, TyCtxt};
9 use rustc::ty::{ToPredicate, TypeFoldable};
10 use rustc_hir as hir;
11
12 use rustc_span::Span;
13 use syntax::ast::Ident;
14
15 use rustc_error_codes::*;
16
17 use std::iter;
18
19 #[derive(Copy, Clone, Debug)]
20 enum AutoderefKind {
21     Builtin,
22     Overloaded,
23 }
24
25 pub struct Autoderef<'a, 'tcx> {
26     infcx: &'a InferCtxt<'a, 'tcx>,
27     body_id: hir::HirId,
28     param_env: ty::ParamEnv<'tcx>,
29     steps: Vec<(Ty<'tcx>, AutoderefKind)>,
30     cur_ty: Ty<'tcx>,
31     obligations: Vec<traits::PredicateObligation<'tcx>>,
32     at_start: bool,
33     include_raw_pointers: bool,
34     span: Span,
35     silence_errors: bool,
36     reached_recursion_limit: bool,
37 }
38
39 impl<'a, 'tcx> Iterator for Autoderef<'a, 'tcx> {
40     type Item = (Ty<'tcx>, usize);
41
42     fn next(&mut self) -> Option<Self::Item> {
43         let tcx = self.infcx.tcx;
44
45         debug!("autoderef: steps={:?}, cur_ty={:?}", self.steps, self.cur_ty);
46         if self.at_start {
47             self.at_start = false;
48             debug!("autoderef stage #0 is {:?}", self.cur_ty);
49             return Some((self.cur_ty, 0));
50         }
51
52         if self.steps.len() >= *tcx.sess.recursion_limit.get() {
53             if !self.silence_errors {
54                 report_autoderef_recursion_limit_error(tcx, self.span, self.cur_ty);
55             }
56             self.reached_recursion_limit = true;
57             return None;
58         }
59
60         if self.cur_ty.is_ty_var() {
61             return None;
62         }
63
64         // Otherwise, deref if type is derefable:
65         let (kind, new_ty) = if let Some(mt) = self.cur_ty.builtin_deref(self.include_raw_pointers)
66         {
67             (AutoderefKind::Builtin, mt.ty)
68         } else {
69             let ty = self.overloaded_deref_ty(self.cur_ty)?;
70             (AutoderefKind::Overloaded, ty)
71         };
72
73         if new_ty.references_error() {
74             return None;
75         }
76
77         self.steps.push((self.cur_ty, kind));
78         debug!(
79             "autoderef stage #{:?} is {:?} from {:?}",
80             self.steps.len(),
81             new_ty,
82             (self.cur_ty, kind)
83         );
84         self.cur_ty = new_ty;
85
86         Some((self.cur_ty, self.steps.len()))
87     }
88 }
89
90 impl<'a, 'tcx> Autoderef<'a, 'tcx> {
91     pub fn new(
92         infcx: &'a InferCtxt<'a, 'tcx>,
93         param_env: ty::ParamEnv<'tcx>,
94         body_id: hir::HirId,
95         span: Span,
96         base_ty: Ty<'tcx>,
97     ) -> Autoderef<'a, 'tcx> {
98         Autoderef {
99             infcx,
100             body_id,
101             param_env,
102             steps: vec![],
103             cur_ty: infcx.resolve_vars_if_possible(&base_ty),
104             obligations: vec![],
105             at_start: true,
106             include_raw_pointers: false,
107             silence_errors: false,
108             reached_recursion_limit: false,
109             span,
110         }
111     }
112
113     fn overloaded_deref_ty(&mut self, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
114         debug!("overloaded_deref_ty({:?})", ty);
115
116         let tcx = self.infcx.tcx;
117
118         // <cur_ty as Deref>
119         let trait_ref = TraitRef {
120             def_id: tcx.lang_items().deref_trait()?,
121             substs: tcx.mk_substs_trait(self.cur_ty, &[]),
122         };
123
124         let cause = traits::ObligationCause::misc(self.span, self.body_id);
125
126         let obligation =
127             traits::Obligation::new(cause.clone(), self.param_env, trait_ref.to_predicate());
128         if !self.infcx.predicate_may_hold(&obligation) {
129             debug!("overloaded_deref_ty: cannot match obligation");
130             return None;
131         }
132
133         let mut fulfillcx = traits::FulfillmentContext::new_in_snapshot();
134         let normalized_ty = fulfillcx.normalize_projection_type(
135             &self.infcx,
136             self.param_env,
137             ty::ProjectionTy::from_ref_and_name(tcx, trait_ref, Ident::from_str("Target")),
138             cause,
139         );
140         if let Err(e) = fulfillcx.select_where_possible(&self.infcx) {
141             // This shouldn't happen, except for evaluate/fulfill mismatches,
142             // but that's not a reason for an ICE (`predicate_may_hold` is conservative
143             // by design).
144             debug!("overloaded_deref_ty: encountered errors {:?} while fulfilling", e);
145             return None;
146         }
147         let obligations = fulfillcx.pending_obligations();
148         debug!("overloaded_deref_ty({:?}) = ({:?}, {:?})", ty, normalized_ty, obligations);
149         self.obligations.extend(obligations);
150
151         Some(self.infcx.resolve_vars_if_possible(&normalized_ty))
152     }
153
154     /// Returns the final type, generating an error if it is an
155     /// unresolved inference variable.
156     pub fn unambiguous_final_ty(&self, fcx: &FnCtxt<'a, 'tcx>) -> Ty<'tcx> {
157         fcx.structurally_resolved_type(self.span, self.cur_ty)
158     }
159
160     /// Returns the final type we ended up with, which may well be an
161     /// inference variable (we will resolve it first, if possible).
162     pub fn maybe_ambiguous_final_ty(&self) -> Ty<'tcx> {
163         self.infcx.resolve_vars_if_possible(&self.cur_ty)
164     }
165
166     pub fn step_count(&self) -> usize {
167         self.steps.len()
168     }
169
170     /// Returns the adjustment steps.
171     pub fn adjust_steps(&self, fcx: &FnCtxt<'a, 'tcx>, needs: Needs) -> Vec<Adjustment<'tcx>> {
172         fcx.register_infer_ok_obligations(self.adjust_steps_as_infer_ok(fcx, needs))
173     }
174
175     pub fn adjust_steps_as_infer_ok(
176         &self,
177         fcx: &FnCtxt<'a, 'tcx>,
178         needs: Needs,
179     ) -> InferOk<'tcx, Vec<Adjustment<'tcx>>> {
180         let mut obligations = vec![];
181         let targets = self.steps.iter().skip(1).map(|&(ty, _)| ty).chain(iter::once(self.cur_ty));
182         let steps: Vec<_> = self
183             .steps
184             .iter()
185             .map(|&(source, kind)| {
186                 if let AutoderefKind::Overloaded = kind {
187                     fcx.try_overloaded_deref(self.span, source, needs).and_then(
188                         |InferOk { value: method, obligations: o }| {
189                             obligations.extend(o);
190                             if let ty::Ref(region, _, mutbl) = method.sig.output().kind {
191                                 Some(OverloadedDeref { region, mutbl })
192                             } else {
193                                 None
194                             }
195                         },
196                     )
197                 } else {
198                     None
199                 }
200             })
201             .zip(targets)
202             .map(|(autoderef, target)| Adjustment { kind: Adjust::Deref(autoderef), target })
203             .collect();
204
205         InferOk { obligations, value: steps }
206     }
207
208     /// also dereference through raw pointer types
209     /// e.g., assuming ptr_to_Foo is the type `*const Foo`
210     /// fcx.autoderef(span, ptr_to_Foo)  => [*const Foo]
211     /// fcx.autoderef(span, ptr_to_Foo).include_raw_ptrs() => [*const Foo, Foo]
212     pub fn include_raw_pointers(mut self) -> Self {
213         self.include_raw_pointers = true;
214         self
215     }
216
217     pub fn silence_errors(mut self) -> Self {
218         self.silence_errors = true;
219         self
220     }
221
222     pub fn reached_recursion_limit(&self) -> bool {
223         self.reached_recursion_limit
224     }
225
226     pub fn finalize(self, fcx: &FnCtxt<'a, 'tcx>) {
227         fcx.register_predicates(self.into_obligations());
228     }
229
230     pub fn into_obligations(self) -> Vec<traits::PredicateObligation<'tcx>> {
231         self.obligations
232     }
233 }
234
235 pub fn report_autoderef_recursion_limit_error<'tcx>(tcx: TyCtxt<'tcx>, span: Span, ty: Ty<'tcx>) {
236     // We've reached the recursion limit, error gracefully.
237     let suggested_limit = *tcx.sess.recursion_limit.get() * 2;
238     let msg = format!("reached the recursion limit while auto-dereferencing `{:?}`", ty);
239     let error_id = (DiagnosticMessageId::ErrorId(55), Some(span), msg);
240     let fresh = tcx.sess.one_time_diagnostics.borrow_mut().insert(error_id);
241     if fresh {
242         struct_span_err!(
243             tcx.sess,
244             span,
245             E0055,
246             "reached the recursion limit while auto-dereferencing `{:?}`",
247             ty
248         )
249         .span_label(span, "deref recursion limit reached")
250         .help(&format!(
251             "consider adding a `#![recursion_limit=\"{}\"]` attribute to your crate",
252             suggested_limit
253         ))
254         .emit();
255     }
256 }
257
258 impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
259     pub fn autoderef(&'a self, span: Span, base_ty: Ty<'tcx>) -> Autoderef<'a, 'tcx> {
260         Autoderef::new(self, self.param_env, self.body_id, span, base_ty)
261     }
262
263     pub fn try_overloaded_deref(
264         &self,
265         span: Span,
266         base_ty: Ty<'tcx>,
267         needs: Needs,
268     ) -> Option<InferOk<'tcx, MethodCallee<'tcx>>> {
269         self.try_overloaded_place_op(span, base_ty, &[], needs, PlaceOp::Deref)
270     }
271 }