Skip to content

Commit 422208a

Browse files
Use UnordSet instead of FxHashSet for names_imported_by_glob_use query.
1 parent ee8bc5b commit 422208a

File tree

6 files changed

+42
-12
lines changed

6 files changed

+42
-12
lines changed

compiler/rustc_data_structures/src/unord.rs

+32-1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ impl<T, I: Iterator<Item = T>> UnordItems<T, I> {
109109
}
110110
}
111111

112+
impl<T> UnordItems<T, std::iter::Empty<T>> {
113+
pub fn empty() -> Self {
114+
UnordItems(std::iter::empty())
115+
}
116+
}
117+
112118
impl<'a, T: Clone + 'a, I: Iterator<Item = &'a T>> UnordItems<&'a T, I> {
113119
#[inline]
114120
pub fn cloned(self) -> UnordItems<T, impl Iterator<Item = T>> {
@@ -133,6 +139,20 @@ impl<T: Ord, I: Iterator<Item = T>> UnordItems<T, I> {
133139
items
134140
}
135141

142+
#[inline]
143+
pub fn into_sorted_stable_ord(self, use_stable_sort: bool) -> Vec<T>
144+
where
145+
T: Ord + StableOrd,
146+
{
147+
let mut items: Vec<T> = self.0.collect();
148+
if use_stable_sort {
149+
items.sort();
150+
} else {
151+
items.sort_unstable()
152+
}
153+
items
154+
}
155+
136156
pub fn into_sorted_small_vec<HCX, const LEN: usize>(self, hcx: &HCX) -> SmallVec<[T; LEN]>
137157
where
138158
T: ToStableHashKey<HCX>,
@@ -175,6 +195,11 @@ impl<V: Eq + Hash> UnordSet<V> {
175195
self.inner.len()
176196
}
177197

198+
#[inline]
199+
pub fn is_empty(&self) -> bool {
200+
self.inner.is_empty()
201+
}
202+
178203
#[inline]
179204
pub fn insert(&mut self, v: V) -> bool {
180205
self.inner.insert(v)
@@ -253,7 +278,7 @@ impl<V: Eq + Hash> UnordSet<V> {
253278
// We can safely extend this UnordSet from a set of unordered values because that
254279
// won't expose the internal ordering anywhere.
255280
#[inline]
256-
pub fn extend<I: Iterator<Item = V>>(&mut self, items: UnordItems<V, I>) {
281+
pub fn extend_unord<I: Iterator<Item = V>>(&mut self, items: UnordItems<V, I>) {
257282
self.inner.extend(items.0)
258283
}
259284

@@ -277,6 +302,12 @@ impl<V: Hash + Eq> FromIterator<V> for UnordSet<V> {
277302
}
278303
}
279304

305+
impl<V: Hash + Eq> From<FxHashSet<V>> for UnordSet<V> {
306+
fn from(value: FxHashSet<V>) -> Self {
307+
UnordSet { inner: value }
308+
}
309+
}
310+
280311
impl<HCX, V: Hash + Eq + HashStable<HCX>> HashStable<HCX> for UnordSet<V> {
281312
#[inline]
282313
fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) {

compiler/rustc_middle/src/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1826,7 +1826,7 @@ rustc_queries! {
18261826
query maybe_unused_trait_imports(_: ()) -> &'tcx FxIndexSet<LocalDefId> {
18271827
desc { "fetching potentially unused trait imports" }
18281828
}
1829-
query names_imported_by_glob_use(def_id: LocalDefId) -> &'tcx FxHashSet<Symbol> {
1829+
query names_imported_by_glob_use(def_id: LocalDefId) -> &'tcx UnordSet<Symbol> {
18301830
desc { |tcx| "finding names imported by glob use for `{}`", tcx.def_path_str(def_id.to_def_id()) }
18311831
}
18321832

compiler/rustc_middle/src/ty/context.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use rustc_data_structures::sharded::{IntoPointer, ShardedHashMap};
3737
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
3838
use rustc_data_structures::steal::Steal;
3939
use rustc_data_structures::sync::{self, Lock, Lrc, MappedReadGuard, ReadGuard, WorkerLocal};
40+
use rustc_data_structures::unord::UnordSet;
4041
use rustc_errors::{
4142
DecorateLint, DiagnosticBuilder, DiagnosticMessage, ErrorGuaranteed, MultiSpan,
4243
};
@@ -2488,7 +2489,9 @@ pub fn provide(providers: &mut ty::query::Providers) {
24882489
providers.maybe_unused_trait_imports =
24892490
|tcx, ()| &tcx.resolutions(()).maybe_unused_trait_imports;
24902491
providers.names_imported_by_glob_use = |tcx, id| {
2491-
tcx.arena.alloc(tcx.resolutions(()).glob_map.get(&id).cloned().unwrap_or_default())
2492+
tcx.arena.alloc(UnordSet::from(
2493+
tcx.resolutions(()).glob_map.get(&id).cloned().unwrap_or_default(),
2494+
))
24922495
};
24932496

24942497
providers.extern_mod_stmt_cnum =

compiler/rustc_middle/src/ty/query.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use rustc_arena::TypedArena;
4141
use rustc_ast as ast;
4242
use rustc_ast::expand::allocator::AllocatorKind;
4343
use rustc_attr as attr;
44-
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
44+
use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet};
4545
use rustc_data_structures::steal::Steal;
4646
use rustc_data_structures::svh::Svh;
4747
use rustc_data_structures::sync::Lrc;

compiler/rustc_passes/src/dead.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ impl<'tcx> Visitor<'tcx> for MarkSymbolVisitor<'tcx> {
371371
}
372372
if tcx.visibility(def_id).is_public() { Some(def_id) } else { None }
373373
});
374-
Extend::extend(&mut self.live_symbols, live_fields);
374+
self.live_symbols.extend(live_fields);
375375

376376
intravisit::walk_struct_def(self, def);
377377
}

src/tools/clippy/clippy_lints/src/wildcard_imports.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,10 @@ impl LateLintPass<'_> for WildcardImports {
155155
)
156156
};
157157

158-
let imports_string = if used_imports.len() == 1 {
159-
used_imports.iter().next().unwrap().to_string()
158+
let mut imports = used_imports.items().map(ToString::to_string).into_sorted_stable_ord(false);
159+
let imports_string = if imports.len() == 1 {
160+
imports.pop().unwrap()
160161
} else {
161-
let mut imports = used_imports
162-
.iter()
163-
.map(ToString::to_string)
164-
.collect::<Vec<_>>();
165-
imports.sort();
166162
if braced_glob {
167163
imports.join(", ")
168164
} else {

0 commit comments

Comments
 (0)