@@ -5,7 +5,7 @@ use rustc_hir::{ForeignItem, ForeignItemKind};
5
5
use rustc_infer:: infer:: TyCtxtInferExt ;
6
6
use rustc_infer:: traits:: { ObligationCause , WellFormedLoc } ;
7
7
use rustc_middle:: query:: Providers ;
8
- use rustc_middle:: ty:: { self , Region , TyCtxt , TypeFoldable , TypeFolder } ;
8
+ use rustc_middle:: ty:: { self , TyCtxt } ;
9
9
use rustc_span:: def_id:: LocalDefId ;
10
10
use rustc_trait_selection:: traits:: { self , ObligationCtxt } ;
11
11
@@ -68,7 +68,13 @@ fn diagnostic_hir_wf_check<'tcx>(
68
68
let infcx = self . tcx . infer_ctxt ( ) . build ( ) ;
69
69
let ocx = ObligationCtxt :: new ( & infcx) ;
70
70
71
- let tcx_ty = self . icx . to_ty ( ty) . fold_with ( & mut EraseAllBoundRegions { tcx : self . tcx } ) ;
71
+ let tcx_ty = self . icx . to_ty ( ty) ;
72
+ // This visitor can walk into binders, resulting in the `tcx_ty` to
73
+ // potentially reference escaping bound variables. We simply erase
74
+ // those here.
75
+ let tcx_ty = self . tcx . fold_regions ( tcx_ty, |r, _| {
76
+ if r. is_bound ( ) { self . tcx . lifetimes . re_erased } else { r }
77
+ } ) ;
72
78
let cause = traits:: ObligationCause :: new (
73
79
ty. span ,
74
80
self . def_id ,
@@ -178,25 +184,3 @@ fn diagnostic_hir_wf_check<'tcx>(
178
184
}
179
185
visitor. cause
180
186
}
181
-
182
- struct EraseAllBoundRegions < ' tcx > {
183
- tcx : TyCtxt < ' tcx > ,
184
- }
185
-
186
- // Higher ranked regions are complicated.
187
- // To make matters worse, the HIR WF check can instantiate them
188
- // outside of a `Binder`, due to the way we (ab)use
189
- // `ItemCtxt::to_ty`. To make things simpler, we just erase all
190
- // of them, regardless of depth. At worse, this will give
191
- // us an inaccurate span for an error message, but cannot
192
- // lead to unsoundness (we call `delay_span_bug` at the start
193
- // of `diagnostic_hir_wf_check`).
194
- impl < ' tcx > TypeFolder < TyCtxt < ' tcx > > for EraseAllBoundRegions < ' tcx > {
195
- fn interner ( & self ) -> TyCtxt < ' tcx > {
196
- self . tcx
197
- }
198
- fn fold_region ( & mut self , r : Region < ' tcx > ) -> Region < ' tcx > {
199
- // FIXME(@lcnr): only erase escaping bound regions!
200
- if r. is_bound ( ) { self . tcx . lifetimes . re_erased } else { r }
201
- }
202
- }
0 commit comments