Skip to content

Commit d505d55

Browse files
committed
Add stable iteration API.
1 parent fb669bc commit d505d55

File tree

6 files changed

+107
-18
lines changed

6 files changed

+107
-18
lines changed

compiler/rustc_data_structures/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ pub mod svh;
8787
pub use ena::snapshot_vec;
8888
pub mod memmap;
8989
pub mod sorted_map;
90+
pub mod stable_iterator;
9091
pub mod stable_set;
9192
#[macro_use]
9293
pub mod stable_hasher;
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
use std::iter::Chain;
2+
3+
use crate::stable_hasher::ToStableHashKey;
4+
5+
pub struct StableIterator<I: Iterator> {
6+
inner: I,
7+
}
8+
9+
impl<T, I: Iterator<Item = T>> StableIterator<I> {
10+
#[inline]
11+
pub fn map<U, F: Fn(T) -> U>(self, f: F) -> StableIterator<impl Iterator<Item = U>> {
12+
StableIterator { inner: self.inner.map(f) }
13+
}
14+
15+
#[inline]
16+
pub fn into_sorted<HCX>(self, hcx: &HCX) -> Vec<T>
17+
where
18+
T: ToStableHashKey<HCX>,
19+
{
20+
let mut items: Vec<T> = self.inner.collect();
21+
items.sort_by_cached_key(|x| x.to_stable_hash_key(hcx));
22+
items
23+
}
24+
25+
#[inline]
26+
pub fn any<F: Fn(T) -> bool>(&mut self, f: F) -> bool {
27+
self.inner.any(f)
28+
}
29+
30+
#[inline]
31+
pub fn all<F: Fn(T) -> bool>(&mut self, f: F) -> bool {
32+
self.inner.all(f)
33+
}
34+
35+
#[inline]
36+
pub fn chain<J: Iterator<Item = I::Item>>(self, other: StableIterator<J>) -> StableChain<I, J> {
37+
self.inner.chain(other.inner).into()
38+
}
39+
}
40+
41+
pub trait IntoStableIterator {
42+
type IntoIter: Iterator;
43+
fn into_stable_iter(self) -> StableIterator<Self::IntoIter>;
44+
}
45+
46+
impl<I: Iterator, S: IntoIterator<Item = I::Item, IntoIter = I>> IntoStableIterator for S {
47+
type IntoIter = I;
48+
49+
#[inline]
50+
fn into_stable_iter(self) -> StableIterator<I> {
51+
StableIterator { inner: self.into_iter() }
52+
}
53+
}
54+
55+
pub struct StableChain<I: Iterator, J: Iterator> {
56+
inner: Chain<I, J>,
57+
}
58+
59+
impl<I: Iterator, J: Iterator<Item = I::Item>> IntoStableIterator for StableChain<I, J> {
60+
type IntoIter = Chain<I, J>;
61+
62+
#[inline]
63+
fn into_stable_iter(self) -> StableIterator<Self::IntoIter> {
64+
self.inner.into_stable_iter()
65+
}
66+
}
67+
68+
impl<I: Iterator, J: Iterator> From<Chain<I, J>> for StableChain<I, J> {
69+
#[inline]
70+
fn from(inner: Chain<I, J>) -> Self {
71+
Self { inner }
72+
}
73+
}

compiler/rustc_data_structures/src/stable_set.rs

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
pub use rustc_hash::FxHashSet;
22
use std::borrow::Borrow;
3+
use std::collections::hash_set;
34
use std::fmt;
45
use std::hash::Hash;
56

67
use crate::stable_hasher::{stable_hash_reduce, HashStable, StableHasher, ToStableHashKey};
8+
use crate::stable_iterator::{IntoStableIterator, StableIterator};
79

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

106-
#[inline]
107-
pub fn any<F>(&self, f: F) -> bool
108-
where
109-
F: FnMut(&T) -> bool,
110-
{
111-
self.base.iter().any(f)
112-
}
113-
114-
#[inline]
115-
pub fn all<F>(&self, f: F) -> bool
116-
where
117-
F: FnMut(&T) -> bool,
118-
{
119-
self.base.iter().all(f)
108+
pub fn stable_iter<'a>(&'a self) -> StableIterator<hash_set::Iter<'a, T>> {
109+
(&self).into_stable_iter()
120110
}
121111
}
122112

@@ -143,6 +133,31 @@ where
143133
}
144134
}
145135

136+
impl<T: Eq + Hash> IntoStableIterator for StableSet<T> {
137+
type IntoIter = hash_set::IntoIter<T>;
138+
#[inline]
139+
fn into_stable_iter(self) -> StableIterator<Self::IntoIter> {
140+
self.base.into_stable_iter()
141+
}
142+
}
143+
144+
impl<'a, T: Eq + Hash> IntoStableIterator for &'a StableSet<T> {
145+
type IntoIter = hash_set::Iter<'a, T>;
146+
#[inline]
147+
fn into_stable_iter(self) -> StableIterator<Self::IntoIter> {
148+
self.base.iter().into_stable_iter()
149+
}
150+
}
151+
152+
impl<T> From<FxHashSet<T>> for StableSet<T>
153+
where
154+
T: Eq + Hash,
155+
{
156+
fn from(base: FxHashSet<T>) -> Self {
157+
Self { base: base }
158+
}
159+
}
160+
146161
impl<T> Extend<T> for StableSet<T>
147162
where
148163
T: Eq + Hash,

compiler/rustc_middle/src/ty/context.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2952,7 +2952,9 @@ pub fn provide(providers: &mut ty::query::Providers) {
29522952
providers.maybe_unused_extern_crates =
29532953
|tcx, ()| &tcx.resolutions(()).maybe_unused_extern_crates[..];
29542954
providers.names_imported_by_glob_use = |tcx, id| {
2955-
tcx.arena.alloc(tcx.resolutions(()).glob_map.get(&id).cloned().unwrap_or_default())
2955+
&StableSet::from(
2956+
tcx.arena.alloc(tcx.resolutions(()).glob_map.get(&id).cloned().unwrap_or_default()),
2957+
)
29562958
};
29572959

29582960
providers.extern_mod_stmt_cnum =

compiler/rustc_middle/src/ty/print/pretty.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2763,7 +2763,6 @@ fn trimmed_def_paths(tcx: TyCtxt<'_>, (): ()) -> FxHashMap<DefId, Symbol> {
27632763
let unique_symbols_rev: &mut FxHashMap<(Namespace, Symbol), Option<DefId>> =
27642764
&mut FxHashMap::default();
27652765

2766-
let hcx = tcx.create_stable_hashing_context();
27672766
for symbol_set in tcx.resolutions(()).glob_map.values() {
27682767
for symbol in symbol_set {
27692768
unique_symbols_rev.insert((Namespace::TypeNS, *symbol), None);

compiler/rustc_resolve/src/late/lifetimes.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,9 +469,8 @@ fn do_resolve(
469469
named_region_map
470470
}
471471

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

0 commit comments

Comments
 (0)