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