1 use super::method::MethodCallee;
2 use super::{FnCtxt, Needs, PlaceOp};
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;
14 use syntax::ast::Ident;
16 use rustc_error_codes::*;
20 #[derive(Copy, Clone, Debug)]
26 pub struct Autoderef<'a, 'tcx> {
27 infcx: &'a InferCtxt<'a, 'tcx>,
29 param_env: ty::ParamEnv<'tcx>,
30 steps: Vec<(Ty<'tcx>, AutoderefKind)>,
32 obligations: Vec<traits::PredicateObligation<'tcx>>,
34 include_raw_pointers: bool,
37 reached_recursion_limit: bool,
40 impl<'a, 'tcx> Iterator for Autoderef<'a, 'tcx> {
41 type Item = (Ty<'tcx>, usize);
43 fn next(&mut self) -> Option<Self::Item> {
44 let tcx = self.infcx.tcx;
46 debug!("autoderef: steps={:?}, cur_ty={:?}", self.steps, self.cur_ty);
48 self.at_start = false;
49 debug!("autoderef stage #0 is {:?}", self.cur_ty);
50 return Some((self.cur_ty, 0));
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);
57 self.reached_recursion_limit = true;
61 if self.cur_ty.is_ty_var() {
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)
68 (AutoderefKind::Builtin, mt.ty)
70 let ty = self.overloaded_deref_ty(self.cur_ty)?;
71 (AutoderefKind::Overloaded, ty)
74 if new_ty.references_error() {
78 self.steps.push((self.cur_ty, kind));
80 "autoderef stage #{:?} is {:?} from {:?}",
87 Some((self.cur_ty, self.steps.len()))
91 impl<'a, 'tcx> Autoderef<'a, 'tcx> {
93 infcx: &'a InferCtxt<'a, 'tcx>,
94 param_env: ty::ParamEnv<'tcx>,
98 ) -> Autoderef<'a, 'tcx> {
104 cur_ty: infcx.resolve_vars_if_possible(&base_ty),
107 include_raw_pointers: false,
108 silence_errors: false,
109 reached_recursion_limit: false,
114 fn overloaded_deref_ty(&mut self, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
115 debug!("overloaded_deref_ty({:?})", ty);
117 let tcx = self.infcx.tcx;
120 let trait_ref = TraitRef {
121 def_id: tcx.lang_items().deref_trait()?,
122 substs: tcx.mk_substs_trait(self.cur_ty, &[]),
125 let cause = traits::ObligationCause::misc(self.span, self.body_id);
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");
134 let mut fulfillcx = traits::FulfillmentContext::new_in_snapshot();
135 let normalized_ty = fulfillcx.normalize_projection_type(
138 ty::ProjectionTy::from_ref_and_name(tcx, trait_ref, Ident::from_str("Target")),
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
145 debug!("overloaded_deref_ty: encountered errors {:?} while fulfilling", e);
148 let obligations = fulfillcx.pending_obligations();
149 debug!("overloaded_deref_ty({:?}) = ({:?}, {:?})", ty, normalized_ty, obligations);
150 self.obligations.extend(obligations);
152 Some(self.infcx.resolve_vars_if_possible(&normalized_ty))
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)
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)
167 pub fn step_count(&self) -> usize {
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))
176 pub fn adjust_steps_as_infer_ok(
178 fcx: &FnCtxt<'a, 'tcx>,
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
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 })
203 .map(|(autoderef, target)| Adjustment { kind: Adjust::Deref(autoderef), target })
206 InferOk { obligations, value: steps }
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;
218 pub fn silence_errors(mut self) -> Self {
219 self.silence_errors = true;
223 pub fn reached_recursion_limit(&self) -> bool {
224 self.reached_recursion_limit
227 pub fn finalize(self, fcx: &FnCtxt<'a, 'tcx>) {
228 fcx.register_predicates(self.into_obligations());
231 pub fn into_obligations(self) -> Vec<traits::PredicateObligation<'tcx>> {
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);
247 "reached the recursion limit while auto-dereferencing `{:?}`",
250 .span_label(span, "deref recursion limit reached")
252 "consider adding a `#![recursion_limit=\"{}\"]` attribute to your crate",
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)
264 pub fn try_overloaded_deref(
269 ) -> Option<InferOk<'tcx, MethodCallee<'tcx>>> {
270 self.try_overloaded_place_op(span, base_ty, &[], needs, PlaceOp::Deref)