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