@@ -5,7 +5,7 @@ use rustc_hir::{ForeignItem, ForeignItemKind};
55use rustc_infer::infer::TyCtxtInferExt;
66use rustc_infer::traits::{ObligationCause, WellFormedLoc};
77use rustc_middle::query::Providers;
8- use rustc_middle::ty::{self, Region, TyCtxt, TypeFoldable, TypeFolder };
8+ use rustc_middle::ty::{self, TyCtxt};
99use rustc_span::def_id::LocalDefId;
1010use rustc_trait_selection::traits::{self, ObligationCtxt};
1111
@@ -68,7 +68,13 @@ fn diagnostic_hir_wf_check<'tcx>(
6868 let infcx = self.tcx.infer_ctxt().build();
6969 let ocx = ObligationCtxt::new(&infcx);
7070
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+ });
7278 let cause = traits::ObligationCause::new(
7379 ty.span,
7480 self.def_id,
@@ -178,25 +184,3 @@ fn diagnostic_hir_wf_check<'tcx>(
178184 }
179185 visitor.cause
180186}
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