rustc_middle/ty/
erase_regions.rs1use 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 ty.super_fold_with(&mut RegionEraserVisitor { tcx })
15}
16
17impl<'tcx> TyCtxt<'tcx> {
18 pub fn erase_regions<T>(self, value: T) -> T
22 where
23 T: TypeFoldable<TyCtxt<'tcx>>,
24 {
25 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 match *r {
62 ty::ReBound(..) => r,
63 _ => self.tcx.lifetimes.re_erased,
64 }
65 }
66}