From 685e15dcc11bacb4861bdd8934245d4f4bdbe688 Mon Sep 17 00:00:00 2001 From: scalexm Date: Mon, 26 Nov 2018 13:44:40 +0100 Subject: [PATCH] Create multiple universes at once --- src/librustc/infer/canonical/mod.rs | 11 ++++------- src/librustc/infer/mod.rs | 11 +++++++++++ src/librustc/ty/mod.rs | 5 +++++ 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/librustc/infer/canonical/mod.rs b/src/librustc/infer/canonical/mod.rs index 230f8958b3385..3a8ff30faddf6 100644 --- a/src/librustc/infer/canonical/mod.rs +++ b/src/librustc/infer/canonical/mod.rs @@ -315,14 +315,11 @@ impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> { T: TypeFoldable<'tcx>, { // For each universe that is referred to in the incoming - // query, create a universe in our local inference context. In - // practice, as of this writing, all queries have no universes - // in them, so this code has no effect, but it is looking - // forward to the day when we *do* want to carry universes - // through into queries. + // query, create a universe in our local inference context. let universes: IndexVec = std::iter::once(ty::UniverseIndex::ROOT) - .chain((0..canonical.max_universe.as_u32()).map(|_| self.create_next_universe())) - .collect(); + .chain( + self.create_next_universes(canonical.max_universe.as_u32()) + ).collect(); let canonical_inference_vars = self.instantiate_canonical_vars(span, canonical.variables, |ui| universes[ui]); diff --git a/src/librustc/infer/mod.rs b/src/librustc/infer/mod.rs index d8beae45b0ad4..8ba1ef2e865b1 100644 --- a/src/librustc/infer/mod.rs +++ b/src/librustc/infer/mod.rs @@ -1519,6 +1519,17 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> { self.universe.set(u); u } + + /// Creates `amount` new universes at once and returns all the created universes + /// in ascending order. + pub fn create_next_universes(&self, amount: u32) -> impl Iterator { + let start = self.universe.get(); + let end = start.advance_by(amount); + self.universe.set(end); + + // Empty range if `amount == 0` + (start.next_universe() ..= end).into_iter() + } } impl<'a, 'gcx, 'tcx> TypeTrace<'tcx> { diff --git a/src/librustc/ty/mod.rs b/src/librustc/ty/mod.rs index b371f4532e5fa..fa6b1d7f18c2c 100644 --- a/src/librustc/ty/mod.rs +++ b/src/librustc/ty/mod.rs @@ -1568,6 +1568,11 @@ impl UniverseIndex { UniverseIndex::from_u32(self.private.checked_add(1).unwrap()) } + /// Increases the universe index by `amount`. + pub fn advance_by(self, amount: u32) -> UniverseIndex { + UniverseIndex::from_u32(self.private.checked_add(amount).unwrap()) + } + /// Returns `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` (`self >= other`).