]> git.lizzy.rs Git - rust.git/blob - src/librustc/ty/erase_regions.rs
Auto merge of #60708 - Centril:rollup-j5smdo0, r=Centril
[rust.git] / src / librustc / ty / erase_regions.rs
1 use crate::ty::{self, Ty, TyCtxt, TypeFlags};
2 use crate::ty::fold::{TypeFolder, TypeFoldable};
3
4 pub(super) fn provide(providers: &mut ty::query::Providers<'_>) {
5     *providers = ty::query::Providers {
6         erase_regions_ty,
7         ..*providers
8     };
9 }
10
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 })
15 }
16
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>
23     {
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) {
26             return value.clone();
27         }
28
29         let value1 = value.fold_with(&mut RegionEraserVisitor { tcx: self });
30         debug!("erase_regions({:?}) = {:?}", value, value1);
31         value1
32     }
33 }
34
35 struct RegionEraserVisitor<'a, 'gcx: 'tcx, 'tcx: 'a> {
36     tcx: TyCtxt<'a, 'gcx, 'tcx>,
37 }
38
39 impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for RegionEraserVisitor<'a, 'gcx, 'tcx> {
40     fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'tcx> {
41         self.tcx
42     }
43
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)
47         } else {
48             ty.super_fold_with(self)
49         }
50     }
51
52     fn fold_binder<T>(&mut self, t: &ty::Binder<T>) -> ty::Binder<T>
53         where T : TypeFoldable<'tcx>
54     {
55         let u = self.tcx.anonymize_late_bound_regions(t);
56         u.super_fold_with(self)
57     }
58
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.
63         //
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.
68         match *r {
69             ty::ReLateBound(..) => r,
70             _ => self.tcx.lifetimes.re_erased
71         }
72     }
73 }