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