1 use crate::ty::{self, Ty, TyCtxt, TypeFlags};
2 use crate::ty::fold::{TypeFolder, TypeFoldable};
4 pub(super) fn provide(providers: &mut ty::query::Providers<'_>) {
5 *providers = ty::query::Providers {
11 fn erase_regions_ty<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, '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 })
17 impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, '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 T : TypeFoldable<'tcx>
24 // If there's nothing to erase avoid performing the query at all
25 if !value.has_type_flags(TypeFlags::HAS_RE_LATE_BOUND | TypeFlags::HAS_FREE_REGIONS) {
29 let value1 = value.fold_with(&mut RegionEraserVisitor { tcx: self });
30 debug!("erase_regions({:?}) = {:?}", value, value1);
35 struct RegionEraserVisitor<'a, 'gcx: 'tcx, 'tcx: 'a> {
36 tcx: TyCtxt<'a, 'gcx, 'tcx>,
39 impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for RegionEraserVisitor<'a, 'gcx, 'tcx> {
40 fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'tcx> {
44 fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
45 if let Some(ty_lifted) = self.tcx.lift_to_global(&ty) {
46 self.tcx.erase_regions_ty(ty_lifted)
48 ty.super_fold_with(self)
52 fn fold_binder<T>(&mut self, t: &ty::Binder<T>) -> ty::Binder<T>
53 where T : TypeFoldable<'tcx>
55 let u = self.tcx.anonymize_late_bound_regions(t);
56 u.super_fold_with(self)
59 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
60 // because late-bound regions affect subtyping, we can't
61 // erase the bound/free distinction, but we can replace
62 // all free regions with 'erased.
64 // Note that we *CAN* replace early-bound regions -- the
65 // type system never "sees" those, they get substituted
66 // away. In codegen, they will always be erased to 'erased
67 // whenever a substitution occurs.
69 ty::ReLateBound(..) => r,
70 _ => self.tcx.lifetimes.re_erased