1 use super::method::MethodCallee;
2 use super::{FnCtxt, Needs, PlaceOp};
5 use rustc::infer::{InferCtxt, InferOk};
6 use rustc::session::DiagnosticMessageId;
7 use rustc::traits::{self, TraitEngine};
8 use rustc::ty::adjustment::{Adjust, Adjustment, OverloadedDeref};
9 use rustc::ty::{self, TraitRef, Ty, TyCtxt};
10 use rustc::ty::{ToPredicate, TypeFoldable};
13 use syntax::ast::Ident;
15 use rustc_error_codes::*;
19 #[derive(Copy, Clone, Debug)]
25 pub struct Autoderef<'a, 'tcx> {
26 infcx: &'a InferCtxt<'a, 'tcx>,
28 param_env: ty::ParamEnv<'tcx>,
29 steps: Vec<(Ty<'tcx>, AutoderefKind)>,
31 obligations: Vec<traits::PredicateObligation<'tcx>>,
33 include_raw_pointers: bool,
36 reached_recursion_limit: bool,
39 impl<'a, 'tcx> Iterator for Autoderef<'a, 'tcx> {
40 type Item = (Ty<'tcx>, usize);
42 fn next(&mut self) -> Option<Self::Item> {
43 let tcx = self.infcx.tcx;
45 debug!("autoderef: steps={:?}, cur_ty={:?}", self.steps, self.cur_ty);
47 self.at_start = false;
48 debug!("autoderef stage #0 is {:?}", self.cur_ty);
49 return Some((self.cur_ty, 0));
52 if self.steps.len() >= *tcx.sess.recursion_limit.get() {
53 if !self.silence_errors {
54 report_autoderef_recursion_limit_error(tcx, self.span, self.cur_ty);
56 self.reached_recursion_limit = true;
60 if self.cur_ty.is_ty_var() {
64 // Otherwise, deref if type is derefable:
65 let (kind, new_ty) = if let Some(mt) = self.cur_ty.builtin_deref(self.include_raw_pointers)
67 (AutoderefKind::Builtin, mt.ty)
69 let ty = self.overloaded_deref_ty(self.cur_ty)?;
70 (AutoderefKind::Overloaded, ty)
73 if new_ty.references_error() {
77 self.steps.push((self.cur_ty, kind));
79 "autoderef stage #{:?} is {:?} from {:?}",
86 Some((self.cur_ty, self.steps.len()))
90 impl<'a, 'tcx> Autoderef<'a, 'tcx> {
92 infcx: &'a InferCtxt<'a, 'tcx>,
93 param_env: ty::ParamEnv<'tcx>,
97 ) -> Autoderef<'a, 'tcx> {
103 cur_ty: infcx.resolve_vars_if_possible(&base_ty),
106 include_raw_pointers: false,
107 silence_errors: false,
108 reached_recursion_limit: false,
113 fn overloaded_deref_ty(&mut self, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
114 debug!("overloaded_deref_ty({:?})", ty);
116 let tcx = self.infcx.tcx;
119 let trait_ref = TraitRef {
120 def_id: tcx.lang_items().deref_trait()?,
121 substs: tcx.mk_substs_trait(self.cur_ty, &[]),
124 let cause = traits::ObligationCause::misc(self.span, self.body_id);
127 traits::Obligation::new(cause.clone(), self.param_env, trait_ref.to_predicate());
128 if !self.infcx.predicate_may_hold(&obligation) {
129 debug!("overloaded_deref_ty: cannot match obligation");
133 let mut fulfillcx = traits::FulfillmentContext::new_in_snapshot();
134 let normalized_ty = fulfillcx.normalize_projection_type(
137 ty::ProjectionTy::from_ref_and_name(tcx, trait_ref, Ident::from_str("Target")),
140 if let Err(e) = fulfillcx.select_where_possible(&self.infcx) {
141 // This shouldn't happen, except for evaluate/fulfill mismatches,
142 // but that's not a reason for an ICE (`predicate_may_hold` is conservative
144 debug!("overloaded_deref_ty: encountered errors {:?} while fulfilling", e);
147 let obligations = fulfillcx.pending_obligations();
148 debug!("overloaded_deref_ty({:?}) = ({:?}, {:?})", ty, normalized_ty, obligations);
149 self.obligations.extend(obligations);
151 Some(self.infcx.resolve_vars_if_possible(&normalized_ty))
154 /// Returns the final type, generating an error if it is an
155 /// unresolved inference variable.
156 pub fn unambiguous_final_ty(&self, fcx: &FnCtxt<'a, 'tcx>) -> Ty<'tcx> {
157 fcx.structurally_resolved_type(self.span, self.cur_ty)
160 /// Returns the final type we ended up with, which may well be an
161 /// inference variable (we will resolve it first, if possible).
162 pub fn maybe_ambiguous_final_ty(&self) -> Ty<'tcx> {
163 self.infcx.resolve_vars_if_possible(&self.cur_ty)
166 pub fn step_count(&self) -> usize {
170 /// Returns the adjustment steps.
171 pub fn adjust_steps(&self, fcx: &FnCtxt<'a, 'tcx>, needs: Needs) -> Vec<Adjustment<'tcx>> {
172 fcx.register_infer_ok_obligations(self.adjust_steps_as_infer_ok(fcx, needs))
175 pub fn adjust_steps_as_infer_ok(
177 fcx: &FnCtxt<'a, 'tcx>,
179 ) -> InferOk<'tcx, Vec<Adjustment<'tcx>>> {
180 let mut obligations = vec![];
181 let targets = self.steps.iter().skip(1).map(|&(ty, _)| ty).chain(iter::once(self.cur_ty));
182 let steps: Vec<_> = self
185 .map(|&(source, kind)| {
186 if let AutoderefKind::Overloaded = kind {
187 fcx.try_overloaded_deref(self.span, source, needs).and_then(
188 |InferOk { value: method, obligations: o }| {
189 obligations.extend(o);
190 if let ty::Ref(region, _, mutbl) = method.sig.output().kind {
191 Some(OverloadedDeref { region, mutbl })
202 .map(|(autoderef, target)| Adjustment { kind: Adjust::Deref(autoderef), target })
205 InferOk { obligations, value: steps }
208 /// also dereference through raw pointer types
209 /// e.g., assuming ptr_to_Foo is the type `*const Foo`
210 /// fcx.autoderef(span, ptr_to_Foo) => [*const Foo]
211 /// fcx.autoderef(span, ptr_to_Foo).include_raw_ptrs() => [*const Foo, Foo]
212 pub fn include_raw_pointers(mut self) -> Self {
213 self.include_raw_pointers = true;
217 pub fn silence_errors(mut self) -> Self {
218 self.silence_errors = true;
222 pub fn reached_recursion_limit(&self) -> bool {
223 self.reached_recursion_limit
226 pub fn finalize(self, fcx: &FnCtxt<'a, 'tcx>) {
227 fcx.register_predicates(self.into_obligations());
230 pub fn into_obligations(self) -> Vec<traits::PredicateObligation<'tcx>> {
235 pub fn report_autoderef_recursion_limit_error<'tcx>(tcx: TyCtxt<'tcx>, span: Span, ty: Ty<'tcx>) {
236 // We've reached the recursion limit, error gracefully.
237 let suggested_limit = *tcx.sess.recursion_limit.get() * 2;
238 let msg = format!("reached the recursion limit while auto-dereferencing `{:?}`", ty);
239 let error_id = (DiagnosticMessageId::ErrorId(55), Some(span), msg);
240 let fresh = tcx.sess.one_time_diagnostics.borrow_mut().insert(error_id);
246 "reached the recursion limit while auto-dereferencing `{:?}`",
249 .span_label(span, "deref recursion limit reached")
251 "consider adding a `#![recursion_limit=\"{}\"]` attribute to your crate",
258 impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
259 pub fn autoderef(&'a self, span: Span, base_ty: Ty<'tcx>) -> Autoderef<'a, 'tcx> {
260 Autoderef::new(self, self.param_env, self.body_id, span, base_ty)
263 pub fn try_overloaded_deref(
268 ) -> Option<InferOk<'tcx, MethodCallee<'tcx>>> {
269 self.try_overloaded_place_op(span, base_ty, &[], needs, PlaceOp::Deref)