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, WithConstness};
9 use rustc::ty::{ToPredicate, TypeFoldable};
10 use rustc_errors::struct_span_err;
14 use syntax::ast::Ident;
18 #[derive(Copy, Clone, Debug)]
24 pub struct Autoderef<'a, 'tcx> {
25 infcx: &'a InferCtxt<'a, 'tcx>,
27 param_env: ty::ParamEnv<'tcx>,
28 steps: Vec<(Ty<'tcx>, AutoderefKind)>,
30 obligations: Vec<traits::PredicateObligation<'tcx>>,
32 include_raw_pointers: bool,
35 reached_recursion_limit: bool,
38 impl<'a, 'tcx> Iterator for Autoderef<'a, 'tcx> {
39 type Item = (Ty<'tcx>, usize);
41 fn next(&mut self) -> Option<Self::Item> {
42 let tcx = self.infcx.tcx;
44 debug!("autoderef: steps={:?}, cur_ty={:?}", self.steps, self.cur_ty);
46 self.at_start = false;
47 debug!("autoderef stage #0 is {:?}", self.cur_ty);
48 return Some((self.cur_ty, 0));
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);
55 self.reached_recursion_limit = true;
59 if self.cur_ty.is_ty_var() {
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)
66 (AutoderefKind::Builtin, mt.ty)
68 let ty = self.overloaded_deref_ty(self.cur_ty)?;
69 (AutoderefKind::Overloaded, ty)
72 if new_ty.references_error() {
76 self.steps.push((self.cur_ty, kind));
78 "autoderef stage #{:?} is {:?} from {:?}",
85 Some((self.cur_ty, self.steps.len()))
89 impl<'a, 'tcx> Autoderef<'a, 'tcx> {
91 infcx: &'a InferCtxt<'a, 'tcx>,
92 param_env: ty::ParamEnv<'tcx>,
96 ) -> Autoderef<'a, 'tcx> {
102 cur_ty: infcx.resolve_vars_if_possible(&base_ty),
105 include_raw_pointers: false,
106 silence_errors: false,
107 reached_recursion_limit: false,
112 fn overloaded_deref_ty(&mut self, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
113 debug!("overloaded_deref_ty({:?})", ty);
115 let tcx = self.infcx.tcx;
118 let trait_ref = TraitRef {
119 def_id: tcx.lang_items().deref_trait()?,
120 substs: tcx.mk_substs_trait(self.cur_ty, &[]),
123 let cause = traits::ObligationCause::misc(self.span, self.body_id);
125 let obligation = traits::Obligation::new(
128 trait_ref.without_const().to_predicate(),
130 if !self.infcx.predicate_may_hold(&obligation) {
131 debug!("overloaded_deref_ty: cannot match obligation");
135 let mut fulfillcx = traits::FulfillmentContext::new_in_snapshot();
136 let normalized_ty = fulfillcx.normalize_projection_type(
139 ty::ProjectionTy::from_ref_and_name(tcx, trait_ref, Ident::from_str("Target")),
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
146 debug!("overloaded_deref_ty: encountered errors {:?} while fulfilling", e);
149 let obligations = fulfillcx.pending_obligations();
150 debug!("overloaded_deref_ty({:?}) = ({:?}, {:?})", ty, normalized_ty, obligations);
151 self.obligations.extend(obligations);
153 Some(self.infcx.resolve_vars_if_possible(&normalized_ty))
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)
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)
168 pub fn step_count(&self) -> usize {
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))
177 pub fn adjust_steps_as_infer_ok(
179 fcx: &FnCtxt<'a, 'tcx>,
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
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 })
204 .map(|(autoderef, target)| Adjustment { kind: Adjust::Deref(autoderef), target })
207 InferOk { obligations, value: steps }
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;
219 pub fn silence_errors(mut self) -> Self {
220 self.silence_errors = true;
224 pub fn reached_recursion_limit(&self) -> bool {
225 self.reached_recursion_limit
228 pub fn finalize(self, fcx: &FnCtxt<'a, 'tcx>) {
229 fcx.register_predicates(self.into_obligations());
232 pub fn into_obligations(self) -> Vec<traits::PredicateObligation<'tcx>> {
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);
248 "reached the recursion limit while auto-dereferencing `{:?}`",
251 .span_label(span, "deref recursion limit reached")
253 "consider adding a `#![recursion_limit=\"{}\"]` attribute to your crate",
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)
265 pub fn try_overloaded_deref(
270 ) -> Option<InferOk<'tcx, MethodCallee<'tcx>>> {
271 self.try_overloaded_place_op(span, base_ty, &[], needs, PlaceOp::Deref)