rustc_hir_analysis/
autoderef.rs

1use rustc_infer::infer::InferCtxt;
2use rustc_infer::traits::PredicateObligations;
3use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt};
4use rustc_session::Limit;
5use rustc_span::Span;
6use rustc_span::def_id::{LOCAL_CRATE, LocalDefId};
7use rustc_trait_selection::traits::ObligationCtxt;
8use tracing::{debug, instrument};
9
10use crate::errors::AutoDerefReachedRecursionLimit;
11use crate::traits;
12use crate::traits::query::evaluate_obligation::InferCtxtExt;
13
14#[derive(Copy, Clone, Debug)]
15pub enum AutoderefKind {
16    /// A true pointer type, such as `&T` and `*mut T`.
17    Builtin,
18    /// A type which must dispatch to a `Deref` implementation.
19    Overloaded,
20}
21struct AutoderefSnapshot<'tcx> {
22    at_start: bool,
23    reached_recursion_limit: bool,
24    steps: Vec<(Ty<'tcx>, AutoderefKind)>,
25    cur_ty: Ty<'tcx>,
26    obligations: PredicateObligations<'tcx>,
27}
28
29/// Recursively dereference a type, considering both built-in
30/// dereferences (`*`) and the `Deref` trait.
31/// Although called `Autoderef` it can be configured to use the
32/// `Receiver` trait instead of the `Deref` trait.
33pub struct Autoderef<'a, 'tcx> {
34    // Meta infos:
35    infcx: &'a InferCtxt<'tcx>,
36    span: Span,
37    body_id: LocalDefId,
38    param_env: ty::ParamEnv<'tcx>,
39
40    // Current state:
41    state: AutoderefSnapshot<'tcx>,
42
43    // Configurations:
44    include_raw_pointers: bool,
45    use_receiver_trait: bool,
46    silence_errors: bool,
47}
48
49impl<'a, 'tcx> Iterator for Autoderef<'a, 'tcx> {
50    type Item = (Ty<'tcx>, usize);
51
52    fn next(&mut self) -> Option<Self::Item> {
53        let tcx = self.infcx.tcx;
54
55        debug!("autoderef: steps={:?}, cur_ty={:?}", self.state.steps, self.state.cur_ty);
56        if self.state.at_start {
57            self.state.at_start = false;
58            debug!("autoderef stage #0 is {:?}", self.state.cur_ty);
59            return Some((self.state.cur_ty, 0));
60        }
61
62        // If we have reached the recursion limit, error gracefully.
63        if !tcx.recursion_limit().value_within_limit(self.state.steps.len()) {
64            if !self.silence_errors {
65                report_autoderef_recursion_limit_error(tcx, self.span, self.state.cur_ty);
66            }
67            self.state.reached_recursion_limit = true;
68            return None;
69        }
70
71        if self.state.cur_ty.is_ty_var() {
72            return None;
73        }
74
75        // Otherwise, deref if type is derefable:
76        // NOTE: in the case of self.use_receiver_trait = true, you might think it would
77        // be better to skip this clause and use the Overloaded case only, since &T
78        // and &mut T implement Receiver. But built-in derefs apply equally to Receiver
79        // and Deref, and this has benefits for const and the emitted MIR.
80        let (kind, new_ty) =
81            if let Some(ty) = self.state.cur_ty.builtin_deref(self.include_raw_pointers) {
82                debug_assert_eq!(ty, self.infcx.resolve_vars_if_possible(ty));
83                // NOTE: we may still need to normalize the built-in deref in case
84                // we have some type like `&<Ty as Trait>::Assoc`, since users of
85                // autoderef expect this type to have been structurally normalized.
86                if self.infcx.next_trait_solver()
87                    && let ty::Alias(..) = ty.kind()
88                {
89                    let (normalized_ty, obligations) = self.structurally_normalize_ty(ty)?;
90                    self.state.obligations.extend(obligations);
91                    (AutoderefKind::Builtin, normalized_ty)
92                } else {
93                    (AutoderefKind::Builtin, ty)
94                }
95            } else if let Some(ty) = self.overloaded_deref_ty(self.state.cur_ty) {
96                // The overloaded deref check already normalizes the pointee type.
97                (AutoderefKind::Overloaded, ty)
98            } else {
99                return None;
100            };
101
102        self.state.steps.push((self.state.cur_ty, kind));
103        debug!(
104            "autoderef stage #{:?} is {:?} from {:?}",
105            self.step_count(),
106            new_ty,
107            (self.state.cur_ty, kind)
108        );
109        self.state.cur_ty = new_ty;
110
111        Some((self.state.cur_ty, self.step_count()))
112    }
113}
114
115impl<'a, 'tcx> Autoderef<'a, 'tcx> {
116    pub fn new(
117        infcx: &'a InferCtxt<'tcx>,
118        param_env: ty::ParamEnv<'tcx>,
119        body_def_id: LocalDefId,
120        span: Span,
121        base_ty: Ty<'tcx>,
122    ) -> Self {
123        Autoderef {
124            infcx,
125            span,
126            body_id: body_def_id,
127            param_env,
128            state: AutoderefSnapshot {
129                steps: vec![],
130                cur_ty: infcx.resolve_vars_if_possible(base_ty),
131                obligations: PredicateObligations::new(),
132                at_start: true,
133                reached_recursion_limit: false,
134            },
135            include_raw_pointers: false,
136            use_receiver_trait: false,
137            silence_errors: false,
138        }
139    }
140
141    fn overloaded_deref_ty(&mut self, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
142        debug!("overloaded_deref_ty({:?})", ty);
143        let tcx = self.infcx.tcx;
144
145        if ty.references_error() {
146            return None;
147        }
148
149        // <ty as Deref>, or whatever the equivalent trait is that we've been asked to walk.
150        let (trait_def_id, trait_target_def_id) = if self.use_receiver_trait {
151            (tcx.lang_items().receiver_trait()?, tcx.lang_items().receiver_target()?)
152        } else {
153            (tcx.lang_items().deref_trait()?, tcx.lang_items().deref_target()?)
154        };
155        let trait_ref = ty::TraitRef::new(tcx, trait_def_id, [ty]);
156        let cause = traits::ObligationCause::misc(self.span, self.body_id);
157        let obligation = traits::Obligation::new(
158            tcx,
159            cause.clone(),
160            self.param_env,
161            ty::Binder::dummy(trait_ref),
162        );
163        if !self.infcx.predicate_may_hold(&obligation) {
164            debug!("overloaded_deref_ty: cannot match obligation");
165            return None;
166        }
167
168        let (normalized_ty, obligations) =
169            self.structurally_normalize_ty(Ty::new_projection(tcx, trait_target_def_id, [ty]))?;
170        debug!("overloaded_deref_ty({:?}) = ({:?}, {:?})", ty, normalized_ty, obligations);
171        self.state.obligations.extend(obligations);
172
173        Some(self.infcx.resolve_vars_if_possible(normalized_ty))
174    }
175
176    #[instrument(level = "debug", skip(self), ret)]
177    pub fn structurally_normalize_ty(
178        &self,
179        ty: Ty<'tcx>,
180    ) -> Option<(Ty<'tcx>, PredicateObligations<'tcx>)> {
181        let ocx = ObligationCtxt::new(self.infcx);
182        let Ok(normalized_ty) = ocx.structurally_normalize_ty(
183            &traits::ObligationCause::misc(self.span, self.body_id),
184            self.param_env,
185            ty,
186        ) else {
187            // We shouldn't have errors here, except for evaluate/fulfill mismatches,
188            // but that's not a reason for an ICE (`predicate_may_hold` is conservative
189            // by design).
190            // FIXME(-Znext-solver): This *actually* shouldn't happen then.
191            return None;
192        };
193        let errors = ocx.select_where_possible();
194        if !errors.is_empty() {
195            // This shouldn't happen, except for evaluate/fulfill mismatches,
196            // but that's not a reason for an ICE (`predicate_may_hold` is conservative
197            // by design).
198            debug!(?errors, "encountered errors while fulfilling");
199            return None;
200        }
201
202        Some((normalized_ty, ocx.into_pending_obligations()))
203    }
204
205    /// Returns the final type we ended up with, which may be an inference
206    /// variable (we will resolve it first, if we want).
207    pub fn final_ty(&self, resolve: bool) -> Ty<'tcx> {
208        if resolve {
209            self.infcx.resolve_vars_if_possible(self.state.cur_ty)
210        } else {
211            self.state.cur_ty
212        }
213    }
214
215    pub fn step_count(&self) -> usize {
216        self.state.steps.len()
217    }
218
219    pub fn into_obligations(self) -> PredicateObligations<'tcx> {
220        self.state.obligations
221    }
222
223    pub fn current_obligations(&self) -> PredicateObligations<'tcx> {
224        self.state.obligations.clone()
225    }
226
227    pub fn steps(&self) -> &[(Ty<'tcx>, AutoderefKind)] {
228        &self.state.steps
229    }
230
231    pub fn span(&self) -> Span {
232        self.span
233    }
234
235    pub fn reached_recursion_limit(&self) -> bool {
236        self.state.reached_recursion_limit
237    }
238
239    /// also dereference through raw pointer types
240    /// e.g., assuming ptr_to_Foo is the type `*const Foo`
241    /// fcx.autoderef(span, ptr_to_Foo)  => [*const Foo]
242    /// fcx.autoderef(span, ptr_to_Foo).include_raw_ptrs() => [*const Foo, Foo]
243    pub fn include_raw_pointers(mut self) -> Self {
244        self.include_raw_pointers = true;
245        self
246    }
247
248    /// Use `core::ops::Receiver` and `core::ops::Receiver::Target` as
249    /// the trait and associated type to iterate, instead of
250    /// `core::ops::Deref` and `core::ops::Deref::Target`
251    pub fn use_receiver_trait(mut self) -> Self {
252        self.use_receiver_trait = true;
253        self
254    }
255
256    pub fn silence_errors(mut self) -> Self {
257        self.silence_errors = true;
258        self
259    }
260}
261
262pub fn report_autoderef_recursion_limit_error<'tcx>(tcx: TyCtxt<'tcx>, span: Span, ty: Ty<'tcx>) {
263    // We've reached the recursion limit, error gracefully.
264    let suggested_limit = match tcx.recursion_limit() {
265        Limit(0) => Limit(2),
266        limit => limit * 2,
267    };
268    tcx.dcx().emit_err(AutoDerefReachedRecursionLimit {
269        span,
270        ty,
271        suggested_limit,
272        crate_name: tcx.crate_name(LOCAL_CRATE),
273    });
274}