]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/check/autoderef.rs
ae6c1738da77dce1b742e250671057ace0d3c46d
[rust.git] / src / librustc_typeck / check / autoderef.rs
1 use super::method::MethodCallee;
2 use super::{FnCtxt, Needs, PlaceOp};
3
4 use rustc_errors::struct_span_err;
5 use rustc_hir as hir;
6 use rustc_infer::infer::{InferCtxt, InferOk};
7 use rustc_middle::ty::adjustment::{Adjust, Adjustment, OverloadedDeref};
8 use rustc_middle::ty::{self, TraitRef, Ty, TyCtxt, WithConstness};
9 use rustc_middle::ty::{ToPredicate, TypeFoldable};
10 use rustc_session::DiagnosticMessageId;
11 use rustc_span::symbol::Ident;
12 use rustc_span::Span;
13 use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
14 use rustc_trait_selection::traits::{self, TraitEngine};
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         // <ty as Deref>
118         let trait_ref = TraitRef {
119             def_id: tcx.lang_items().deref_trait()?,
120             substs: tcx.mk_substs_trait(ty, &[]),
121         };
122
123         let cause = traits::ObligationCause::misc(self.span, self.body_id);
124
125         let obligation = traits::Obligation::new(
126             cause.clone(),
127             self.param_env,
128             trait_ref.without_const().to_predicate(),
129         );
130         if !self.infcx.predicate_may_hold(&obligation) {
131             debug!("overloaded_deref_ty: cannot match obligation");
132             return None;
133         }
134
135         let mut fulfillcx = traits::FulfillmentContext::new_in_snapshot();
136         let normalized_ty = fulfillcx.normalize_projection_type(
137             &self.infcx,
138             self.param_env,
139             ty::ProjectionTy::from_ref_and_name(tcx, trait_ref, Ident::from_str("Target")),
140             cause,
141         );
142         if let Err(e) = fulfillcx.select_where_possible(&self.infcx) {
143             // This shouldn't happen, except for evaluate/fulfill mismatches,
144             // but that's not a reason for an ICE (`predicate_may_hold` is conservative
145             // by design).
146             debug!("overloaded_deref_ty: encountered errors {:?} while fulfilling", e);
147             return None;
148         }
149         let obligations = fulfillcx.pending_obligations();
150         debug!("overloaded_deref_ty({:?}) = ({:?}, {:?})", ty, normalized_ty, obligations);
151         self.obligations.extend(obligations);
152
153         Some(self.infcx.resolve_vars_if_possible(&normalized_ty))
154     }
155
156     /// Returns the final type, generating an error if it is an
157     /// unresolved inference variable.
158     pub fn unambiguous_final_ty(&self, fcx: &FnCtxt<'a, 'tcx>) -> Ty<'tcx> {
159         fcx.structurally_resolved_type(self.span, self.cur_ty)
160     }
161
162     /// Returns the final type we ended up with, which may well be an
163     /// inference variable (we will resolve it first, if possible).
164     pub fn maybe_ambiguous_final_ty(&self) -> Ty<'tcx> {
165         self.infcx.resolve_vars_if_possible(&self.cur_ty)
166     }
167
168     pub fn step_count(&self) -> usize {
169         self.steps.len()
170     }
171
172     /// Returns the adjustment steps.
173     pub fn adjust_steps(&self, fcx: &FnCtxt<'a, 'tcx>, needs: Needs) -> Vec<Adjustment<'tcx>> {
174         fcx.register_infer_ok_obligations(self.adjust_steps_as_infer_ok(fcx, needs))
175     }
176
177     pub fn adjust_steps_as_infer_ok(
178         &self,
179         fcx: &FnCtxt<'a, 'tcx>,
180         needs: Needs,
181     ) -> InferOk<'tcx, Vec<Adjustment<'tcx>>> {
182         let mut obligations = vec![];
183         let targets = self.steps.iter().skip(1).map(|&(ty, _)| ty).chain(iter::once(self.cur_ty));
184         let steps: Vec<_> = self
185             .steps
186             .iter()
187             .map(|&(source, kind)| {
188                 if let AutoderefKind::Overloaded = kind {
189                     fcx.try_overloaded_deref(self.span, source, needs).and_then(
190                         |InferOk { value: method, obligations: o }| {
191                             obligations.extend(o);
192                             if let ty::Ref(region, _, mutbl) = method.sig.output().kind {
193                                 Some(OverloadedDeref { region, mutbl })
194                             } else {
195                                 None
196                             }
197                         },
198                     )
199                 } else {
200                     None
201                 }
202             })
203             .zip(targets)
204             .map(|(autoderef, target)| Adjustment { kind: Adjust::Deref(autoderef), target })
205             .collect();
206
207         InferOk { obligations, value: steps }
208     }
209
210     /// also dereference through raw pointer types
211     /// e.g., assuming ptr_to_Foo is the type `*const Foo`
212     /// fcx.autoderef(span, ptr_to_Foo)  => [*const Foo]
213     /// fcx.autoderef(span, ptr_to_Foo).include_raw_ptrs() => [*const Foo, Foo]
214     pub fn include_raw_pointers(mut self) -> Self {
215         self.include_raw_pointers = true;
216         self
217     }
218
219     pub fn silence_errors(mut self) -> Self {
220         self.silence_errors = true;
221         self
222     }
223
224     pub fn reached_recursion_limit(&self) -> bool {
225         self.reached_recursion_limit
226     }
227
228     pub fn finalize(self, fcx: &FnCtxt<'a, 'tcx>) {
229         fcx.register_predicates(self.into_obligations());
230     }
231
232     pub fn into_obligations(self) -> Vec<traits::PredicateObligation<'tcx>> {
233         self.obligations
234     }
235 }
236
237 pub fn report_autoderef_recursion_limit_error<'tcx>(tcx: TyCtxt<'tcx>, span: Span, ty: Ty<'tcx>) {
238     // We've reached the recursion limit, error gracefully.
239     let suggested_limit = *tcx.sess.recursion_limit.get() * 2;
240     let msg = format!("reached the recursion limit while auto-dereferencing `{:?}`", ty);
241     let error_id = (DiagnosticMessageId::ErrorId(55), Some(span), msg);
242     let fresh = tcx.sess.one_time_diagnostics.borrow_mut().insert(error_id);
243     if fresh {
244         struct_span_err!(
245             tcx.sess,
246             span,
247             E0055,
248             "reached the recursion limit while auto-dereferencing `{:?}`",
249             ty
250         )
251         .span_label(span, "deref recursion limit reached")
252         .help(&format!(
253             "consider adding a `#![recursion_limit=\"{}\"]` attribute to your crate (`{}`)",
254             suggested_limit, tcx.crate_name,
255         ))
256         .emit();
257     }
258 }
259
260 impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
261     pub fn autoderef(&'a self, span: Span, base_ty: Ty<'tcx>) -> Autoderef<'a, 'tcx> {
262         Autoderef::new(self, self.param_env, self.body_id, span, base_ty)
263     }
264
265     pub fn try_overloaded_deref(
266         &self,
267         span: Span,
268         base_ty: Ty<'tcx>,
269         needs: Needs,
270     ) -> Option<InferOk<'tcx, MethodCallee<'tcx>>> {
271         self.try_overloaded_place_op(span, base_ty, &[], needs, PlaceOp::Deref)
272     }
273 }