Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 069a4af

Browse files
committed
Auto merge of rust-lang#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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 5 additions & 5 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 8 additions & 24 deletions
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

Lines changed: 4 additions & 2 deletions
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

Lines changed: 5 additions & 5 deletions
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

Lines changed: 1 addition & 1 deletion
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
};

0 commit comments

Comments
 (0)