Skip to content

Commit

Permalink
change skolemizations to use universe index
Browse files Browse the repository at this point in the history
This is sort of confusing "side step". All it does is to change the
representation of a skolemized region. but the source of that universe
index is not the inference context, which is what we eventually want,
but rather an internal counter in the region inference context.

We'll patch that up later. But doing this now ought to help with
confusing diffs later.
  • Loading branch information
sgrif committed May 2, 2018
1 parent e904d56 commit 360cbf2
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 29 deletions.
36 changes: 18 additions & 18 deletions src/librustc/infer/region_constraints/mod.rs
Expand Up @@ -48,7 +48,7 @@ pub struct RegionConstraintCollector<'tcx> {
glbs: CombineMap<'tcx>,

/// Number of skolemized variables currently active.
skolemization_count: u32,
skolemization_count: ty::UniverseIndex,

/// Global counter used during the GLB algorithm to create unique
/// names for fresh bound regions
Expand Down Expand Up @@ -233,7 +233,7 @@ type CombineMap<'tcx> = FxHashMap<TwoRegions<'tcx>, RegionVid>;
pub struct RegionSnapshot {
length: usize,
region_snapshot: ut::Snapshot<ut::InPlace<ty::RegionVid>>,
skolemization_count: u32,
skolemization_count: ty::UniverseIndex,
}

