Skip to main content

hir_ty/next_solver/ty/
erase_regions.rs

1use rustc_type_ir::{
2    Interner, TypeFlags, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitableExt,
3    inherent::IntoKind as _,
4};
5use tracing::debug;
6
7use crate::next_solver::{Binder, Clauses, Const, DbInterner, Predicate, Region, RegionKind, Ty};
8
9impl<'db> DbInterner<'db> {
10    pub fn erase_and_anonymize_regions_ty(self, ty: Ty<'db>) -> Ty<'db> {
11        // N.B., use `super_fold_with` here. If we used `fold_with`, it
12        // could invoke the `erase_and_anonymize_regions_ty` query recursively.
13        ty.super_fold_with(&mut RegionEraserAndAnonymizerVisitor { interner: self })
14    }
15
16    /// Returns an equivalent value with all free regions removed and
17    /// bound regions anonymized. (note that bound regions are important
18    /// for subtyping and generally type equality so *cannot* be removed)
19    pub fn erase_and_anonymize_regions<T>(self, value: T) -> T
20    where
21        T: TypeFoldable<DbInterner<'db>>,
22    {
23        // If there's nothing to erase or anonymize, avoid performing the query at all
24        if !value.has_type_flags(TypeFlags::HAS_BINDER_VARS | TypeFlags::HAS_FREE_REGIONS) {
25            return value;
26        }
27        debug!("erase_and_anonymize_regions({:?})", value);
28        let value1 = value.fold_with(&mut RegionEraserAndAnonymizerVisitor { interner: self });
29        debug!("erase_and_anonymize_regions = {:?}", value1);
30        value1
31    }
32}
33
34struct RegionEraserAndAnonymizerVisitor<'db> {
35    interner: DbInterner<'db>,
36}
37
38impl<'db> TypeFolder<DbInterner<'db>> for RegionEraserAndAnonymizerVisitor<'db> {
39    fn cx(&self) -> DbInterner<'db> {
40        self.interner
41    }
42
43    fn fold_ty(&mut self, ty: Ty<'db>) -> Ty<'db> {
44        if !ty.has_type_flags(TypeFlags::HAS_BINDER_VARS | TypeFlags::HAS_FREE_REGIONS) {
45            ty
46        } else if ty.has_infer() {
47            ty.super_fold_with(self)
48        } else {
49            self.interner.erase_and_anonymize_regions_ty(ty)
50        }
51    }
52
53    fn fold_binder<T>(&mut self, t: Binder<'db, T>) -> Binder<'db, T>
54    where
55        T: TypeFoldable<DbInterner<'db>>,
56    {
57        let u = self.interner.anonymize_bound_vars(t);
58        u.super_fold_with(self)
59    }
60
61    fn fold_region(&mut self, r: Region<'db>) -> Region<'db> {
62        // We must not erase bound regions. `for<'a> fn(&'a ())` and
63        // `fn(&'free ())` are different types: they may implement different
64        // traits and have a different `TypeId`.
65        match r.kind() {
66            RegionKind::ReBound(..) => r,
67            _ => self.interner.default_types().regions.erased,
68        }
69    }
70
71    fn fold_const(&mut self, ct: Const<'db>) -> Const<'db> {
72        if ct.has_type_flags(TypeFlags::HAS_BINDER_VARS | TypeFlags::HAS_FREE_REGIONS) {
73            ct.super_fold_with(self)
74        } else {
75            ct
76        }
77    }
78
79    fn fold_predicate(&mut self, p: Predicate<'db>) -> Predicate<'db> {
80        if p.has_type_flags(TypeFlags::HAS_BINDER_VARS | TypeFlags::HAS_FREE_REGIONS) {
81            p.super_fold_with(self)
82        } else {
83            p
84        }
85    }
86
87    fn fold_clauses(&mut self, c: Clauses<'db>) -> Clauses<'db> {
88        if c.has_type_flags(TypeFlags::HAS_BINDER_VARS | TypeFlags::HAS_FREE_REGIONS) {
89            c.super_fold_with(self)
90        } else {
91            c
92        }
93    }
94}