1 use crate::errors::AutoDerefReachedRecursionLimit;
2 use crate::traits::query::evaluate_obligation::InferCtxtExt;
3 use crate::traits::{self, TraitEngine, TraitEngineExt};
5 use rustc_infer::infer::InferCtxt;
6 use rustc_middle::ty::TypeVisitable;
7 use rustc_middle::ty::{self, Ty, TyCtxt};
8 use rustc_session::Limit;
9 use rustc_span::def_id::LOCAL_CRATE;
12 #[derive(Copy, Clone, Debug)]
13 pub enum AutoderefKind {
18 struct AutoderefSnapshot<'tcx> {
20 reached_recursion_limit: bool,
21 steps: Vec<(Ty<'tcx>, AutoderefKind)>,
23 obligations: Vec<traits::PredicateObligation<'tcx>>,
26 pub struct Autoderef<'a, 'tcx> {
28 infcx: &'a InferCtxt<'tcx>,
31 param_env: ty::ParamEnv<'tcx>,
34 state: AutoderefSnapshot<'tcx>,
37 include_raw_pointers: bool,
41 impl<'a, 'tcx> Iterator for Autoderef<'a, 'tcx> {
42 type Item = (Ty<'tcx>, usize);
44 fn next(&mut self) -> Option<Self::Item> {
45 let tcx = self.infcx.tcx;
47 debug!("autoderef: steps={:?}, cur_ty={:?}", self.state.steps, self.state.cur_ty);
48 if self.state.at_start {
49 self.state.at_start = false;
50 debug!("autoderef stage #0 is {:?}", self.state.cur_ty);
51 return Some((self.state.cur_ty, 0));
54 // If we have reached the recursion limit, error gracefully.
55 if !tcx.recursion_limit().value_within_limit(self.state.steps.len()) {
56 if !self.silence_errors {
57 report_autoderef_recursion_limit_error(tcx, self.span, self.state.cur_ty);
59 self.state.reached_recursion_limit = true;
63 if self.state.cur_ty.is_ty_var() {
67 // Otherwise, deref if type is derefable:
69 if let Some(mt) = self.state.cur_ty.builtin_deref(self.include_raw_pointers) {
70 (AutoderefKind::Builtin, mt.ty)
71 } else if let Some(ty) = self.overloaded_deref_ty(self.state.cur_ty) {
72 (AutoderefKind::Overloaded, ty)
77 if new_ty.references_error() {
81 self.state.steps.push((self.state.cur_ty, kind));
83 "autoderef stage #{:?} is {:?} from {:?}",
86 (self.state.cur_ty, kind)
88 self.state.cur_ty = new_ty;
90 Some((self.state.cur_ty, self.step_count()))
94 impl<'a, 'tcx> Autoderef<'a, 'tcx> {
96 infcx: &'a InferCtxt<'tcx>,
97 param_env: ty::ParamEnv<'tcx>,
101 ) -> Autoderef<'a, 'tcx> {
107 state: AutoderefSnapshot {
109 cur_ty: infcx.resolve_vars_if_possible(base_ty),
112 reached_recursion_limit: false,
114 include_raw_pointers: false,
115 silence_errors: false,
119 fn overloaded_deref_ty(&mut self, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
120 debug!("overloaded_deref_ty({:?})", ty);
122 let tcx = self.infcx.tcx;
125 let trait_ref = tcx.mk_trait_ref(tcx.lang_items().deref_trait()?, ty, &[]);
127 let cause = traits::ObligationCause::misc(self.span, self.body_id);
129 let obligation = traits::Obligation::new(
133 ty::Binder::dummy(trait_ref).without_const(),
135 if !self.infcx.predicate_may_hold(&obligation) {
136 debug!("overloaded_deref_ty: cannot match obligation");
140 let mut fulfillcx = <dyn TraitEngine<'tcx>>::new_in_snapshot(tcx);
141 let normalized_ty = fulfillcx.normalize_projection_type(
145 item_def_id: tcx.lang_items().deref_target()?,
146 substs: trait_ref.substs,
150 let errors = fulfillcx.select_where_possible(&self.infcx);
151 if !errors.is_empty() {
152 // This shouldn't happen, except for evaluate/fulfill mismatches,
153 // but that's not a reason for an ICE (`predicate_may_hold` is conservative
155 debug!("overloaded_deref_ty: encountered errors {:?} while fulfilling", errors);
158 let obligations = fulfillcx.pending_obligations();
159 debug!("overloaded_deref_ty({:?}) = ({:?}, {:?})", ty, normalized_ty, obligations);
160 self.state.obligations.extend(obligations);
162 Some(self.infcx.resolve_vars_if_possible(normalized_ty))
165 /// Returns the final type we ended up with, which may be an inference
166 /// variable (we will resolve it first, if we want).
167 pub fn final_ty(&self, resolve: bool) -> Ty<'tcx> {
169 self.infcx.resolve_vars_if_possible(self.state.cur_ty)
175 pub fn step_count(&self) -> usize {
176 self.state.steps.len()
179 pub fn into_obligations(self) -> Vec<traits::PredicateObligation<'tcx>> {
180 self.state.obligations
183 pub fn steps(&self) -> &[(Ty<'tcx>, AutoderefKind)] {
187 pub fn span(&self) -> Span {
191 pub fn reached_recursion_limit(&self) -> bool {
192 self.state.reached_recursion_limit
195 /// also dereference through raw pointer types
196 /// e.g., assuming ptr_to_Foo is the type `*const Foo`
197 /// fcx.autoderef(span, ptr_to_Foo) => [*const Foo]
198 /// fcx.autoderef(span, ptr_to_Foo).include_raw_ptrs() => [*const Foo, Foo]
199 pub fn include_raw_pointers(mut self) -> Self {
200 self.include_raw_pointers = true;
204 pub fn silence_errors(mut self) -> Self {
205 self.silence_errors = true;
210 pub fn report_autoderef_recursion_limit_error<'tcx>(tcx: TyCtxt<'tcx>, span: Span, ty: Ty<'tcx>) {
211 // We've reached the recursion limit, error gracefully.
212 let suggested_limit = match tcx.recursion_limit() {
213 Limit(0) => Limit(2),
216 tcx.sess.emit_err(AutoDerefReachedRecursionLimit {
220 crate_name: tcx.crate_name(LOCAL_CRATE),