1
Fork 0

Use UnordSet instead of FxHashSet for names_imported_by_glob_use query.

This commit is contained in:
Michael Woerister 2023-02-21 15:18:10 +01:00
parent ee8bc5b0b2
commit 422208ae52
6 changed files with 42 additions and 12 deletions

View file

@ -109,6 +109,12 @@ impl<T, I: Iterator<Item = T>> UnordItems<T, I> {
} }
} }
impl<T> UnordItems<T, std::iter::Empty<T>> {
pub fn empty() -> Self {
UnordItems(std::iter::empty())
}
}
impl<'a, T: Clone + 'a, I: Iterator<Item = &'a T>> UnordItems<&'a T, I> { impl<'a, T: Clone + 'a, I: Iterator<Item = &'a T>> UnordItems<&'a T, I> {
#[inline] #[inline]
pub fn cloned(self) -> UnordItems<T, impl Iterator<Item = T>> { pub fn cloned(self) -> UnordItems<T, impl Iterator<Item = T>> {
@ -133,6 +139,20 @@ impl<T: Ord, I: Iterator<Item = T>> UnordItems<T, I> {
items items
} }
#[inline]
pub fn into_sorted_stable_ord(self, use_stable_sort: bool) -> Vec<T>
where
T: Ord + StableOrd,
{
let mut items: Vec<T> = self.0.collect();
if use_stable_sort {
items.sort();
} else {
items.sort_unstable()
}
items
}
pub fn into_sorted_small_vec<HCX, const LEN: usize>(self, hcx: &HCX) -> SmallVec<[T; LEN]> pub fn into_sorted_small_vec<HCX, const LEN: usize>(self, hcx: &HCX) -> SmallVec<[T; LEN]>
where where
T: ToStableHashKey<HCX>, T: ToStableHashKey<HCX>,
@ -175,6 +195,11 @@ impl<V: Eq + Hash> UnordSet<V> {
self.inner.len() self.inner.len()
} }
#[inline]
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
#[inline] #[inline]
pub fn insert(&mut self, v: V) -> bool { pub fn insert(&mut self, v: V) -> bool {
self.inner.insert(v) self.inner.insert(v)
@ -253,7 +278,7 @@ impl<V: Eq + Hash> UnordSet<V> {
// We can safely extend this UnordSet from a set of unordered values because that // We can safely extend this UnordSet from a set of unordered values because that
// won't expose the internal ordering anywhere. // won't expose the internal ordering anywhere.
#[inline] #[inline]
pub fn extend<I: Iterator<Item = V>>(&mut self, items: UnordItems<V, I>) { pub fn extend_unord<I: Iterator<Item = V>>(&mut self, items: UnordItems<V, I>) {
self.inner.extend(items.0) self.inner.extend(items.0)
} }
@ -277,6 +302,12 @@ impl<V: Hash + Eq> FromIterator<V> for UnordSet<V> {
} }
} }
impl<V: Hash + Eq> From<FxHashSet<V>> for UnordSet<V> {
fn from(value: FxHashSet<V>) -> Self {
UnordSet { inner: value }
}
}
impl<HCX, V: Hash + Eq + HashStable<HCX>> HashStable<HCX> for UnordSet<V> { impl<HCX, V: Hash + Eq + HashStable<HCX>> HashStable<HCX> for UnordSet<V> {
#[inline] #[inline]
fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) { fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) {

View file

@ -1826,7 +1826,7 @@ rustc_queries! {
query maybe_unused_trait_imports(_: ()) -> &'tcx FxIndexSet<LocalDefId> { query maybe_unused_trait_imports(_: ()) -> &'tcx FxIndexSet<LocalDefId> {
desc { "fetching potentially unused trait imports" } desc { "fetching potentially unused trait imports" }
} }
query names_imported_by_glob_use(def_id: LocalDefId) -> &'tcx FxHashSet<Symbol> { query names_imported_by_glob_use(def_id: LocalDefId) -> &'tcx UnordSet<Symbol> {
desc { |tcx| "finding names imported by glob use for `{}`", tcx.def_path_str(def_id.to_def_id()) } desc { |tcx| "finding names imported by glob use for `{}`", tcx.def_path_str(def_id.to_def_id()) }
} }

View file

@ -37,6 +37,7 @@ use rustc_data_structures::sharded::{IntoPointer, ShardedHashMap};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_data_structures::steal::Steal; use rustc_data_structures::steal::Steal;
use rustc_data_structures::sync::{self, Lock, Lrc, MappedReadGuard, ReadGuard, WorkerLocal}; use rustc_data_structures::sync::{self, Lock, Lrc, MappedReadGuard, ReadGuard, WorkerLocal};
use rustc_data_structures::unord::UnordSet;
use rustc_errors::{ use rustc_errors::{
DecorateLint, DiagnosticBuilder, DiagnosticMessage, ErrorGuaranteed, MultiSpan, DecorateLint, DiagnosticBuilder, DiagnosticMessage, ErrorGuaranteed, MultiSpan,
}; };
@ -2488,7 +2489,9 @@ pub fn provide(providers: &mut ty::query::Providers) {
providers.maybe_unused_trait_imports = providers.maybe_unused_trait_imports =
|tcx, ()| &tcx.resolutions(()).maybe_unused_trait_imports; |tcx, ()| &tcx.resolutions(()).maybe_unused_trait_imports;
providers.names_imported_by_glob_use = |tcx, id| { providers.names_imported_by_glob_use = |tcx, id| {
tcx.arena.alloc(tcx.resolutions(()).glob_map.get(&id).cloned().unwrap_or_default()) tcx.arena.alloc(UnordSet::from(
tcx.resolutions(()).glob_map.get(&id).cloned().unwrap_or_default(),
))
}; };
providers.extern_mod_stmt_cnum = providers.extern_mod_stmt_cnum =

View file

@ -41,7 +41,7 @@ use rustc_arena::TypedArena;
use rustc_ast as ast; use rustc_ast as ast;
use rustc_ast::expand::allocator::AllocatorKind; use rustc_ast::expand::allocator::AllocatorKind;
use rustc_attr as attr; use rustc_attr as attr;
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet}; use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet};
use rustc_data_structures::steal::Steal; use rustc_data_structures::steal::Steal;
use rustc_data_structures::svh::Svh; use rustc_data_structures::svh::Svh;
use rustc_data_structures::sync::Lrc; use rustc_data_structures::sync::Lrc;

View file

@ -371,7 +371,7 @@ impl<'tcx> Visitor<'tcx> for MarkSymbolVisitor<'tcx> {
} }
if tcx.visibility(def_id).is_public() { Some(def_id) } else { None } if tcx.visibility(def_id).is_public() { Some(def_id) } else { None }
}); });
Extend::extend(&mut self.live_symbols, live_fields); self.live_symbols.extend(live_fields);
intravisit::walk_struct_def(self, def); intravisit::walk_struct_def(self, def);
} }

View file

@ -155,14 +155,10 @@ impl LateLintPass<'_> for WildcardImports {
) )
}; };
let imports_string = if used_imports.len() == 1 { let mut imports = used_imports.items().map(ToString::to_string).into_sorted_stable_ord(false);
used_imports.iter().next().unwrap().to_string() let imports_string = if imports.len() == 1 {
imports.pop().unwrap()
} else { } else {
let mut imports = used_imports
.iter()
.map(ToString::to_string)
.collect::<Vec<_>>();
imports.sort();
if braced_glob { if braced_glob {
imports.join(", ") imports.join(", ")
} else { } else {