Skip to content

Commit 3b8008f

Browse files
committed
track skol levels in the InferCtxt rather than via counter
1 parent afb4010 commit 3b8008f

File tree

5 files changed

+26
-85
lines changed

5 files changed

+26
-85
lines changed

src/librustc/infer/higher_ranked/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl<'a, 'gcx, 'tcx> CombineFields<'a, 'gcx, 'tcx> {
6060
// Second, we instantiate each bound region in the supertype with a
6161
// fresh concrete region.
6262
let (b_prime, skol_map) =
63-
self.infcx.skolemize_late_bound_regions(b, snapshot);
63+
self.infcx.skolemize_late_bound_regions(b);
6464

6565
debug!("a_prime={:?}", a_prime);
6666
debug!("b_prime={:?}", b_prime);
@@ -188,14 +188,13 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
188188
///
189189
/// See `README.md` for more details.
190190
pub fn skolemize_late_bound_regions<T>(&self,
191-
binder: &ty::Binder<T>,
192-
snapshot: &CombinedSnapshot<'a, 'tcx>)
191+
binder: &ty::Binder<T>)
193192
-> (T, SkolemizationMap<'tcx>)
194193
where T : TypeFoldable<'tcx>
195194
{
196195
let (result, map) = self.tcx.replace_late_bound_regions(binder, |br| {
197-
self.borrow_region_constraints()
198-
.push_skolemized(self.tcx, br, &snapshot.region_constraints_snapshot)
196+
self.universe.set(self.universe().subuniverse());
197+
self.tcx.mk_region(ty::ReSkolemized(self.universe(), br))
199198
});
200199

201200
debug!("skolemize_bound_regions(binder={:?}, result={:?}, map={:?})",
@@ -380,7 +379,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
380379
debug!("pop_skolemized({:?})", skol_map);
381380
let skol_regions: FxHashSet<_> = skol_map.values().cloned().collect();
382381
self.borrow_region_constraints()
383-
.pop_skolemized(self.tcx, &skol_regions, &snapshot.region_constraints_snapshot);
382+
.pop_skolemized(self.universe(), &skol_regions, &snapshot.region_constraints_snapshot);
383+
self.universe.set(snapshot.universe);
384384
if !skol_map.is_empty() {
385385
self.projection_cache.borrow_mut().rollback_skolemized(
386386
&snapshot.projection_cache_snapshot);

src/librustc/infer/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,7 @@ pub struct CombinedSnapshot<'a, 'tcx:'a> {
499499
float_snapshot: ut::Snapshot<ut::InPlace<ty::FloatVid>>,
500500
region_constraints_snapshot: RegionSnapshot,
501501
region_obligations_snapshot: usize,
502+
universe: ty::UniverseIndex,
502503
was_in_snapshot: bool,
503504
_in_progress_tables: Option<Ref<'a, ty::TypeckTables<'tcx>>>,
504505
}
@@ -627,6 +628,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
627628
float_snapshot: self.float_unification_table.borrow_mut().snapshot(),
628629
region_constraints_snapshot: self.borrow_region_constraints().start_snapshot(),
629630
region_obligations_snapshot: self.region_obligations.borrow().len(),
631+
universe: self.universe(),
630632
was_in_snapshot: in_snapshot,
631633
// Borrow tables "in progress" (i.e. during typeck)
632634
// to ban writes from within a snapshot to them.
@@ -644,10 +646,12 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
644646
float_snapshot,
645647
region_constraints_snapshot,
646648
region_obligations_snapshot,
649+
universe,
647650
was_in_snapshot,
648651
_in_progress_tables } = snapshot;
649652

650653
self.in_snapshot.set(was_in_snapshot);
654+
self.universe.set(universe);
651655

652656
self.projection_cache
653657
.borrow_mut()
@@ -676,6 +680,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
676680
float_snapshot,
677681
region_constraints_snapshot,
678682
region_obligations_snapshot: _,
683+
universe: _,
679684
was_in_snapshot,
680685
_in_progress_tables } = snapshot;
681686

@@ -820,7 +825,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
820825

821826
Some(self.commit_if_ok(|snapshot| {
822827
let (ty::SubtypePredicate { a_is_expected, a, b}, skol_map) =
823-
self.skolemize_late_bound_regions(predicate, snapshot);
828+
self.skolemize_late_bound_regions(predicate);
824829

825830
let cause_span = cause.span;
826831
let ok = self.at(cause, param_env).sub_exp(a_is_expected, a, b)?;
@@ -837,7 +842,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
837842
{
838843
self.commit_if_ok(|snapshot| {
839844
let (ty::OutlivesPredicate(r_a, r_b), skol_map) =
840-
self.skolemize_late_bound_regions(predicate, snapshot);
845+
self.skolemize_late_bound_regions(predicate);
841846
let origin =
842847
SubregionOrigin::from_obligation_cause(cause,
843848
|| RelateRegionParamBound(cause.span));

src/librustc/infer/region_constraints/mod.rs

Lines changed: 8 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc_data_structures::unify as ut;
2222
use ty::{self, Ty, TyCtxt};
2323
use ty::{Region, RegionVid};
2424
use ty::ReStatic;
25-
use ty::{BrFresh, ReLateBound, ReSkolemized, ReVar};
25+
use ty::{BrFresh, ReLateBound, ReVar};
2626

2727
use std::collections::BTreeMap;
2828
use std::{cmp, fmt, mem, u32};
@@ -45,9 +45,6 @@ pub struct RegionConstraintCollector<'tcx> {
4545
/// exist). This prevents us from making many such regions.
4646
glbs: CombineMap<'tcx>,
4747

48-
/// Number of skolemized variables currently active.
49-
skolemization_count: ty::UniverseIndex,
50-
5148
/// Global counter used during the GLB algorithm to create unique
5249
/// names for fresh bound regions
5350
bound_count: u32,
@@ -237,7 +234,6 @@ pub struct RegionVariableInfo {
237234
pub struct RegionSnapshot {
238235
length: usize,
239236
region_snapshot: ut::Snapshot<ut::InPlace<ty::RegionVid>>,
240-
skolemization_count: ty::UniverseIndex,
241237
}
242238

243239
/// When working with skolemized regions, we often wish to find all of
@@ -281,7 +277,6 @@ impl<'tcx> RegionConstraintCollector<'tcx> {
281277
data: RegionConstraintData::default(),
282278
lubs: FxHashMap(),
283279
glbs: FxHashMap(),
284-
skolemization_count: ty::UniverseIndex::ROOT,
285280
bound_count: 0,
286281
undo_log: Vec::new(),
287282
unification_table: ut::UnificationTable::new(),
@@ -327,14 +322,11 @@ impl<'tcx> RegionConstraintCollector<'tcx> {
327322
data,
328323
lubs,
329324
glbs,
330-
skolemization_count,
331325
bound_count: _,
332326
undo_log: _,
333327
unification_table,
334328
} = self;
335329

336-
assert_eq!(*skolemization_count, ty::UniverseIndex::ROOT);
337-
338330
// Clear the tables of (lubs, glbs), so that we will create
339331
// fresh regions if we do a LUB operation. As it happens,
340332
// LUB/GLB are not performed by the MIR type-checker, which is
@@ -369,20 +361,13 @@ impl<'tcx> RegionConstraintCollector<'tcx> {
369361
RegionSnapshot {
370362
length,
371363
region_snapshot: self.unification_table.snapshot(),
372-
skolemization_count: self.skolemization_count,
373364
}
374365
}
375366

376367
pub fn commit(&mut self, snapshot: RegionSnapshot) {
377368
debug!("RegionConstraintCollector: commit({})", snapshot.length);
378369
assert!(self.undo_log.len() > snapshot.length);
379370
assert!(self.undo_log[snapshot.length] == OpenSnapshot);
380-
assert!(
381-
self.skolemization_count == snapshot.skolemization_count,
382-
"failed to pop skolemized regions: {:?} now vs {:?} at start",
383-
self.skolemization_count,
384-
snapshot.skolemization_count
385-
);
386371

387372
if snapshot.length == 0 {
388373
self.undo_log.truncate(0);
@@ -402,7 +387,6 @@ impl<'tcx> RegionConstraintCollector<'tcx> {
402387
}
403388
let c = self.undo_log.pop().unwrap();
404389
assert!(c == OpenSnapshot);
405-
self.skolemization_count = snapshot.skolemization_count;
406390
self.unification_table.rollback_to(snapshot.region_snapshot);
407391
}
408392

@@ -469,46 +453,13 @@ impl<'tcx> RegionConstraintCollector<'tcx> {
469453
self.var_infos[vid].origin
470454
}
471455

472-
/// Creates a new skolemized region. Skolemized regions are fresh
473-
/// regions used when performing higher-ranked computations. They
474-
/// must be used in a very particular way and are never supposed
475-
/// to "escape" out into error messages or the code at large.
476-
///
477-
/// The idea is to always create a snapshot. Skolemized regions
478-
/// can be created in the context of this snapshot, but before the
479-
/// snapshot is committed or rolled back, they must be popped
480-
/// (using `pop_skolemized_regions`), so that their numbers can be
481-
/// recycled. Normally you don't have to think about this: you use
482-
/// the APIs in `higher_ranked/mod.rs`, such as
483-
/// `skolemize_late_bound_regions` and `plug_leaks`, which will
484-
/// guide you on this path (ensure that the `SkolemizationMap` is
485-
/// consumed and you are good). There are also somewhat extensive
486-
/// comments in `higher_ranked/README.md`.
487-
///
488-
/// The `snapshot` argument to this function is not really used;
489-
/// it's just there to make it explicit which snapshot bounds the
490-
/// skolemized region that results. It should always be the top-most snapshot.
491-
pub fn push_skolemized(
492-
&mut self,
493-
tcx: TyCtxt<'_, '_, 'tcx>,
494-
br: ty::BoundRegion,
495-
snapshot: &RegionSnapshot,
496-
) -> Region<'tcx> {
497-
assert!(self.in_snapshot());
498-
assert!(self.undo_log[snapshot.length] == OpenSnapshot);
499-
500-
let universe = self.skolemization_count.subuniverse();
501-
self.skolemization_count = universe;
502-
tcx.mk_region(ReSkolemized(universe, br))
503-
}
504-
505456
/// Removes all the edges to/from the skolemized regions that are
506457
/// in `skols`. This is used after a higher-ranked operation
507458
/// completes to remove all trace of the skolemized regions
508459
/// created in that time.
509460
pub fn pop_skolemized(
510461
&mut self,
511-
_tcx: TyCtxt<'_, '_, 'tcx>,
462+
skolemization_count: ty::UniverseIndex,
512463
skols: &FxHashSet<ty::Region<'tcx>>,
513464
snapshot: &RegionSnapshot,
514465
) {
@@ -517,24 +468,16 @@ impl<'tcx> RegionConstraintCollector<'tcx> {
517468
assert!(self.in_snapshot());
518469
assert!(self.undo_log[snapshot.length] == OpenSnapshot);
519470
assert!(
520-
self.skolemization_count.as_usize() >= skols.len(),
471+
skolemization_count.as_usize() >= skols.len(),
521472
"popping more skolemized variables than actually exist, \
522473
sc now = {:?}, skols.len = {:?}",
523-
self.skolemization_count,
474+
skolemization_count,
524475
skols.len()
525476
);
526477

527-
let last_to_pop = self.skolemization_count.subuniverse();
478+
let last_to_pop = skolemization_count.subuniverse();
528479
let first_to_pop = ty::UniverseIndex::from(last_to_pop.as_u32() - skols.len() as u32);
529480

530-
assert!(
531-
first_to_pop >= snapshot.skolemization_count,
532-
"popping more regions than snapshot contains, \
533-
sc now = {:?}, sc then = {:?}, skols.len = {:?}",
534-
self.skolemization_count,
535-
snapshot.skolemization_count,
536-
skols.len()
537-
);
538481
debug_assert! {
539482
skols.iter()
540483
.all(|&k| match *k {
@@ -545,8 +488,8 @@ impl<'tcx> RegionConstraintCollector<'tcx> {
545488
false
546489
}),
547490
"invalid skolemization keys or keys out of range ({:?}..{:?}): {:?}",
548-
snapshot.skolemization_count,
549-
self.skolemization_count,
491+
first_to_pop,
492+
last_to_pop,
550493
skols
551494
}
552495

@@ -563,7 +506,6 @@ impl<'tcx> RegionConstraintCollector<'tcx> {
563506
self.rollback_undo_entry(undo_entry);
564507
}
565508

566-
self.skolemization_count = snapshot.skolemization_count;
567509
return;
568510

569511
fn kill_constraint<'tcx>(
@@ -896,12 +838,7 @@ pub fn region_universe(var_infos: &VarInfos, region: Region<'_>) -> ty::Universe
896838

897839
impl fmt::Debug for RegionSnapshot {
898840
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
899-
write!(
900-
f,
901-
"RegionSnapshot(length={},skolemization={:?})",
902-
self.length,
903-
self.skolemization_count
904-
)
841+
write!(f, "RegionSnapshot(length={})", self.length)
905842
}
906843
}
907844

src/librustc/traits/project.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ pub fn poly_project_and_unify_type<'cx, 'gcx, 'tcx>(
188188
let infcx = selcx.infcx();
189189
infcx.commit_if_ok(|snapshot| {
190190
let (skol_predicate, skol_map) =
191-
infcx.skolemize_late_bound_regions(&obligation.predicate, snapshot);
191+
infcx.skolemize_late_bound_regions(&obligation.predicate);
192192

193193
let skol_obligation = obligation.with(skol_predicate);
194194
let r = match project_and_unify_type(selcx, &skol_obligation) {

src/librustc/traits/select.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,7 +1454,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
14541454
let poly_trait_predicate =
14551455
self.infcx().resolve_type_vars_if_possible(&obligation.predicate);
14561456
let (skol_trait_predicate, skol_map) =
1457-
self.infcx().skolemize_late_bound_regions(&poly_trait_predicate, snapshot);
1457+
self.infcx().skolemize_late_bound_regions(&poly_trait_predicate);
14581458
debug!("match_projection_obligation_against_definition_bounds: \
14591459
skol_trait_predicate={:?} skol_map={:?}",
14601460
skol_trait_predicate,
@@ -2282,7 +2282,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
22822282

22832283
self.in_snapshot(|this, snapshot| {
22842284
let (skol_ty, skol_map) =
2285-
this.infcx().skolemize_late_bound_regions(&ty, snapshot);
2285+
this.infcx().skolemize_late_bound_regions(&ty);
22862286
let Normalized { value: normalized_ty, mut obligations } =
22872287
project::normalize_with_depth(this,
22882288
param_env,
@@ -2502,7 +2502,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
25022502
let trait_obligations = self.in_snapshot(|this, snapshot| {
25032503
let poly_trait_ref = obligation.predicate.to_poly_trait_ref();
25042504
let (trait_ref, skol_map) =
2505-
this.infcx().skolemize_late_bound_regions(&poly_trait_ref, snapshot);
2505+
this.infcx().skolemize_late_bound_regions(&poly_trait_ref);
25062506
let cause = obligation.derived_cause(ImplDerivedObligation);
25072507
this.impl_or_trait_obligations(cause,
25082508
obligation.recursion_depth + 1,
@@ -3084,8 +3084,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
30843084
}
30853085

30863086
let (skol_obligation, skol_map) = self.infcx().skolemize_late_bound_regions(
3087-
&obligation.predicate,
3088-
snapshot);
3087+
&obligation.predicate);
30893088
let skol_obligation_trait_ref = skol_obligation.trait_ref;
30903089

30913090
let impl_substs = self.infcx.fresh_substs_for_item(obligation.cause.span,

0 commit comments

Comments
 (0)