Skip to content

Commit 04708ba

Browse files
committed
track skol levels in the InferCtxt rather than via counter
1 parent f5d240b commit 04708ba

File tree

5 files changed

+26
-87
lines changed

5 files changed

+26
-87
lines changed

src/librustc/infer/higher_ranked/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl<'a, 'gcx, 'tcx> CombineFields<'a, 'gcx, 'tcx> {
6262
// Second, we instantiate each bound region in the supertype with a
6363
// fresh concrete region.
6464
let (b_prime, skol_map) =
65-
self.infcx.skolemize_late_bound_regions(b, snapshot);
65+
self.infcx.skolemize_late_bound_regions(b);
6666

6767
debug!("a_prime={:?}", a_prime);
6868
debug!("b_prime={:?}", b_prime);
@@ -587,14 +587,13 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
587587
///
588588
/// [rustc guide]: https://rust-lang-nursery.github.io/rustc-guide/trait-hrtb.html
589589
pub fn skolemize_late_bound_regions<T>(&self,
590-
binder: &ty::Binder<T>,
591-
snapshot: &CombinedSnapshot<'a, 'tcx>)
590+
binder: &ty::Binder<T>)
592591
-> (T, SkolemizationMap<'tcx>)
593592
where T : TypeFoldable<'tcx>
594593
{
595594
let (result, map) = self.tcx.replace_late_bound_regions(binder, |br| {
596-
self.borrow_region_constraints()
597-
.push_skolemized(self.tcx, br, &snapshot.region_constraints_snapshot)
595+
self.universe.set(self.universe().subuniverse());
596+
self.tcx.mk_region(ty::ReSkolemized(self.universe(), br))
598597
});
599598

600599
debug!("skolemize_bound_regions(binder={:?}, result={:?}, map={:?})",
@@ -779,7 +778,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
779778
debug!("pop_skolemized({:?})", skol_map);
780779
let skol_regions: FxHashSet<_> = skol_map.values().cloned().collect();
781780
self.borrow_region_constraints()
782-
.pop_skolemized(self.tcx, &skol_regions, &snapshot.region_constraints_snapshot);
781+
.pop_skolemized(self.universe(), &skol_regions, &snapshot.region_constraints_snapshot);
782+
self.universe.set(snapshot.universe);
783783
if !skol_map.is_empty() {
784784
self.projection_cache.borrow_mut().rollback_skolemized(
785785
&snapshot.projection_cache_snapshot);

src/librustc/infer/mod.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,7 @@ pub struct CombinedSnapshot<'a, 'tcx:'a> {
501501
float_snapshot: ut::Snapshot<ut::InPlace<ty::FloatVid>>,
502502
region_constraints_snapshot: RegionSnapshot,
503503
region_obligations_snapshot: usize,
504+
universe: ty::UniverseIndex,
504505
was_in_snapshot: bool,
505506
_in_progress_tables: Option<Ref<'a, ty::TypeckTables<'tcx>>>,
506507
}
@@ -630,6 +631,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
630631
float_snapshot: self.float_unification_table.borrow_mut().snapshot(),
631632
region_constraints_snapshot: self.borrow_region_constraints().start_snapshot(),
632633
region_obligations_snapshot: self.region_obligations.borrow().len(),
634+
universe: self.universe(),
633635
was_in_snapshot: in_snapshot,
634636
// Borrow tables "in progress" (i.e. during typeck)
635637
// to ban writes from within a snapshot to them.
@@ -647,10 +649,12 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
647649
float_snapshot,
648650
region_constraints_snapshot,
649651
region_obligations_snapshot,
652+
universe,
650653
was_in_snapshot,
651654
_in_progress_tables } = snapshot;
652655

653656
self.in_snapshot.set(was_in_snapshot);
657+
self.universe.set(universe);
654658

655659
self.projection_cache
656660
.borrow_mut()
@@ -679,6 +683,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
679683
float_snapshot,
680684
region_constraints_snapshot,
681685
region_obligations_snapshot: _,
686+
universe: _,
682687
was_in_snapshot,
683688
_in_progress_tables } = snapshot;
684689

@@ -823,7 +828,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
823828

824829
Some(self.commit_if_ok(|snapshot| {
825830
let (ty::SubtypePredicate { a_is_expected, a, b}, skol_map) =
826-
self.skolemize_late_bound_regions(predicate, snapshot);
831+
self.skolemize_late_bound_regions(predicate);
827832

828833
let cause_span = cause.span;
829834
let ok = self.at(cause, param_env).sub_exp(a_is_expected, a, b)?;
@@ -840,7 +845,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
840845
{
841846
self.commit_if_ok(|snapshot| {
842847
let (ty::OutlivesPredicate(r_a, r_b), skol_map) =
843-
self.skolemize_late_bound_regions(predicate, snapshot);
848+
self.skolemize_late_bound_regions(predicate);
844849
let origin =
845850
SubregionOrigin::from_obligation_cause(cause,
846851
|| RelateRegionParamBound(cause.span));

src/librustc/infer/region_constraints/mod.rs

+8-73
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,48 +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). For more info on how skolemization
486-
/// for HRTBs works, see the [rustc guide].
487-
///
488-
/// [rustc guide]: https://rust-lang-nursery.github.io/rustc-guide/trait-hrtb.html
489-
///
490-
/// The `snapshot` argument to this function is not really used;
491-
/// it's just there to make it explicit which snapshot bounds the
492-
/// skolemized region that results. It should always be the top-most snapshot.
493-
pub fn push_skolemized(
494-
&mut self,
495-
tcx: TyCtxt<'_, '_, 'tcx>,
496-
br: ty::BoundRegion,
497-
snapshot: &RegionSnapshot,
498-
) -> Region<'tcx> {
499-
assert!(self.in_snapshot());
500-
assert!(self.undo_log[snapshot.length] == OpenSnapshot);
501-
502-
let universe = self.skolemization_count.subuniverse();
503-
self.skolemization_count = universe;
504-
tcx.mk_region(ReSkolemized(universe, br))
505-
}
506-
507456
/// Removes all the edges to/from the skolemized regions that are
508457
/// in `skols`. This is used after a higher-ranked operation
509458
/// completes to remove all trace of the skolemized regions
510459
/// created in that time.
511460
pub fn pop_skolemized(
512461
&mut self,
513-
_tcx: TyCtxt<'_, '_, 'tcx>,
462+
skolemization_count: ty::UniverseIndex,
514463
skols: &FxHashSet<ty::Region<'tcx>>,
515464
snapshot: &RegionSnapshot,
516465
) {
@@ -519,24 +468,16 @@ impl<'tcx> RegionConstraintCollector<'tcx> {
519468
assert!(self.in_snapshot());
520469
assert!(self.undo_log[snapshot.length] == OpenSnapshot);
521470
assert!(
522-
self.skolemization_count.as_usize() >= skols.len(),
471+
skolemization_count.as_usize() >= skols.len(),
523472
"popping more skolemized variables than actually exist, \
524473
sc now = {:?}, skols.len = {:?}",
525-
self.skolemization_count,
474+
skolemization_count,
526475
skols.len()
527476
);
528477

529-
let last_to_pop = self.skolemization_count.subuniverse();
478+
let last_to_pop = skolemization_count.subuniverse();
530479
let first_to_pop = ty::UniverseIndex::from(last_to_pop.as_u32() - skols.len() as u32);
531480

532-
assert!(
533-
first_to_pop >= snapshot.skolemization_count,
534-
"popping more regions than snapshot contains, \
535-
sc now = {:?}, sc then = {:?}, skols.len = {:?}",
536-
self.skolemization_count,
537-
snapshot.skolemization_count,
538-
skols.len()
539-
);
540481
debug_assert! {
541482
skols.iter()
542483
.all(|&k| match *k {
@@ -547,8 +488,8 @@ impl<'tcx> RegionConstraintCollector<'tcx> {
547488
false
548489
}),
549490
"invalid skolemization keys or keys out of range ({:?}..{:?}): {:?}",
550-
snapshot.skolemization_count,
551-
self.skolemization_count,
491+
first_to_pop,
492+
last_to_pop,
552493
skols
553494
}
554495

@@ -565,7 +506,6 @@ impl<'tcx> RegionConstraintCollector<'tcx> {
565506
self.rollback_undo_entry(undo_entry);
566507
}
567508

568-
self.skolemization_count = snapshot.skolemization_count;
569509
return;
570510

571511
fn kill_constraint<'tcx>(
@@ -900,12 +840,7 @@ impl<'tcx> RegionConstraintCollector<'tcx> {
900840

901841
impl fmt::Debug for RegionSnapshot {
902842
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
903-
write!(
904-
f,
905-
"RegionSnapshot(length={},skolemization={:?})",
906-
self.length,
907-
self.skolemization_count
908-
)
843+
write!(f, "RegionSnapshot(length={})", self.length)
909844
}
910845
}
911846

src/librustc/traits/project.rs

+1-1
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

+4-5
Original file line numberDiff line numberDiff line change
@@ -1509,7 +1509,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
15091509
let poly_trait_predicate =
15101510
self.infcx().resolve_type_vars_if_possible(&obligation.predicate);
15111511
let (skol_trait_predicate, skol_map) =
1512-
self.infcx().skolemize_late_bound_regions(&poly_trait_predicate, snapshot);
1512+
self.infcx().skolemize_late_bound_regions(&poly_trait_predicate);
15131513
debug!("match_projection_obligation_against_definition_bounds: \
15141514
skol_trait_predicate={:?} skol_map={:?}",
15151515
skol_trait_predicate,
@@ -2338,7 +2338,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
23382338

23392339
self.in_snapshot(|this, snapshot| {
23402340
let (skol_ty, skol_map) =
2341-
this.infcx().skolemize_late_bound_regions(&ty, snapshot);
2341+
this.infcx().skolemize_late_bound_regions(&ty);
23422342
let Normalized { value: normalized_ty, mut obligations } =
23432343
project::normalize_with_depth(this,
23442344
param_env,
@@ -2559,7 +2559,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
25592559
let trait_obligations = self.in_snapshot(|this, snapshot| {
25602560
let poly_trait_ref = obligation.predicate.to_poly_trait_ref();
25612561
let (trait_ref, skol_map) =
2562-
this.infcx().skolemize_late_bound_regions(&poly_trait_ref, snapshot);
2562+
this.infcx().skolemize_late_bound_regions(&poly_trait_ref);
25632563
let cause = obligation.derived_cause(ImplDerivedObligation);
25642564
this.impl_or_trait_obligations(cause,
25652565
obligation.recursion_depth + 1,
@@ -3142,8 +3142,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
31423142
}
31433143

31443144
let (skol_obligation, skol_map) = self.infcx().skolemize_late_bound_regions(
3145-
&obligation.predicate,
3146-
snapshot);
3145+
&obligation.predicate);
31473146
let skol_obligation_trait_ref = skol_obligation.trait_ref;
31483147

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

0 commit comments

Comments
 (0)