2 use crate::ty::fold::{TypeFoldable, TypeFolder};
3 use crate::ty::{self, Ty, TyCtxt, TypeFlags};
5 pub(super) fn provide(providers: &mut ty::query::Providers) {
6 *providers = ty::query::Providers { erase_regions_ty, ..*providers };
9 fn erase_regions_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> {
10 // N.B., use `super_fold_with` here. If we used `fold_with`, it
11 // could invoke the `erase_regions_ty` query recursively.
12 ty.super_fold_with(&mut RegionEraserVisitor { tcx }).into_ok()
15 impl<'tcx> TyCtxt<'tcx> {
16 /// Returns an equivalent value with all free regions removed (note
17 /// that late-bound regions remain, because they are important for
18 /// subtyping, but they are anonymized and normalized as well)..
19 pub fn erase_regions<T>(self, value: T) -> T
21 T: TypeFoldable<'tcx>,
23 // If there's nothing to erase avoid performing the query at all
25 .has_type_flags(TypeFlags::HAS_RE_LATE_BOUND | TypeFlags::HAS_POTENTIAL_FREE_REGIONS)
29 debug!("erase_regions({:?})", value);
30 let value1 = value.fold_with(&mut RegionEraserVisitor { tcx: self }).into_ok();
31 debug!("erase_regions = {:?}", value1);
36 struct RegionEraserVisitor<'tcx> {
40 impl TypeFolder<'tcx> for RegionEraserVisitor<'tcx> {
41 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
45 fn fold_ty(&mut self, ty: Ty<'tcx>) -> Result<Ty<'tcx>, Self::Error> {
46 if ty.needs_infer() { ty.super_fold_with(self) } else { Ok(self.tcx.erase_regions_ty(ty)) }
49 fn fold_binder<T>(&mut self, t: ty::Binder<'tcx, T>) -> Result<ty::Binder<'tcx, T>, Self::Error>
51 T: TypeFoldable<'tcx>,
53 let u = self.tcx.anonymize_late_bound_regions(t);
54 u.super_fold_with(self)
57 fn fold_region(&mut self, r: ty::Region<'tcx>) -> Result<ty::Region<'tcx>, Self::Error> {
58 // because late-bound regions affect subtyping, we can't
59 // erase the bound/free distinction, but we can replace
60 // all free regions with 'erased.
62 // Note that we *CAN* replace early-bound regions -- the
63 // type system never "sees" those, they get substituted
64 // away. In codegen, they will always be erased to 'erased
65 // whenever a substitution occurs.
67 ty::ReLateBound(..) => Ok(r),
68 _ => Ok(self.tcx.lifetimes.re_erased),
74 c: mir::ConstantKind<'tcx>,
75 ) -> Result<mir::ConstantKind<'tcx>, Self::Error> {
76 c.super_fold_with(self)