Skip to content

Commit 41cfb20

Browse files
committed
replace unnecessary folder impls with fold_region
1 parent 00bfd6b commit 41cfb20

File tree

4 files changed

+15
-51
lines changed

4 files changed

+15
-51
lines changed

compiler/rustc_const_eval/src/transform/promote_consts.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,7 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
864864
};
865865

866866
// Use the underlying local for this (necessarily interior) borrow.
867+
debug_assert!(region.is_erased());
867868
let ty = local_decls[place.local].ty;
868869
let span = statement.source_info.span;
869870

@@ -873,8 +874,6 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
873874
ty::TypeAndMut { ty, mutbl: borrow_kind.to_mutbl_lossy() },
874875
);
875876

876-
*region = tcx.lifetimes.re_erased;
877-
878877
let mut projection = vec![PlaceElem::Deref];
879878
projection.extend(place.projection);
880879
place.projection = tcx.mk_place_elems(&projection);

compiler/rustc_hir_analysis/src/hir_wf_check.rs

+8-24
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_hir::{ForeignItem, ForeignItemKind};
55
use rustc_infer::infer::TyCtxtInferExt;
66
use rustc_infer::traits::{ObligationCause, WellFormedLoc};
77
use rustc_middle::query::Providers;
8-
use rustc_middle::ty::{self, Region, TyCtxt, TypeFoldable, TypeFolder};
8+
use rustc_middle::ty::{self, TyCtxt};
99
use rustc_span::def_id::LocalDefId;
1010
use 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-
}

compiler/rustc_hir_typeck/src/writeback.rs

+5-24
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_hir as hir;
99
use rustc_hir::intravisit::{self, Visitor};
1010
use rustc_infer::infer::error_reporting::TypeAnnotationNeeded::E0282;
1111
use rustc_middle::ty::adjustment::{Adjust, Adjustment, PointerCoercion};
12-
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable};
12+
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder};
1313
use rustc_middle::ty::visit::TypeVisitableExt;
1414
use rustc_middle::ty::{self, Ty, TyCtxt};
1515
use rustc_span::symbol::sym;
@@ -768,49 +768,30 @@ impl<'cx, 'tcx> Resolver<'cx, 'tcx> {
768768
}
769769
}
770770

771-
struct EraseEarlyRegions<'tcx> {
772-
tcx: TyCtxt<'tcx>,
773-
}
774-
775-
impl<'tcx> TypeFolder<TyCtxt<'tcx>> for EraseEarlyRegions<'tcx> {
776-
fn interner(&self) -> TyCtxt<'tcx> {
777-
self.tcx
778-
}
779-
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
780-
if ty.has_type_flags(ty::TypeFlags::HAS_FREE_REGIONS) {
781-
ty.super_fold_with(self)
782-
} else {
783-
ty
784-
}
785-
}
786-
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
787-
if r.is_bound() { r } else { self.tcx.lifetimes.re_erased }
788-
}
789-
}
790-
791771
impl<'cx, 'tcx> TypeFolder<TyCtxt<'tcx>> for Resolver<'cx, 'tcx> {
792772
fn interner(&self) -> TyCtxt<'tcx> {
793773
self.fcx.tcx
794774
}
795775

796776
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
777+
let tcx = self.fcx.tcx;
797778
match self.fcx.fully_resolve(t) {
798779
Ok(t) if self.fcx.next_trait_solver() => {
799780
// We must normalize erasing regions here, since later lints
800781
// expect that types that show up in the typeck are fully
801782
// normalized.
802-
if let Ok(t) = self.fcx.tcx.try_normalize_erasing_regions(self.fcx.param_env, t) {
783+
if let Ok(t) = tcx.try_normalize_erasing_regions(self.fcx.param_env, t) {
803784
t
804785
} else {
805-
EraseEarlyRegions { tcx: self.fcx.tcx }.fold_ty(t)
786+
tcx.fold_regions(t, |_, _| tcx.lifetimes.re_erased)
806787
}
807788
}
808789
Ok(t) => {
809790
// Do not anonymize late-bound regions
810791
// (e.g. keep `for<'a>` named `for<'a>`).
811792
// This allows NLL to generate error messages that
812793
// refer to the higher-ranked lifetime names written by the user.
813-
EraseEarlyRegions { tcx: self.fcx.tcx }.fold_ty(t)
794+
tcx.fold_regions(t, |_, _| tcx.lifetimes.re_erased)
814795
}
815796
Err(_) => {
816797
debug!("Resolver::fold_ty: input type `{:?}` not fully resolvable", t);

compiler/rustc_infer/src/errors/note_and_explain.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl<'a> DescriptionCtx<'a> {
7575
// We shouldn't really be having unification failures with ReVar
7676
// and ReBound though.
7777
//
78-
// FIXME(@lcnr): figure out why we `ReBound` have to handle `ReBound`
78+
// FIXME(@lcnr): figure out why we have to handle `ReBound`
7979
// here, this feels somewhat off.
8080
ty::ReVar(_) | ty::ReBound(..) | ty::ReErased => {
8181
(alt_span, "revar", format!("{region:?}"))

0 commit comments

Comments
 (0)