/// When working with skolemized regions, we often wish to find all of
Expand Down Expand Up @@ -277,7 +277,7 @@ impl<'tcx> RegionConstraintCollector<'tcx> {
data: RegionConstraintData::default(),
lubs: FxHashMap(),
glbs: FxHashMap(),
skolemization_count: 0,
skolemization_count: ty::UniverseIndex::ROOT,
bound_count: 0,
undo_log: Vec::new(),
unification_table: ut::UnificationTable::new(),
Expand Down Expand Up @@ -329,7 +329,7 @@ impl<'tcx> RegionConstraintCollector<'tcx> {
unification_table,
} = self;

assert_eq!(*skolemization_count, 0);
assert_eq!(*skolemization_count, ty::UniverseIndex::ROOT);

// Clear the tables of (lubs, glbs), so that we will create
// fresh regions if we do a LUB operation. As it happens,
Expand Down Expand Up @@ -375,7 +375,7 @@ impl<'tcx> RegionConstraintCollector<'tcx> {
assert!(self.undo_log[snapshot.length] == OpenSnapshot);
assert!(
self.skolemization_count == snapshot.skolemization_count,
"failed to pop skolemized regions: {} now vs {} at start",
"failed to pop skolemized regions: {:?} now vs {:?} at start",
self.skolemization_count,
snapshot.skolemization_count
);
Expand Down Expand Up @@ -485,9 +485,9 @@ impl<'tcx> RegionConstraintCollector<'tcx> {
assert!(self.in_snapshot());
assert!(self.undo_log[snapshot.length] == OpenSnapshot);

let sc = self.skolemization_count;
self.skolemization_count = sc + 1;
tcx.mk_region(ReSkolemized(ty::SkolemizedRegionVid { index: sc }, br))
let universe = self.skolemization_count.subuniverse();
self.skolemization_count = universe;
tcx.mk_region(ReSkolemized(universe, br))
}

/// Removes all the edges to/from the skolemized regions that are
Expand All @@ -505,34 +505,34 @@ impl<'tcx> RegionConstraintCollector<'tcx> {
assert!(self.in_snapshot());
assert!(self.undo_log[snapshot.length] == OpenSnapshot);
assert!(
self.skolemization_count as usize >= skols.len(),
self.skolemization_count.as_usize() >= skols.len(),
"popping more skolemized variables than actually exist, \
sc now = {}, skols.len = {}",
sc now = {:?}, skols.len = {:?}",
self.skolemization_count,
skols.len()
);

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

assert!(
first_to_pop >= snapshot.skolemization_count,
"popping more regions than snapshot contains, \
sc now = {}, sc then = {}, skols.len = {}",
sc now = {:?}, sc then = {:?}, skols.len = {:?}",
self.skolemization_count,
snapshot.skolemization_count,
skols.len()
);
debug_assert! {
skols.iter()
.all(|&k| match *k {
ty::ReSkolemized(index, _) =>
index.index >= first_to_pop &&
index.index < last_to_pop,
ty::ReSkolemized(universe, _) =>
universe >= first_to_pop &&
universe < last_to_pop,
_ =>
false
}),
"invalid skolemization keys or keys out of range ({}..{}): {:?}",
"invalid skolemization keys or keys out of range ({:?}..{:?}): {:?}",
snapshot.skolemization_count,
self.skolemization_count,
skols
Expand Down Expand Up @@ -867,7 +867,7 @@ impl fmt::Debug for RegionSnapshot {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"RegionSnapshot(length={},skolemization={})",
"RegionSnapshot(length={},skolemization={:?})",
self.length,
self.skolemization_count
)
Expand Down
20 changes: 17 additions & 3 deletions src/librustc/ty/mod.rs
Expand Up @@ -69,7 +69,7 @@ pub use self::sty::{ExistentialTraitRef, PolyExistentialTraitRef};
pub use self::sty::{ExistentialProjection, PolyExistentialProjection, Const};
pub use self::sty::{BoundRegion, EarlyBoundRegion, FreeRegion, Region};
pub use self::sty::RegionKind;
pub use self::sty::{TyVid, IntVid, FloatVid, RegionVid, SkolemizedRegionVid};
pub use self::sty::{TyVid, IntVid, FloatVid, RegionVid};
pub use self::sty::BoundRegion::*;
pub use self::sty::InferTy::*;
pub use self::sty::RegionKind::*;
Expand Down Expand Up @@ -1370,7 +1370,7 @@ impl<'tcx> InstantiatedPredicates<'tcx> {
/// type name in a non-zero universe is a skolemized type -- an
/// idealized representative of "types in general" that we use for
/// checking generic functions.
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
pub struct UniverseIndex(u32);

impl UniverseIndex {
Expand All @@ -1390,7 +1390,21 @@ impl UniverseIndex {
/// region `'a`, but that region was not nameable from `U` because
/// it was not in scope there.
pub fn subuniverse(self) -> UniverseIndex {
UniverseIndex(self.0 + 1)
UniverseIndex(self.0.checked_add(1).unwrap())
}

pub fn as_u32(&self) -> u32 {
self.0
}

pub fn as_usize(&self) -> usize {
self.0 as usize
}
}

impl From<u32> for UniverseIndex {
fn from(index: u32) -> Self {
UniverseIndex(index)
}
}

Expand Down
7 changes: 1 addition & 6 deletions src/librustc/ty/sty.rs
Expand Up @@ -1021,7 +1021,7 @@ pub enum RegionKind {

/// A skolemized region - basically the higher-ranked version of ReFree.
/// Should not exist after typeck.
ReSkolemized(SkolemizedRegionVid, BoundRegion),
ReSkolemized(ty::UniverseIndex, BoundRegion),

/// Empty lifetime is for data that is never accessed.
/// Bottom in the region lattice. We treat ReEmpty somewhat
Expand Down Expand Up @@ -1075,11 +1075,6 @@ newtype_index!(RegionVid
DEBUG_FORMAT = custom,
});

#[derive(Clone, Copy, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, PartialOrd, Ord)]
pub struct SkolemizedRegionVid {
pub index: u32,
}

#[derive(Clone, Copy, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
pub enum InferTy {
TyVar(TyVid),
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/util/ppaux.rs
Expand Up @@ -808,8 +808,8 @@ define_print! {
write!(f, "'?{}", c.index())
}

ty::ReSkolemized(id, ref bound_region) => {
write!(f, "ReSkolemized({}, {:?})", id.index, bound_region)
ty::ReSkolemized(universe, ref bound_region) => {
write!(f, "ReSkolemized({:?}, {:?})", universe, bound_region)
}

ty::ReEmpty => write!(f, "ReEmpty"),
Expand Down

0 comments on commit 360cbf2

Please sign in to comment.