Skip to content

Commit

Permalink
change skolemizations to use universe index
Browse files Browse the repository at this point in the history
  • Loading branch information
nikomatsakis committed Nov 7, 2017
1 parent de2dcd6 commit 12a2305
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 27 deletions.
34 changes: 17 additions & 17 deletions src/librustc/infer/region_inference/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ pub struct RegionVarBindings<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {

lubs: RefCell<CombineMap<'tcx>>,
glbs: RefCell<CombineMap<'tcx>>,
skolemization_count: Cell<u32>,
skolemization_count: Cell<ty::UniverseIndex>,
bound_count: Cell<u32>,

/// The undo log records actions that might later be undone.
Expand All @@ -240,7 +240,7 @@ pub struct RegionVarBindings<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
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 @@ -362,7 +362,7 @@ impl<'a, 'gcx, 'tcx> RegionVarBindings<'a, 'gcx, 'tcx> {
givens: RefCell::new(FxHashSet()),
lubs: RefCell::new(FxHashMap()),
glbs: RefCell::new(FxHashMap()),
skolemization_count: Cell::new(0),
skolemization_count: Cell::new(ty::UniverseIndex::ROOT),
bound_count: Cell::new(0),
undo_log: RefCell::new(Vec::new()),
unification_table: RefCell::new(ut::UnificationTable::new()),
Expand All @@ -389,7 +389,7 @@ impl<'a, 'gcx, 'tcx> RegionVarBindings<'a, 'gcx, 'tcx> {
assert!(self.undo_log.borrow().len() > snapshot.length);
assert!((*self.undo_log.borrow())[snapshot.length] == OpenSnapshot);
assert!(self.skolemization_count.get() == snapshot.skolemization_count,
"failed to pop skolemized regions: {} now vs {} at start",
"failed to pop skolemized regions: {:?} now vs {:?} at start",
self.skolemization_count.get(),
snapshot.skolemization_count);

Expand Down Expand Up @@ -501,9 +501,9 @@ impl<'a, 'gcx, 'tcx> RegionVarBindings<'a, 'gcx, 'tcx> {
assert!(self.in_snapshot());
assert!(self.undo_log.borrow()[snapshot.length] == OpenSnapshot);

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

/// Removes all the edges to/from the skolemized regions that are
Expand All @@ -517,31 +517,31 @@ impl<'a, 'gcx, 'tcx> RegionVarBindings<'a, 'gcx, 'tcx> {

assert!(self.in_snapshot());
assert!(self.undo_log.borrow()[snapshot.length] == OpenSnapshot);
assert!(self.skolemization_count.get() as usize >= skols.len(),
assert!(self.skolemization_count.get().as_usize() >= skols.len(),
"popping more skolemized variables than actually exist, \
sc now = {}, skols.len = {}",
self.skolemization_count.get(),
self.skolemization_count.get().as_usize(),
skols.len());

let last_to_pop = self.skolemization_count.get();
let first_to_pop = last_to_pop - (skols.len() as u32);
let last_to_pop = self.skolemization_count.get().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.get(),
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.get(),
skols
Expand Down Expand Up @@ -1523,7 +1523,7 @@ impl<'tcx> fmt::Debug for RegionAndOrigin<'tcx> {

impl fmt::Debug for RegionSnapshot {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "RegionSnapshot(length={},skolemization={})",
write!(f, "RegionSnapshot(length={},skolemization={:?})",
self.length, self.skolemization_count)
}
}
Expand Down
18 changes: 15 additions & 3 deletions src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,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 @@ -1275,7 +1275,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 @@ -1295,7 +1295,19 @@ 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 from(v: u32) -> UniverseIndex {
UniverseIndex(v)
}

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

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

/// Gets the "depth" of this universe in the universe tree. This
Expand Down
7 changes: 1 addition & 6 deletions src/librustc/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,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 @@ -898,11 +898,6 @@ pub struct RegionVid {
pub index: u32,
}

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

#[derive(Clone, Copy, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
pub enum InferTy {
TyVar(TyVid),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/util/ppaux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ define_print! {
}

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

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

0 comments on commit 12a2305

Please sign in to comment.