Skip to content

Commit

Permalink
Add stable iteration API.
Browse files Browse the repository at this point in the history
  • Loading branch information
hameerabbasi committed Apr 8, 2022
1 parent fb669bc commit d505d55
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 18 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_data_structures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ pub mod svh;
pub use ena::snapshot_vec;
pub mod memmap;
pub mod sorted_map;
pub mod stable_iterator;
pub mod stable_set;
#[macro_use]
pub mod stable_hasher;
Expand Down
73 changes: 73 additions & 0 deletions compiler/rustc_data_structures/src/stable_iterator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use std::iter::Chain;

use crate::stable_hasher::ToStableHashKey;

pub struct StableIterator<I: Iterator> {
inner: I,
}

impl<T, I: Iterator<Item = T>> StableIterator<I> {
#[inline]
pub fn map<U, F: Fn(T) -> U>(self, f: F) -> StableIterator<impl Iterator<Item = U>> {
StableIterator { inner: self.inner.map(f) }
}

#[inline]
pub fn into_sorted<HCX>(self, hcx: &HCX) -> Vec<T>
where
T: ToStableHashKey<HCX>,
{
let mut items: Vec<T> = self.inner.collect();
items.sort_by_cached_key(|x| x.to_stable_hash_key(hcx));
items
}

#[inline]
pub fn any<F: Fn(T) -> bool>(&mut self, f: F) -> bool {
self.inner.any(f)
}

#[inline]
pub fn all<F: Fn(T) -> bool>(&mut self, f: F) -> bool {
self.inner.all(f)
}

#[inline]
pub fn chain<J: Iterator<Item = I::Item>>(self, other: StableIterator<J>) -> StableChain<I, J> {
self.inner.chain(other.inner).into()
}
}

pub trait IntoStableIterator {
type IntoIter: Iterator;
fn into_stable_iter(self) -> StableIterator<Self::IntoIter>;
}

impl<I: Iterator, S: IntoIterator<Item = I::Item, IntoIter = I>> IntoStableIterator for S {
type IntoIter = I;

#[inline]
fn into_stable_iter(self) -> StableIterator<I> {
StableIterator { inner: self.into_iter() }
}
}

pub struct StableChain<I: Iterator, J: Iterator> {
inner: Chain<I, J>,
}

impl<I: Iterator, J: Iterator<Item = I::Item>> IntoStableIterator for StableChain<I, J> {
type IntoIter = Chain<I, J>;

#[inline]
fn into_stable_iter(self) -> StableIterator<Self::IntoIter> {
self.inner.into_stable_iter()
}
}

impl<I: Iterator, J: Iterator> From<Chain<I, J>> for StableChain<I, J> {
#[inline]
fn from(inner: Chain<I, J>) -> Self {
Self { inner }
}
}
43 changes: 29 additions & 14 deletions compiler/rustc_data_structures/src/stable_set.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
pub use rustc_hash::FxHashSet;
use std::borrow::Borrow;
use std::collections::hash_set;
use std::fmt;
use std::hash::Hash;

use crate::stable_hasher::{stable_hash_reduce, HashStable, StableHasher, ToStableHashKey};
use crate::stable_iterator::{IntoStableIterator, StableIterator};

/// A deterministic wrapper around FxHashSet that does not provide iteration support.
///
Expand Down Expand Up @@ -103,20 +105,8 @@ impl<T: Hash + Eq> StableSet<T> {
self.base.contains(value)
}

#[inline]
pub fn any<F>(&self, f: F) -> bool
where
F: FnMut(&T) -> bool,
{
self.base.iter().any(f)
}

#[inline]
pub fn all<F>(&self, f: F) -> bool
where
F: FnMut(&T) -> bool,
{
self.base.iter().all(f)
pub fn stable_iter<'a>(&'a self) -> StableIterator<hash_set::Iter<'a, T>> {
(&self).into_stable_iter()
}
}

Expand All @@ -143,6 +133,31 @@ where
}
}

impl<T: Eq + Hash> IntoStableIterator for StableSet<T> {
type IntoIter = hash_set::IntoIter<T>;
#[inline]
fn into_stable_iter(self) -> StableIterator<Self::IntoIter> {
self.base.into_stable_iter()
}
}

impl<'a, T: Eq + Hash> IntoStableIterator for &'a StableSet<T> {
type IntoIter = hash_set::Iter<'a, T>;
#[inline]
fn into_stable_iter(self) -> StableIterator<Self::IntoIter> {
self.base.iter().into_stable_iter()
}
}

impl<T> From<FxHashSet<T>> for StableSet<T>
where
T: Eq + Hash,
{
fn from(base: FxHashSet<T>) -> Self {
Self { base: base }
}
}

impl<T> Extend<T> for StableSet<T>
where
T: Eq + Hash,
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2952,7 +2952,9 @@ pub fn provide(providers: &mut ty::query::Providers) {
providers.maybe_unused_extern_crates =
|tcx, ()| &tcx.resolutions(()).maybe_unused_extern_crates[..];
providers.names_imported_by_glob_use = |tcx, id| {
tcx.arena.alloc(tcx.resolutions(()).glob_map.get(&id).cloned().unwrap_or_default())
&StableSet::from(
tcx.arena.alloc(tcx.resolutions(()).glob_map.get(&id).cloned().unwrap_or_default()),
)
};

providers.extern_mod_stmt_cnum =
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2763,7 +2763,6 @@ fn trimmed_def_paths(tcx: TyCtxt<'_>, (): ()) -> FxHashMap<DefId, Symbol> {
let unique_symbols_rev: &mut FxHashMap<(Namespace, Symbol), Option<DefId>> =
&mut FxHashMap::default();

let hcx = tcx.create_stable_hashing_context();
for symbol_set in tcx.resolutions(()).glob_map.values() {
for symbol in symbol_set {
unique_symbols_rev.insert((Namespace::TypeNS, *symbol), None);
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_resolve/src/late/lifetimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,9 +469,8 @@ fn do_resolve(
named_region_map
}

fn convert_named_region_map(named_region_map: NamedRegionMap, tcx: TyCtxt<'_>) -> ResolveLifetimes {
fn convert_named_region_map(named_region_map: NamedRegionMap) -> ResolveLifetimes {
let mut rl = ResolveLifetimes::default();
let hcx = tcx.create_stable_hashing_context();
for (hir_id, v) in named_region_map.defs {
let map = rl.defs.entry(hir_id.owner).or_default();
map.insert(hir_id.local_id, v);
Expand Down

0 comments on commit d505d55

Please sign in to comment.