1 use super::{FnCtxt, PlaceOp, Needs};
2 use super::method::MethodCallee;
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};
12 use syntax::ast::{self, Ident};
16 #[derive(Copy, Clone, Debug)]
22 pub struct Autoderef<'a, 'gcx: 'tcx, 'tcx: 'a> {
23 infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
25 param_env: ty::ParamEnv<'tcx>,
26 steps: Vec<(Ty<'tcx>, AutoderefKind)>,
28 obligations: Vec<traits::PredicateObligation<'tcx>>,
30 include_raw_pointers: bool,
33 reached_recursion_limit: bool
36 impl<'a, 'gcx, 'tcx> Iterator for Autoderef<'a, 'gcx, 'tcx> {
37 type Item = (Ty<'tcx>, usize);
39 fn next(&mut self) -> Option<Self::Item> {
40 let tcx = self.infcx.tcx;
42 debug!("autoderef: steps={:?}, 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:
65 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));
77 debug!("autoderef stage #{:?} is {:?} from {:?}",
83 Some((self.cur_ty, self.steps.len()))
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>,
93 -> Autoderef<'a, 'gcx, 'tcx>
100 cur_ty: infcx.resolve_type_vars_if_possible(&base_ty),
103 include_raw_pointers: false,
104 silence_errors: false,
105 reached_recursion_limit: false,
110 fn overloaded_deref_ty(&mut self, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
111 debug!("overloaded_deref_ty({:?})", ty);
113 let tcx = self.infcx.tcx;
116 let trait_ref = TraitRef {
117 def_id: tcx.lang_items().deref_trait()?,
118 substs: tcx.mk_substs_trait(self.cur_ty, &[]),
121 let cause = traits::ObligationCause::misc(self.span, self.body_id);
123 let obligation = traits::Obligation::new(cause.clone(),
125 trait_ref.to_predicate());
126 if !self.infcx.predicate_may_hold(&obligation) {
127 debug!("overloaded_deref_ty: cannot match obligation");
131 let mut fulfillcx = traits::FulfillmentContext::new_in_snapshot();
132 let normalized_ty = fulfillcx.normalize_projection_type(
135 ty::ProjectionTy::from_ref_and_name(
138 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",
149 let obligations = fulfillcx.pending_obligations();
150 debug!("overloaded_deref_ty({:?}) = ({:?}, {:?})",
151 ty, normalized_ty, obligations);
152 self.obligations.extend(obligations);
154 Some(self.infcx.resolve_type_vars_if_possible(&normalized_ty))
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)
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)
169 pub fn step_count(&self) -> usize {
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))
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 {
201 }).zip(targets).map(|(autoderef, target)| {
203 kind: Adjust::Deref(autoderef),
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;
223 pub fn silence_errors(mut self) -> Self {
224 self.silence_errors = true;
228 pub fn reached_recursion_limit(&self) -> bool {
229 self.reached_recursion_limit
232 pub fn finalize(self, fcx: &FnCtxt<'a, 'gcx, 'tcx>) {
233 fcx.register_predicates(self.into_obligations());
236 pub fn into_obligations(self) -> Vec<traits::PredicateObligation<'tcx>> {
241 pub fn report_autoderef_recursion_limit_error<'a, 'gcx, 'tcx>(
242 tcx: TyCtxt<'a, 'gcx, 'tcx>, span: Span, ty: Ty<'tcx>)
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 `{:?}`",
248 let error_id = (DiagnosticMessageId::ErrorId(55), Some(span), msg);
249 let fresh = tcx.sess.one_time_diagnostics.borrow_mut().insert(error_id);
251 struct_span_err!(tcx.sess,
254 "reached the recursion limit while auto-dereferencing `{:?}`",
256 .span_label(span, "deref recursion limit reached")
258 "consider adding a `#![recursion_limit=\"{}\"]` attribute to your crate",
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)
269 pub fn try_overloaded_deref(&self,
273 -> Option<InferOk<'tcx, MethodCallee<'tcx>>> {
274 self.try_overloaded_place_op(span, base_ty, &[], needs, PlaceOp::Deref)