rustc_middle/ty/
erase_regions.rs

1use tracing::debug;
2
3use crate::query::Providers;
4use crate::ty::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable};
5use crate::ty::{self, Ty, TyCtxt, TypeFlags, TypeVisitableExt};
6
7pub(super) fn provide(providers: &mut Providers) {
8    *providers = Providers { erase_regions_ty, ..*providers };
9}
10
11fn erase_regions_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> {
12    // N.B., use `super_fold_with` here. If we used `fold_with`, it
13    // could invoke the `erase_regions_ty` query recursively.
14    ty.super_fold_with(&mut RegionEraserVisitor { tcx })
15}
16
17impl<'tcx> TyCtxt<'tcx> {
18    /// Returns an equivalent value with all free regions removed (note
19    /// that late-bound regions remain, because they are important for
20    /// subtyping, but they are anonymized and normalized as well)..
21    pub fn erase_regions<T>(self, value: T) -> T
22    where
23        T: TypeFoldable<TyCtxt<'tcx>>,
24    {
25        // If there's nothing to erase or anonymize, avoid performing the query at all
26        if !value.has_type_flags(TypeFlags::HAS_BINDER_VARS | TypeFlags::HAS_FREE_REGIONS) {
27            return value;
28        }
29        debug!("erase_regions({:?})", value);
30        let value1 = value.fold_with(&mut RegionEraserVisitor { tcx: self });
31        debug!("erase_regions = {:?}", value1);
32        value1
33    }
34}
35
36struct RegionEraserVisitor<'tcx> {
37    tcx: TyCtxt<'tcx>,
38}
39
40impl<'tcx> TypeFolder<TyCtxt<'tcx>> for RegionEraserVisitor<'tcx> {
41    fn cx(&self) -> TyCtxt<'tcx> {
42        self.tcx
43    }
44
45    fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
46        if ty.has_infer() { ty.super_fold_with(self) } else { self.tcx.erase_regions_ty(ty) }
47    }
48
49    fn fold_binder<T>(&mut self, t: ty::Binder<'tcx, T>) -> ty::Binder<'tcx, T>
50    where
51        T: TypeFoldable<TyCtxt<'tcx>>,
52    {
53        let u = self.tcx.anonymize_bound_vars(t);
54        u.super_fold_with(self)
55    }
56
57    fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
58        // We must not erase bound regions. `for<'a> fn(&'a ())` and
59        // `fn(&'free ())` are different types: they may implement different
60        // traits and have a different `TypeId`.
61        match *r {
62            ty::ReBound(..) => r,
63            _ => self.tcx.lifetimes.re_erased,
64        }
65    }
66}