Skip to content

Commit 069a4af

Browse files
committed
Auto merge of #117944 - lcnr:region-refactor-uwu, r=BoxyUwU
some additional region refactorings the commits are selfcontained ✨ r? `@BoxyUwU`
2 parents 4d7f952 + 40b154e commit 069a4af

File tree

39 files changed

+93
-120
lines changed

39 files changed

+93
-120
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1578,8 +1578,9 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
15781578
return;
15791579
};
15801580
let sig = args.as_closure().sig();
1581-
let tupled_params =
1582-
tcx.erase_late_bound_regions(sig.inputs().iter().next().unwrap().map_bound(|&b| b));
1581+
let tupled_params = tcx.instantiate_bound_regions_with_erased(
1582+
sig.inputs().iter().next().unwrap().map_bound(|&b| b),
1583+
);
15831584
let ty::Tuple(params) = tupled_params.kind() else { return };
15841585

15851586
// Find the first argument with a matching type, get its name

compiler/rustc_borrowck/src/type_check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1387,7 +1387,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
13871387
return;
13881388
}
13891389
};
1390-
let (sig, map) = tcx.replace_late_bound_regions(sig, |br| {
1390+
let (sig, map) = tcx.instantiate_bound_regions(sig, |br| {
13911391
use crate::renumber::RegionCtxt;
13921392

13931393
let region_ctxt_fn = || {

compiler/rustc_borrowck/src/universal_regions.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -738,13 +738,13 @@ trait InferCtxtExt<'tcx> {
738738
where
739739
T: TypeFoldable<TyCtxt<'tcx>>;
740740

741-
fn replace_late_bound_regions_with_nll_infer_vars_in_recursive_scope(
741+
fn instantiate_bound_regions_with_nll_infer_vars_in_recursive_scope(
742742
&self,
743743
mir_def_id: LocalDefId,
744744
indices: &mut UniversalRegionIndices<'tcx>,
745745
);
746746

747-
fn replace_late_bound_regions_with_nll_infer_vars_in_item(
747+
fn instantiate_bound_regions_with_nll_infer_vars_in_item(
748748
&self,
749749
mir_def_id: LocalDefId,
750750
indices: &mut UniversalRegionIndices<'tcx>,
@@ -780,7 +780,7 @@ impl<'cx, 'tcx> InferCtxtExt<'tcx> for BorrowckInferCtxt<'cx, 'tcx> {
780780
where
781781
T: TypeFoldable<TyCtxt<'tcx>>,
782782
{
783-
let (value, _map) = self.tcx.replace_late_bound_regions(value, |br| {
783+
let (value, _map) = self.tcx.instantiate_bound_regions(value, |br| {
784784
debug!(?br);
785785
let liberated_region =
786786
ty::Region::new_late_param(self.tcx, all_outlive_scope.to_def_id(), br.kind);
@@ -810,7 +810,7 @@ impl<'cx, 'tcx> InferCtxtExt<'tcx> for BorrowckInferCtxt<'cx, 'tcx> {
810810
/// set of late-bound regions and checks for any that we have not yet seen, adding them to the
811811
/// inputs vector.
812812
#[instrument(skip(self, indices))]
813-
fn replace_late_bound_regions_with_nll_infer_vars_in_recursive_scope(
813+
fn instantiate_bound_regions_with_nll_infer_vars_in_recursive_scope(
814814
&self,
815815
mir_def_id: LocalDefId,
816816
indices: &mut UniversalRegionIndices<'tcx>,
@@ -830,7 +830,7 @@ impl<'cx, 'tcx> InferCtxtExt<'tcx> for BorrowckInferCtxt<'cx, 'tcx> {
830830
}
831831

832832
#[instrument(skip(self, indices))]
833-
fn replace_late_bound_regions_with_nll_infer_vars_in_item(
833+
fn instantiate_bound_regions_with_nll_infer_vars_in_item(
834834
&self,
835835
mir_def_id: LocalDefId,
836836
indices: &mut UniversalRegionIndices<'tcx>,

compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ fn push_debuginfo_type_name<'tcx>(
249249
.projection_bounds()
250250
.map(|bound| {
251251
let ExistentialProjection { def_id: item_def_id, term, .. } =
252-
tcx.erase_late_bound_regions(bound);
252+
tcx.instantiate_bound_regions_with_erased(bound);
253253
// FIXME(associated_const_equality): allow for consts here
254254
(item_def_id, term.ty().unwrap())
255255
})

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/collect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ impl<'tcx> AstConv<'tcx> for ItemCtxt<'tcx> {
440440
second: format!(
441441
"{}::",
442442
// Replace the existing lifetimes with a new named lifetime.
443-
self.tcx.replace_late_bound_regions_uncached(
443+
self.tcx.instantiate_bound_regions_uncached(
444444
poly_trait_ref,
445445
|_| {
446446
ty::Region::new_early_param(self.tcx, ty::EarlyParamRegion {

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/fn_ctxt/suggestions.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
846846
let bound_vars = self.tcx.late_bound_vars(hir_ty.hir_id.owner.into());
847847
let ty = Binder::bind_with_vars(ty, bound_vars);
848848
let ty = self.normalize(hir_ty.span, ty);
849-
let ty = self.tcx.erase_late_bound_regions(ty);
849+
let ty = self.tcx.instantiate_bound_regions_with_erased(ty);
850850
if self.can_coerce(expected, ty) {
851851
err.subdiagnostic(errors::ExpectedReturnTypeLabel::Other {
852852
span: hir_ty.span,
@@ -1023,7 +1023,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10231023
if let hir::FnRetTy::Return(ty) = fn_decl.output {
10241024
let ty = self.astconv().ast_ty_to_ty(ty);
10251025
let bound_vars = self.tcx.late_bound_vars(fn_id);
1026-
let ty = self.tcx.erase_late_bound_regions(Binder::bind_with_vars(ty, bound_vars));
1026+
let ty = self
1027+
.tcx
1028+
.instantiate_bound_regions_with_erased(Binder::bind_with_vars(ty, bound_vars));
10271029
let ty = match self.tcx.asyncness(fn_id.owner) {
10281030
ty::Asyncness::Yes => self.get_impl_future_output_ty(ty).unwrap_or_else(|| {
10291031
span_bug!(fn_decl.output.span(), "failed to get output type of async function")

compiler/rustc_hir_typeck/src/method/probe.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
809809
return;
810810
}
811811

812-
let new_trait_ref = this.erase_late_bound_regions(new_trait_ref);
812+
let new_trait_ref = this.instantiate_bound_regions_with_erased(new_trait_ref);
813813

814814
let (xform_self_ty, xform_ret_ty) =
815815
this.xform_self_ty(item, new_trait_ref.self_ty(), new_trait_ref.args);
@@ -1885,7 +1885,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
18851885
fn_sig.instantiate(self.tcx, args)
18861886
};
18871887

1888-
self.erase_late_bound_regions(xform_fn_sig)
1888+
self.instantiate_bound_regions_with_erased(xform_fn_sig)
18891889
}
18901890

18911891
/// Gets the type of an impl and generate substitutions with inference vars.
@@ -1897,7 +1897,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
18971897
}
18981898

18991899
/// Replaces late-bound-regions bound by `value` with `'static` using
1900-
/// `ty::erase_late_bound_regions`.
1900+
/// `ty::instantiate_bound_regions_with_erased`.
19011901
///
19021902
/// This is only a reasonable thing to do during the *probe* phase, not the *confirm* phase, of
19031903
/// method matching. It is reasonable during the probe phase because we don't consider region
@@ -1914,11 +1914,11 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
19141914
/// region got replaced with the same variable, which requires a bit more coordination
19151915
/// and/or tracking the substitution and
19161916
/// so forth.
1917-
fn erase_late_bound_regions<T>(&self, value: ty::Binder<'tcx, T>) -> T
1917+
fn instantiate_bound_regions_with_erased<T>(&self, value: ty::Binder<'tcx, T>) -> T
19181918
where
19191919
T: TypeFoldable<TyCtxt<'tcx>>,
19201920
{
1921-
self.tcx.erase_late_bound_regions(value)
1921+
self.tcx.instantiate_bound_regions_with_erased(value)
19221922
}
19231923

19241924
/// Determine if the given associated item type is relevant in the current context.

compiler/rustc_hir_typeck/src/method/suggest.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1454,7 +1454,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14541454
.filter_map(|item| {
14551455
// Only assoc fns that return `Self`, `Option<Self>` or `Result<Self, _>`.
14561456
let ret_ty = self.tcx.fn_sig(item.def_id).skip_binder().output();
1457-
let ret_ty = self.tcx.erase_late_bound_regions(ret_ty);
1457+
let ret_ty = self.tcx.instantiate_bound_regions_with_erased(ret_ty);
14581458
let ty::Adt(def, args) = ret_ty.kind() else {
14591459
return None;
14601460
};

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:?}"))

compiler/rustc_lint/src/foreign_modules.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,8 @@ fn structurally_same_type_impl<'tcx>(
341341

342342
// We don't compare regions, but leaving bound regions around ICEs, so
343343
// we erase them.
344-
let a_sig = tcx.erase_late_bound_regions(a_poly_sig);
345-
let b_sig = tcx.erase_late_bound_regions(b_poly_sig);
344+
let a_sig = tcx.instantiate_bound_regions_with_erased(a_poly_sig);
345+
let b_sig = tcx.instantiate_bound_regions_with_erased(b_poly_sig);
346346

347347
(a_sig.abi, a_sig.unsafety, a_sig.c_variadic)
348348
== (b_sig.abi, b_sig.unsafety, b_sig.c_variadic)

compiler/rustc_lint/src/types.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1234,7 +1234,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
12341234
};
12351235
}
12361236

1237-
let sig = tcx.erase_late_bound_regions(sig);
1237+
let sig = tcx.instantiate_bound_regions_with_erased(sig);
12381238
for arg in sig.inputs() {
12391239
match self.check_type_for_ffi(cache, *arg) {
12401240
FfiSafe => {}
@@ -1391,7 +1391,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
13911391
/// types that have external ABIs, as these still need checked.
13921392
fn check_fn(&mut self, def_id: LocalDefId, decl: &'tcx hir::FnDecl<'_>) {
13931393
let sig = self.cx.tcx.fn_sig(def_id).instantiate_identity();
1394-
let sig = self.cx.tcx.erase_late_bound_regions(sig);
1394+
let sig = self.cx.tcx.instantiate_bound_regions_with_erased(sig);
13951395

13961396
for (input_ty, input_hir) in iter::zip(sig.inputs(), decl.inputs) {
13971397
for (fn_ptr_ty, span) in self.find_fn_ptr_ty_with_external_abi(input_hir, *input_ty) {
@@ -1409,7 +1409,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
14091409
/// Check if a function's argument types and result type are "ffi-safe".
14101410
fn check_foreign_fn(&mut self, def_id: LocalDefId, decl: &'tcx hir::FnDecl<'_>) {
14111411
let sig = self.cx.tcx.fn_sig(def_id).instantiate_identity();
1412-
let sig = self.cx.tcx.erase_late_bound_regions(sig);
1412+
let sig = self.cx.tcx.instantiate_bound_regions_with_erased(sig);
14131413

14141414
for (input_ty, input_hir) in iter::zip(sig.inputs(), decl.inputs) {
14151415
self.check_type_for_ffi_and_report_errors(input_hir.span, *input_ty, false, false);

compiler/rustc_metadata/src/native_libs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ impl<'tcx> Collector<'tcx> {
470470
}
471471

472472
fn i686_arg_list_size(&self, item: DefId) -> usize {
473-
let argument_types: &List<Ty<'_>> = self.tcx.erase_late_bound_regions(
473+
let argument_types: &List<Ty<'_>> = self.tcx.instantiate_bound_regions_with_erased(
474474
self.tcx
475475
.type_of(item)
476476
.instantiate_identity()

compiler/rustc_middle/src/ty/fold.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ impl<'tcx> TyCtxt<'tcx> {
250250
///
251251
/// This method only replaces late bound regions. Any types or
252252
/// constants bound by `value` will cause an ICE.
253-
pub fn replace_late_bound_regions<T, F>(
253+
pub fn instantiate_bound_regions<T, F>(
254254
self,
255255
value: Binder<'tcx, T>,
256256
mut fld_r: F,
@@ -261,11 +261,11 @@ impl<'tcx> TyCtxt<'tcx> {
261261
{
262262
let mut region_map = BTreeMap::new();
263263
let real_fld_r = |br: ty::BoundRegion| *region_map.entry(br).or_insert_with(|| fld_r(br));
264-
let value = self.replace_late_bound_regions_uncached(value, real_fld_r);
264+
let value = self.instantiate_bound_regions_uncached(value, real_fld_r);
265265
(value, region_map)
266266
}
267267

268-
pub fn replace_late_bound_regions_uncached<T, F>(
268+
pub fn instantiate_bound_regions_uncached<T, F>(
269269
self,
270270
value: Binder<'tcx, T>,
271271
mut replace_regions: F,
@@ -325,7 +325,7 @@ impl<'tcx> TyCtxt<'tcx> {
325325
where
326326
T: TypeFoldable<TyCtxt<'tcx>>,
327327
{
328-
self.replace_late_bound_regions_uncached(value, |br| {
328+
self.instantiate_bound_regions_uncached(value, |br| {
329329
ty::Region::new_late_param(self, all_outlive_scope, br.kind)
330330
})
331331
}
@@ -361,11 +361,11 @@ impl<'tcx> TyCtxt<'tcx> {
361361

362362
/// Replaces any late-bound regions bound in `value` with `'erased`. Useful in codegen but also
363363
/// method lookup and a few other places where precise region relationships are not required.
364-
pub fn erase_late_bound_regions<T>(self, value: Binder<'tcx, T>) -> T
364+
pub fn instantiate_bound_regions_with_erased<T>(self, value: Binder<'tcx, T>) -> T
365365
where
366366
T: TypeFoldable<TyCtxt<'tcx>>,
367367
{
368-
self.replace_late_bound_regions(value, |_| self.lifetimes.re_erased).0
368+
self.instantiate_bound_regions(value, |_| self.lifetimes.re_erased).0
369369
}
370370

371371
/// Anonymize all bound variables in `value`, this is mostly used to improve caching.

0 commit comments

Comments
 (0)