@@ -2,7 +2,8 @@ use crate::ty::fold::{TypeFoldable, TypeFolder};
2
2
use crate :: ty:: { self , Ty , TyCtxt , TypeFlags } ;
3
3
4
4
pub ( super ) fn provide ( providers : & mut ty:: query:: Providers ) {
5
- * providers = ty:: query:: Providers { erase_regions_ty, ..* providers } ;
5
+ * providers =
6
+ ty:: query:: Providers { erase_regions_ty, erase_early_and_late_regions_ty, ..* providers } ;
6
7
}
7
8
8
9
fn erase_regions_ty < ' tcx > ( tcx : TyCtxt < ' tcx > , ty : Ty < ' tcx > ) -> Ty < ' tcx > {
@@ -11,6 +12,12 @@ fn erase_regions_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> {
11
12
ty. super_fold_with ( & mut RegionEraserVisitor { tcx } )
12
13
}
13
14
15
+ fn erase_early_and_late_regions_ty ( tcx : TyCtxt < ' tcx > , ty : Ty < ' tcx > ) -> Ty < ' tcx > {
16
+ // N.B., use `super_fold_with` here. If we used `fold_with`, it
17
+ // could invoke the `erase_regions_ty` query recursively.
18
+ ty. super_fold_with ( & mut AllRegionEraserVisitor { tcx } )
19
+ }
20
+
14
21
impl < ' tcx > TyCtxt < ' tcx > {
15
22
/// Returns an equivalent value with all free regions removed (note
16
23
/// that late-bound regions remain, because they are important for
@@ -28,6 +35,36 @@ impl<'tcx> TyCtxt<'tcx> {
28
35
debug ! ( "erase_regions({:?}) = {:?}" , value, value1) ;
29
36
value1
30
37
}
38
+
39
+ /// Like `erase_regions`, but erases all regions, including late-bound regions.
40
+ /// This is only useful during certain parts of codegen, where regions truly
41
+ /// don't matter. Normally, `erase_regions` should be used instead.
42
+ pub fn erase_early_and_late_regions < T > ( self , value : & T ) -> T
43
+ where
44
+ T : TypeFoldable < ' tcx > ,
45
+ {
46
+ // If there's nothing to erase avoid performing the query at all
47
+ if !value. has_type_flags ( TypeFlags :: HAS_RE_LATE_BOUND | TypeFlags :: HAS_FREE_REGIONS ) {
48
+ return value. clone ( ) ;
49
+ }
50
+ value. fold_with ( & mut AllRegionEraserVisitor { tcx : self } )
51
+ }
52
+ }
53
+
54
+ struct AllRegionEraserVisitor < ' tcx > {
55
+ tcx : TyCtxt < ' tcx > ,
56
+ }
57
+
58
+ impl TypeFolder < ' tcx > for AllRegionEraserVisitor < ' tcx > {
59
+ fn tcx < ' b > ( & ' b self ) -> TyCtxt < ' tcx > {
60
+ self . tcx
61
+ }
62
+ fn fold_ty ( & mut self , ty : Ty < ' tcx > ) -> Ty < ' tcx > {
63
+ self . tcx . erase_early_and_late_regions_ty ( ty)
64
+ }
65
+ fn fold_region ( & mut self , _: ty:: Region < ' tcx > ) -> ty:: Region < ' tcx > {
66
+ self . tcx . lifetimes . re_erased
67
+ }
31
68
}
32
69
33
70
struct RegionEraserVisitor < ' tcx > {
0 commit comments