From 12a230562ece9b0d29018a436676141054dc53b7 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Mon, 31 Jul 2017 07:40:24 +0300 Subject: [PATCH] change skolemizations to use universe index --- src/librustc/infer/region_inference/mod.rs | 34 +++++++++++----------- src/librustc/ty/mod.rs | 18 ++++++++++-- src/librustc/ty/sty.rs | 7 +---- src/librustc/util/ppaux.rs | 2 +- 4 files changed, 34 insertions(+), 27 deletions(-) diff --git a/src/librustc/infer/region_inference/mod.rs b/src/librustc/infer/region_inference/mod.rs index 5db162848bfe9..b197344bda914 100644 --- a/src/librustc/infer/region_inference/mod.rs +++ b/src/librustc/infer/region_inference/mod.rs @@ -216,7 +216,7 @@ pub struct RegionVarBindings<'a, 'gcx: 'a+'tcx, 'tcx: 'a> { lubs: RefCell>, glbs: RefCell>, - skolemization_count: Cell, + skolemization_count: Cell, bound_count: Cell, /// The undo log records actions that might later be undone. @@ -240,7 +240,7 @@ pub struct RegionVarBindings<'a, 'gcx: 'a+'tcx, 'tcx: 'a> { pub struct RegionSnapshot { length: usize, region_snapshot: ut::Snapshot>, - skolemization_count: u32, + skolemization_count: ty::UniverseIndex, } /// When working with skolemized regions, we often wish to find all of @@ -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()), @@ -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); @@ -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 @@ -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 @@ -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) } } diff --git a/src/librustc/ty/mod.rs b/src/librustc/ty/mod.rs index 8fbc14023ce66..af2bb1b1e7fab 100644 --- a/src/librustc/ty/mod.rs +++ b/src/librustc/ty/mod.rs @@ -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::*; @@ -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 { @@ -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 diff --git a/src/librustc/ty/sty.rs b/src/librustc/ty/sty.rs index d0ac7d0183a58..c1328552ccb4e 100644 --- a/src/librustc/ty/sty.rs +++ b/src/librustc/ty/sty.rs @@ -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 @@ -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), diff --git a/src/librustc/util/ppaux.rs b/src/librustc/util/ppaux.rs index 73dfc01718eb1..ba147443a71dc 100644 --- a/src/librustc/util/ppaux.rs +++ b/src/librustc/util/ppaux.rs @@ -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"),