Skip to content

Commit 2d2bccf

Browse files
committed
rebase
1 parent 46bd77a commit 2d2bccf

File tree

4 files changed

+92
-81
lines changed

4 files changed

+92
-81
lines changed

compiler/rustc_borrowck/src/renumber.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_middle::mir::{Body, Location, Promoted};
99
use rustc_middle::ty::subst::SubstsRef;
1010
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable};
1111
#[cfg(debug_assertions)]
12-
use rustc_span::Symbol;
12+
use rustc_span::{Span, Symbol};
1313

1414
/// Replaces all free regions appearing in the MIR with fresh
1515
/// inference variables, returning the number of variables created.
@@ -62,16 +62,23 @@ where
6262
})
6363
}
6464

65+
#[cfg(debug_assertions)]
66+
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
67+
pub(crate) enum BoundRegionInfo {
68+
Name(Symbol),
69+
Span(Span),
70+
}
71+
6572
#[cfg(debug_assertions)]
6673
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
6774
pub(crate) enum RegionCtxt {
6875
Location(Location),
6976
TyContext(TyContext),
7077
Free(Symbol),
71-
Bound(Symbol),
72-
LateBound(Symbol),
78+
Bound(BoundRegionInfo),
79+
LateBound(BoundRegionInfo),
7380
Existential(Option<Symbol>),
74-
Placeholder(Symbol),
81+
Placeholder(BoundRegionInfo),
7582
Unknown,
7683
}
7784

@@ -86,10 +93,7 @@ impl RegionCtxt {
8693
match self {
8794
RegionCtxt::Unknown => 1,
8895
RegionCtxt::Existential(None) => 2,
89-
RegionCtxt::Existential(Some(_anon))
90-
| RegionCtxt::Free(_anon)
91-
| RegionCtxt::Bound(_anon)
92-
| RegionCtxt::LateBound(_anon) => 2,
96+
RegionCtxt::Existential(Some(_anon)) | RegionCtxt::Free(_anon) => 2,
9397
RegionCtxt::Location(_) => 3,
9498
RegionCtxt::TyContext(_) => 4,
9599
_ => 5,

compiler/rustc_borrowck/src/type_check/mod.rs

+14-6
Original file line numberDiff line numberDiff line change
@@ -1346,13 +1346,21 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
13461346

13471347
#[cfg(debug_assertions)]
13481348
{
1349-
use crate::renumber::RegionCtxt;
1349+
use crate::renumber::{BoundRegionInfo, RegionCtxt};
13501350
use rustc_span::Symbol;
13511351

1352-
let name = match br.kind {
1353-
ty::BoundRegionKind::BrAnon(_) => Symbol::intern("anon"),
1354-
ty::BoundRegionKind::BrNamed(_, name) => name,
1355-
ty::BoundRegionKind::BrEnv => Symbol::intern("env"),
1352+
let reg_info = match br.kind {
1353+
// FIXME Probably better to use the `Span` here
1354+
ty::BoundRegionKind::BrAnon(_, Some(span)) => {
1355+
BoundRegionInfo::Span(span)
1356+
}
1357+
ty::BoundRegionKind::BrAnon(..) => {
1358+
BoundRegionInfo::Name(Symbol::intern("anon"))
1359+
}
1360+
ty::BoundRegionKind::BrNamed(_, name) => BoundRegionInfo::Name(name),
1361+
ty::BoundRegionKind::BrEnv => {
1362+
BoundRegionInfo::Name(Symbol::intern("env"))
1363+
}
13561364
};
13571365

13581366
self.infcx.next_region_var(
@@ -1361,7 +1369,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
13611369
br.kind,
13621370
LateBoundRegionConversionTime::FnCall,
13631371
),
1364-
RegionCtxt::LateBound(name),
1372+
RegionCtxt::LateBound(reg_info),
13651373
)
13661374
}
13671375
});

compiler/rustc_borrowck/src/type_check/relate_tys.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_trait_selection::traits::query::Fallible;
1010
use crate::constraints::OutlivesConstraint;
1111
use crate::diagnostics::UniverseInfo;
1212
#[cfg(debug_assertions)]
13-
use crate::renumber::RegionCtxt;
13+
use crate::renumber::{BoundRegionInfo, RegionCtxt};
1414
use crate::type_check::{InstantiateOpaqueType, Locations, TypeChecker};
1515

1616
impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
@@ -130,17 +130,19 @@ impl<'tcx> TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, 'tcx>
130130

