Skip to main content

rustc_middle/ty/
erase_regions.rs

1use tracing::debug;
2
3use crate::query::Providers;
4use crate::ty::{
5    self, Ty, TyCtxt, TypeFlags, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitableExt,
6};
7
8pub(super) fn provide(providers: &mut Providers) {
9    *providers = Providers { erase_and_anonymize_regions_ty, ..*providers };
10}
11
12fn erase_and_anonymize_regions_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> {
13    // N.B., use `super_fold_with` here. If we used `fold_with`, it
14    // could invoke the `erase_and_anonymize_regions_ty` query recursively.
15    ty.super_fold_with(&mut RegionEraserAndAnonymizerVisitor { tcx })
16}
17
18impl<'tcx> TyCtxt<'tcx> {
19    /// Returns an equivalent value with all free regions removed and
20    /// bound regions anonymized. (note that bound regions are important
21    /// for subtyping and generally type equality so *cannot* be removed)
22    pub fn erase_and_anonymize_regions<T>(self, value: T) -> T
23    where
24        T: TypeFoldable<TyCtxt<'tcx>>,
25    {
26        // If there's nothing to erase or anonymize, avoid performing the query at all
27        if !value.has_type_flags(TypeFlags::HAS_BINDER_VARS | TypeFlags::HAS_FREE_REGIONS) {
28            return value;
29        }
30        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_middle/src/ty/erase_regions.rs:30",
                        "rustc_middle::ty::erase_regions", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_middle/src/ty/erase_regions.rs"),
                        ::tracing_core::__macro_support::Option::Some(30u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_middle::ty::erase_regions"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("erase_and_anonymize_regions({0:?})",
                                                    value) as &dyn Value))])
            });
    } else { ; }
};debug!("erase_and_anonymize_regions({:?})", value);
31        let value1 = value.fold_with(&mut RegionEraserAndAnonymizerVisitor { tcx: self });
32        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_middle/src/ty/erase_regions.rs:32",
                        "rustc_middle::ty::erase_regions", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_middle/src/ty/erase_regions.rs"),
                        ::tracing_core::__macro_support::Option::Some(32u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_middle::ty::erase_regions"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("erase_and_anonymize_regions = {0:?}",
                                                    value1) as &dyn Value))])
            });
    } else { ; }
};debug!("erase_and_anonymize_regions = {:?}", value1);
33        value1
34    }
35}
36
37struct RegionEraserAndAnonymizerVisitor<'tcx> {
38    tcx: TyCtxt<'tcx>,
39}
40
41impl<'tcx> TypeFolder<TyCtxt<'tcx>> for RegionEraserAndAnonymizerVisitor<'tcx> {
42    fn cx(&self) -> TyCtxt<'tcx> {
43        self.tcx
44    }
45
46    fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
47        if !ty.has_type_flags(TypeFlags::HAS_BINDER_VARS | TypeFlags::HAS_FREE_REGIONS) {
48            ty
49        } else if ty.has_infer() {
50            ty.super_fold_with(self)
51        } else {
52            self.tcx.erase_and_anonymize_regions_ty(ty)
53        }
54    }
55
56    fn fold_binder<T>(&mut self, t: ty::Binder<'tcx, T>) -> ty::Binder<'tcx, T>
57    where
58        T: TypeFoldable<TyCtxt<'tcx>>,
59    {
60        let u = self.tcx.anonymize_bound_vars(t);
61        u.super_fold_with(self)
62    }
63
64    fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
65        // We must not erase bound regions. `for<'a> fn(&'a ())` and
66        // `fn(&'free ())` are different types: they may implement different
67        // traits and have a different `TypeId`.
68        match r.kind() {
69            ty::ReBound(..) => r,
70            _ => self.tcx.lifetimes.re_erased,
71        }
72    }
73
74    fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
75        if ct.has_type_flags(TypeFlags::HAS_BINDER_VARS | TypeFlags::HAS_FREE_REGIONS) {
76            ct.super_fold_with(self)
77        } else {
78            ct
79        }
80    }
81
82    fn fold_predicate(&mut self, p: ty::Predicate<'tcx>) -> ty::Predicate<'tcx> {
83        if p.has_type_flags(TypeFlags::HAS_BINDER_VARS | TypeFlags::HAS_FREE_REGIONS) {
84            p.super_fold_with(self)
85        } else {
86            p
87        }
88    }
89
90    fn fold_clauses(&mut self, c: ty::Clauses<'tcx>) -> ty::Clauses<'tcx> {
91        if c.has_type_flags(TypeFlags::HAS_BINDER_VARS | TypeFlags::HAS_FREE_REGIONS) {
92            c.super_fold_with(self)
93        } else {
94            c
95        }
96    }
97}