]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/check/autoderef.rs
Rollup merge of #53559 - Dylan-DPC:fix/missing-doc-lint, r=kennytm
[rust.git] / src / librustc_typeck / check / autoderef.rs
1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use astconv::AstConv;
12
13 use super::{FnCtxt, PlaceOp, Needs};
14 use super::method::MethodCallee;
15
16 use rustc::infer::InferOk;
17 use rustc::session::DiagnosticMessageId;
18 use rustc::traits;
19 use rustc::ty::{self, Ty, TraitRef};
20 use rustc::ty::{ToPredicate, TypeFoldable};
21 use rustc::ty::adjustment::{Adjustment, Adjust, OverloadedDeref};
22
23 use syntax_pos::Span;
24 use syntax::ast::Ident;
25
26 use std::iter;
27
28 #[derive(Copy, Clone, Debug)]
29 enum AutoderefKind {
30     Builtin,
31     Overloaded,
32 }
33
34 pub struct Autoderef<'a, 'gcx: 'tcx, 'tcx: 'a> {
35     fcx: &'a FnCtxt<'a, 'gcx, 'tcx>,
36     steps: Vec<(Ty<'tcx>, AutoderefKind)>,
37     cur_ty: Ty<'tcx>,
38     obligations: Vec<traits::PredicateObligation<'tcx>>,
39     at_start: bool,
40     include_raw_pointers: bool,
41     span: Span,
42 }
43
44 impl<'a, 'gcx, 'tcx> Iterator for Autoderef<'a, 'gcx, 'tcx> {
45     type Item = (Ty<'tcx>, usize);
46
47     fn next(&mut self) -> Option<Self::Item> {
48         let tcx = self.fcx.tcx;
49
50         debug!("autoderef: steps={:?}, cur_ty={:?}",
51                self.steps,
52                self.cur_ty);
53         if self.at_start {
54             self.at_start = false;
55             debug!("autoderef stage #0 is {:?}", self.cur_ty);
56             return Some((self.cur_ty, 0));
57         }
58
59         if self.steps.len() >= *tcx.sess.recursion_limit.get() {
60             // We've reached the recursion limit, error gracefully.
61             let suggested_limit = *tcx.sess.recursion_limit.get() * 2;
62             let msg = format!("reached the recursion limit while auto-dereferencing {:?}",
63                               self.cur_ty);
64             let error_id = (DiagnosticMessageId::ErrorId(55), Some(self.span), msg.clone());
65             let fresh = tcx.sess.one_time_diagnostics.borrow_mut().insert(error_id);
66             if fresh {
67                 struct_span_err!(tcx.sess,
68                                  self.span,
69                                  E0055,
70                                  "reached the recursion limit while auto-dereferencing {:?}",
71                                  self.cur_ty)
72                     .span_label(self.span, "deref recursion limit reached")
73                     .help(&format!(
74                         "consider adding a `#![recursion_limit=\"{}\"]` attribute to your crate",
75                         suggested_limit))
76                     .emit();
77             }
78             return None;
79         }
80
81         if self.cur_ty.is_ty_var() {
82             return None;
83         }
84
85         // Otherwise, deref if type is derefable:
86         let (kind, new_ty) =
87             if let Some(mt) = self.cur_ty.builtin_deref(self.include_raw_pointers) {
88                 (AutoderefKind::Builtin, mt.ty)
89             } else {
90                 let ty = self.overloaded_deref_ty(self.cur_ty)?;
91                 (AutoderefKind::Overloaded, ty)
92             };
93
94         if new_ty.references_error() {
95             return None;
96         }
97
98         self.steps.push((self.cur_ty, kind));
99         debug!("autoderef stage #{:?} is {:?} from {:?}",
100                self.steps.len(),
101                new_ty,
102                (self.cur_ty, kind));
103         self.cur_ty = new_ty;
104
105         Some((self.cur_ty, self.steps.len()))
106     }
107 }
108
109 impl<'a, 'gcx, 'tcx> Autoderef<'a, 'gcx, 'tcx> {
110     fn overloaded_deref_ty(&mut self, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
111         debug!("overloaded_deref_ty({:?})", ty);
112
113         let tcx = self.fcx.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.fcx.body_id);
122
123         let obligation = traits::Obligation::new(cause.clone(),
124                                                  self.fcx.param_env,
125                                                  trait_ref.to_predicate());
126         if !self.fcx.predicate_may_hold(&obligation) {
127             debug!("overloaded_deref_ty: cannot match obligation");
128             return None;
129         }
130
131         let mut selcx = traits::SelectionContext::new(self.fcx);
132         let normalized_ty = traits::normalize_projection_type(&mut selcx,
133                                                               self.fcx.param_env,
134                                                               ty::ProjectionTy::from_ref_and_name(
135                                                                   tcx,
136                                                                   trait_ref,
137                                                                   Ident::from_str("Target"),
138                                                               ),
139                                                               cause,
140                                                               0,
141                                                               &mut self.obligations);
142
143         debug!("overloaded_deref_ty({:?}) = {:?}", ty, normalized_ty);
144
145         Some(self.fcx.resolve_type_vars_if_possible(&normalized_ty))
146     }
147
148     /// Returns the final type, generating an error if it is an
149     /// unresolved inference variable.
150     pub fn unambiguous_final_ty(&self) -> Ty<'tcx> {
151         self.fcx.structurally_resolved_type(self.span, self.cur_ty)
152     }
153
154     /// Returns the final type we ended up with, which may well be an
155     /// inference variable (we will resolve it first, if possible).
156     pub fn maybe_ambiguous_final_ty(&self) -> Ty<'tcx> {
157         self.fcx.resolve_type_vars_if_possible(&self.cur_ty)
158     }
159
160     pub fn step_count(&self) -> usize {
161         self.steps.len()
162     }
163
164     /// Returns the adjustment steps.
165     pub fn adjust_steps(&self, needs: Needs)
166                         -> Vec<Adjustment<'tcx>> {
167         self.fcx.register_infer_ok_obligations(self.adjust_steps_as_infer_ok(needs))
168     }
169
170     pub fn adjust_steps_as_infer_ok(&self, needs: Needs)
171                                     -> InferOk<'tcx, Vec<Adjustment<'tcx>>> {
172         let mut obligations = vec![];
173         let targets = self.steps.iter().skip(1).map(|&(ty, _)| ty)
174             .chain(iter::once(self.cur_ty));
175         let steps: Vec<_> = self.steps.iter().map(|&(source, kind)| {
176             if let AutoderefKind::Overloaded = kind {
177                 self.fcx.try_overloaded_deref(self.span, source, needs)
178                     .and_then(|InferOk { value: method, obligations: o }| {
179                         obligations.extend(o);
180                         if let ty::Ref(region, _, mutbl) = method.sig.output().sty {
181                             Some(OverloadedDeref {
182                                 region,
183                                 mutbl,
184                             })
185                         } else {
186                             None
187                         }
188                     })
189             } else {
190                 None
191             }
192         }).zip(targets).map(|(autoderef, target)| {
193             Adjustment {
194                 kind: Adjust::Deref(autoderef),
195                 target
196             }
197         }).collect();
198
199         InferOk {
200             obligations,
201             value: steps
202         }
203     }
204
205     /// also dereference through raw pointer types
206     /// e.g. assuming ptr_to_Foo is the type `*const Foo`
207     /// fcx.autoderef(span, ptr_to_Foo)  => [*const Foo]
208     /// fcx.autoderef(span, ptr_to_Foo).include_raw_ptrs() => [*const Foo, Foo]
209     pub fn include_raw_pointers(mut self) -> Self {
210         self.include_raw_pointers = true;
211         self
212     }
213
214     pub fn finalize(self) {
215         let fcx = self.fcx;
216         fcx.register_predicates(self.into_obligations());
217     }
218
219     pub fn into_obligations(self) -> Vec<traits::PredicateObligation<'tcx>> {
220         self.obligations
221     }
222 }
223
224 impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
225     pub fn autoderef(&'a self, span: Span, base_ty: Ty<'tcx>) -> Autoderef<'a, 'gcx, 'tcx> {
226         Autoderef {
227             fcx: self,
228             steps: vec![],
229             cur_ty: self.resolve_type_vars_if_possible(&base_ty),
230             obligations: vec![],
231             at_start: true,
232             include_raw_pointers: false,
233             span,
234         }
235     }
236
237     pub fn try_overloaded_deref(&self,
238                                 span: Span,
239                                 base_ty: Ty<'tcx>,
240                                 needs: Needs)
241                                 -> Option<InferOk<'tcx, MethodCallee<'tcx>>> {
242         self.try_overloaded_place_op(span, base_ty, &[], needs, PlaceOp::Deref)
243     }
244 }