131131
#[cfg(debug_assertions)]
132132
{
133-
let name = match placeholder.name {
134-
ty::BoundRegionKind::BrAnon(_) => Symbol::intern("anon"),
135-
ty::BoundRegionKind::BrNamed(_, name) => name,
136-
ty::BoundRegionKind::BrEnv => Symbol::intern("env"),
133+
let reg_info = match placeholder.name {
134+
// FIXME Probably better to use the `Span` here
135+
ty::BoundRegionKind::BrAnon(_, Some(span)) => BoundRegionInfo::Span(span),
136+
ty::BoundRegionKind::BrAnon(..) => BoundRegionInfo::Name(Symbol::intern("anon")),
137+
ty::BoundRegionKind::BrNamed(_, name) => BoundRegionInfo::Name(name),
138+
ty::BoundRegionKind::BrEnv => BoundRegionInfo::Name(Symbol::intern("env")),
137139
};
138140

139141
let reg_var = reg
140142
.try_get_var()
141143
.unwrap_or_else(|| bug!("expected region {:?} to be of kind ReVar", reg));
142144
let mut var_to_origin = self.type_checker.infcx.reg_var_to_origin.borrow_mut();
143-
let prev = var_to_origin.insert(reg_var, RegionCtxt::Placeholder(name));
145+
let prev = var_to_origin.insert(reg_var, RegionCtxt::Placeholder(reg_info));
144146
assert!(matches!(prev, None));
145147
}
146148

compiler/rustc_borrowck/src/universal_regions.rs

+58-61
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use std::iter;
3232

3333
use crate::nll::ToRegionVid;
3434
#[cfg(debug_assertions)]
35-
use crate::renumber::RegionCtxt;
35+
use crate::renumber::{BoundRegionInfo, RegionCtxt};
3636
use crate::BorrowckInferCtxt;
3737

3838
#[derive(Debug)]
@@ -446,7 +446,22 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
446446
|r| {
447447
debug!(?r);
448448
if !indices.indices.contains_key(&r) {
449+
#[cfg(not(debug_assertions))]
449450
let region_vid = self.infcx.next_nll_region_var(FR);
451+
452+
#[cfg(debug_assertions)]
453+
let region_vid = {
454+
let name = match r.get_name() {
455+
Some(name) => name,
456+
_ => Symbol::intern("anon"),
457+
};
458+
459+
self.infcx.next_nll_region_var(
460+
FR,
461+
RegionCtxt::LateBound(BoundRegionInfo::Name(name)),
462+
)
463+
};
464+
450465
debug!(?region_vid);
451466
indices.insert_late_bound_region(r, region_vid.to_region_vid());
452467
}
@@ -474,7 +489,20 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
474489
for_each_late_bound_region_in_item(self.infcx.tcx, self.mir_def.did, |r| {
475490
debug!(?r);
476491
if !indices.indices.contains_key(&r) {
492+
#[cfg(not(debug_assertions))]
477493
let region_vid = self.infcx.next_nll_region_var(FR);
494+
495+
#[cfg(debug_assertions)]
496+
let region_vid = {
497+
let name = match r.get_name() {
498+
Some(name) => name,
499+
_ => Symbol::intern("anon"),
500+
};
501+
502+
self.infcx
503+
.next_nll_region_var(FR, RegionCtxt::LateBound(BoundRegionInfo::Name(name)))
504+
};
505+
478506
debug!(?region_vid);
479507
indices.insert_late_bound_region(r, region_vid.to_region_vid());
480508
}
@@ -773,7 +801,6 @@ impl<'cx, 'tcx> InferCtxtExt<'tcx> for BorrowckInferCtxt<'cx, 'tcx> {
773801
})
774802
}
775803

776-
#[cfg(not(debug_assertions))]
777804
#[instrument(level = "debug", skip(self, indices))]
778805
fn replace_bound_regions_with_nll_infer_vars<T>(
779806
&self,
@@ -788,39 +815,19 @@ impl<'cx, 'tcx> InferCtxtExt<'tcx> for BorrowckInferCtxt<'cx, 'tcx> {
788815
let (value, _map) = self.tcx.replace_late_bound_regions(value, |br| {
789816
debug!(?br);
790817
let liberated_region = self.tcx.mk_re_free(all_outlive_scope.to_def_id(), br.kind);
818+
#[cfg(not(debug_assertions))]
791819
let region_vid = self.next_nll_region_var(origin);
792-
indices.insert_late_bound_region(liberated_region, region_vid.to_region_vid());
793-
debug!(?liberated_region, ?region_vid);
794-
region_vid
795-
});
796-
value
797-
}
798820

799-
#[cfg(debug_assertions)]
800-
#[instrument(level = "debug", skip(self, indices))]
801-
fn replace_bound_regions_with_nll_infer_vars<T>(
802-
&self,
803-
origin: NllRegionVariableOrigin,
804-
all_outlive_scope: LocalDefId,
805-
value: ty::Binder<'tcx, T>,
806-
indices: &mut UniversalRegionIndices<'tcx>,
807-
) -> T
808-
where
809-
T: TypeFoldable<'tcx>,
810-
{
811-
let (value, _map) = self.tcx.replace_late_bound_regions(value, |br| {
812-
debug!(?br);
813-
let liberated_region = self.tcx.mk_region(ty::ReFree(ty::FreeRegion {
814-
scope: all_outlive_scope.to_def_id(),
815-
bound_region: br.kind,
816-
}));
821+
#[cfg(debug_assertions)]
822+
let region_vid = {
823+
let name = match br.kind.get_name() {
824+
Some(name) => name,
825+
_ => Symbol::intern("anon"),
826+
};
817827

818-
let name = match br.kind.get_name() {
819-
Some(name) => name,
820-
_ => Symbol::intern("anon"),
828+
self.next_nll_region_var(origin, RegionCtxt::Bound(BoundRegionInfo::Name(name)))
821829
};
822830

823-
let region_vid = self.next_nll_region_var(origin, RegionCtxt::Bound(name));
824831
indices.insert_late_bound_region(liberated_region, region_vid.to_region_vid());
825832
debug!(?liberated_region, ?region_vid);
826833
region_vid
@@ -837,7 +844,6 @@ impl<'cx, 'tcx> InferCtxtExt<'tcx> for BorrowckInferCtxt<'cx, 'tcx> {
837844
/// entries for them and store them in the indices map. This code iterates over the complete
838845
/// set of late-bound regions and checks for any that we have not yet seen, adding them to the
839846
/// inputs vector.
840-
#[cfg(not(debug_assertions))]
841847
#[instrument(skip(self, indices))]
842848
fn replace_late_bound_regions_with_nll_infer_vars_in_recursive_scope(
843849
&self,
@@ -847,7 +853,19 @@ impl<'cx, 'tcx> InferCtxtExt<'tcx> for BorrowckInferCtxt<'cx, 'tcx> {
847853
for_each_late_bound_region_in_recursive_scope(self.tcx, mir_def_id, |r| {
848854
debug!(?r);
849855
if !indices.indices.contains_key(&r) {
856+
#[cfg(not(debug_assertions))]
850857
let region_vid = self.next_nll_region_var(FR);
858+
859+
#[cfg(debug_assertions)]
860+
let region_vid = {
861+
let name = match r.get_name() {
862+
Some(name) => name,
863+
_ => Symbol::intern("anon"),
864+
};
865+
866+
self.next_nll_region_var(FR, RegionCtxt::LateBound(BoundRegionInfo::Name(name)))
867+
};
868+
851869
debug!(?region_vid);
852870
indices.insert_late_bound_region(r, region_vid.to_region_vid());
853871
}
@@ -863,40 +881,19 @@ impl<'cx, 'tcx> InferCtxtExt<'tcx> for BorrowckInferCtxt<'cx, 'tcx> {
863881
for_each_late_bound_region_in_item(self.tcx, mir_def_id, |r| {
864882
debug!(?r);
865883
if !indices.indices.contains_key(&r) {
884+
#[cfg(not(debug_assertions))]
866885
let region_vid = self.next_nll_region_var(FR);
867-
debug!(?region_vid);
868-
indices.insert_late_bound_region(r, region_vid.to_region_vid());
869-
}
870-
});
871-
}
872886

873-
/// Finds late-bound regions that do not appear in the parameter listing and adds them to the
874-
/// indices vector. Typically, we identify late-bound regions as we process the inputs and
875-
/// outputs of the closure/function. However, sometimes there are late-bound regions which do
876-
/// not appear in the fn parameters but which are nonetheless in scope. The simplest case of
877-
/// this are unused functions, like fn foo<'a>() { } (see e.g., #51351). Despite not being used,
878-
/// users can still reference these regions (e.g., let x: &'a u32 = &22;), so we need to create
879-
/// entries for them and store them in the indices map. This code iterates over the complete
880-
/// set of late-bound regions and checks for any that we have not yet seen, adding them to the
881-
/// inputs vector.
882-
#[cfg(debug_assertions)]
883-
#[instrument(skip(self, indices))]
884-
fn replace_late_bound_regions_with_nll_infer_vars(
885-
&self,
886-
mir_def_id: LocalDefId,
887-
indices: &mut UniversalRegionIndices<'tcx>,
888-
) {
889-
let typeck_root_def_id = self.tcx.typeck_root_def_id(mir_def_id.to_def_id());
890-
for_each_late_bound_region_defined_on(self.tcx, typeck_root_def_id, |r| {
891-
debug!(?r);
892-
if !indices.indices.contains_key(&r) {
893-
let name = match r.get_name() {
894-
Some(name) => name,
895-
_ => Symbol::intern("anon"),
887+
#[cfg(debug_assertions)]
888+
let region_vid = {
889+
let name = match r.get_name() {
890+
Some(name) => name,
891+
_ => Symbol::intern("anon"),
892+
};
893+
894+
self.next_nll_region_var(FR, RegionCtxt::LateBound(BoundRegionInfo::Name(name)))
896895
};
897896

898-
let region_vid = self.next_nll_region_var(FR, RegionCtxt::LateBound(name));
899-
debug!(?region_vid);
900897
indices.insert_late_bound_region(r, region_vid.to_region_vid());
901898
}
902899
});

0 commit comments

Comments
 (0)