Skip to content

Commit

Permalink
remove the sub/super terminology for universes
Browse files Browse the repository at this point in the history
Instead, we talk about:

- creating the "next" universe
- universes "extending" one another
- and `u1.can_name(u2)`, meaning that `u1` contains all names from `u2`
  • Loading branch information
nikomatsakis committed Oct 15, 2018
1 parent f419077 commit 05f67ca
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 38 deletions.
4 changes: 2 additions & 2 deletions src/librustc/infer/higher_ranked/mod.rs
Expand Up @@ -593,11 +593,11 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
where
T : TypeFoldable<'tcx>,
{
let new_universe = self.create_superuniverse();
let next_universe = self.create_next_universe();

let (result, map) = self.tcx.replace_late_bound_regions(binder, |br| {
self.tcx.mk_region(ty::RePlaceholder(ty::Placeholder {
universe: new_universe,
universe: next_universe,
name: br,
}))
});
Expand Down
11 changes: 4 additions & 7 deletions src/librustc/infer/mod.rs
Expand Up @@ -1489,13 +1489,10 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
self.universe.get()
}

/// Create and return a new subunivese of the current universe;
/// update `self.universe` to that new universe. At present,
/// used only in the NLL subtyping code, which uses the new
/// universe-based scheme instead of the more limited leak-check
/// scheme.
pub fn create_superuniverse(&self) -> ty::UniverseIndex {
let u = self.universe.get().superuniverse();
/// Create and return a fresh universe that extends all previous
/// universes. Updates `self.universe` to that new universe.
pub fn create_next_universe(&self) -> ty::UniverseIndex {
let u = self.universe.get().next_universe();
self.universe.set(u);
u
}
Expand Down
40 changes: 22 additions & 18 deletions src/librustc/ty/mod.rs
Expand Up @@ -1457,10 +1457,10 @@ impl<'tcx> InstantiatedPredicates<'tcx> {
/// "Universes" are used during type- and trait-checking in the
/// presence of `for<..>` binders to control what sets of names are
/// visible. Universes are arranged into a tree: the root universe
/// contains names that are always visible. But when you enter into
/// some superuniverse, then it may add names that are only visible
/// within that subtree (but it can still name the names of its
/// ancestor universes).
/// contains names that are always visible. Each child then adds a new
/// set of names that are visible, in addition to those of its parent.
/// We say that the child universe "extends" the parent universe with
/// new names.
///
/// To make this more concrete, consider this program:
///
Expand All @@ -1472,11 +1472,11 @@ impl<'tcx> InstantiatedPredicates<'tcx> {
/// ```
///
/// The struct name `Foo` is in the root universe U0. But the type
/// parameter `T`, introduced on `bar`, is in a superuniverse U1 --
/// i.e., within `bar`, we can name both `T` and `Foo`, but outside of
/// `bar`, we cannot name `T`. Then, within the type of `y`, the
/// region `'a` is in a superuniverse U2 of U1, because we can name it
/// inside the fn type but not outside.
/// parameter `T`, introduced on `bar`, is in an extended universe U1
/// -- i.e., within `bar`, we can name both `T` and `Foo`, but outside
/// of `bar`, we cannot name `T`. Then, within the type of `y`, the
/// region `'a` is in a universe U2 that extends U1, because we can
/// name it inside the fn type but not outside.
///
/// Universes are used to do type- and trait-checking around these
/// "forall" binders (also called **universal quantification**). The
Expand All @@ -1500,24 +1500,28 @@ impl_stable_hash_for!(struct UniverseIndex { private });
impl UniverseIndex {
pub const ROOT: UniverseIndex = UniverseIndex::from_u32_const(0);

/// A "superuniverse" corresponds to being inside a `forall` quantifier.
/// So, for example, suppose we have this type in universe `U`:
/// Returns the "next" universe index in order -- this new index
/// is considered to extend all previous universes. This
/// corresponds to entering a `forall` quantifier. So, for
/// example, suppose we have this type in universe `U`:
///
/// ```
/// for<'a> fn(&'a u32)
/// ```
///
/// Once we "enter" into this `for<'a>` quantifier, we are in a
/// superuniverse of `U` -- in this new universe, we can name the
/// region `'a`, but that region was not nameable from `U` because
/// it was not in scope there.
pub fn superuniverse(self) -> UniverseIndex {
/// new universe that extends `U` -- in this new universe, we can
/// name the region `'a`, but that region was not nameable from
/// `U` because it was not in scope there.
pub fn next_universe(self) -> UniverseIndex {
UniverseIndex::from_u32(self.private.checked_add(1).unwrap())
}

/// True if the names in this universe are a subset of the names in `other`.
pub fn is_subset_of(self, other: UniverseIndex) -> bool {
self.private <= other.private
/// True if `self` can name a name from `other` -- in other words,
/// if the set of names in `self` is a superset of those in
/// `other`.
pub fn can_name(self, other: UniverseIndex) -> bool {
self.private >= other.private
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/librustc_mir/borrow_check/nll/region_infer/mod.rs
Expand Up @@ -112,7 +112,7 @@ struct RegionDefinition<'tcx> {
/// Which universe is this region variable defined in? This is
/// most often `ty::UniverseIndex::ROOT`, but when we encounter
/// forall-quantifiers like `for<'a> { 'a = 'b }`, we would create
/// the variable for `'a` in a superuniverse.
/// the variable for `'a` in a fresh universe that extends ROOT.
universe: ty::UniverseIndex,

/// If this is 'static or an early-bound region, then this is
Expand Down Expand Up @@ -339,11 +339,11 @@ impl<'tcx> RegionInferenceContext<'tcx> {

NLLRegionVariableOrigin::Placeholder(placeholder) => {
// Each placeholder region is only visible from
// its universe `ui` and its superuniverses. So we
// its universe `ui` and its extensions. So we
// can't just add it into `scc` unless the
// universe of the scc can name this region.
let scc_universe = self.scc_universes[scc];
if placeholder.universe.is_subset_of(scc_universe) {
if scc_universe.can_name(placeholder.universe) {
self.scc_values.add_element(scc, placeholder);
} else {
self.add_incompatible_universe(scc);
Expand Down Expand Up @@ -541,7 +541,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
// Quick check: if scc_b's declared universe is a subset of
// scc_a's declared univese (typically, both are ROOT), then
// it cannot contain any problematic universe elements.
if self.scc_universes[scc_b].is_subset_of(universe_a) {
if universe_a.can_name(self.scc_universes[scc_b]) {
return true;
}

Expand All @@ -550,7 +550,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
// from universe_a
self.scc_values
.placeholders_contained_in(scc_b)
.all(|p| p.universe.is_subset_of(universe_a))
.all(|p| universe_a.can_name(p.universe))
}

/// Extend `scc` so that it can outlive some placeholder region
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_mir/borrow_check/nll/region_infer/values.rs
Expand Up @@ -148,8 +148,8 @@ crate enum RegionElement {
/// a lifetime parameter).
RootUniversalRegion(RegionVid),

/// A superuniverse from a superuniverse (e.g., instantiated from a
/// `for<'a> fn(&'a u32)` type).
/// A placeholder (e.g., instantiated from a `for<'a> fn(&'a u32)`
/// type).
PlaceholderRegion(ty::Placeholder),
}

Expand Down
8 changes: 4 additions & 4 deletions src/librustc_mir/borrow_check/nll/type_check/relate_tys.rs
Expand Up @@ -159,7 +159,7 @@ trait TypeRelatingDelegate<'tcx> {
fn push_outlives(&mut self, sup: ty::Region<'tcx>, sub: ty::Region<'tcx>);

/// Creates a new universe index. Used when instantiating placeholders.
fn next_superuniverse(&mut self) -> ty::UniverseIndex;
fn create_next_universe(&mut self) -> ty::UniverseIndex;

/// Creates a new region variable representing a higher-ranked
/// region that is instantiated existentially. This creates an
Expand Down Expand Up @@ -218,8 +218,8 @@ impl NllTypeRelatingDelegate<'me, 'bccx, 'gcx, 'tcx> {
}

impl TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, '_, 'tcx> {
fn next_superuniverse(&mut self) -> ty::UniverseIndex {
self.infcx.create_superuniverse()
fn create_next_universe(&mut self) -> ty::UniverseIndex {
self.infcx.create_next_universe()
}

fn next_existential_region_var(&mut self) -> ty::Region<'tcx> {
Expand Down Expand Up @@ -324,7 +324,7 @@ where
// new universe for the placeholders we will make
// from here out.
let universe = lazy_universe.unwrap_or_else(|| {
let universe = delegate.next_superuniverse();
let universe = delegate.create_next_universe();
lazy_universe = Some(universe);
universe
});
Expand Down

0 comments on commit 05f67ca

Please sign in to comment.