Skip to content

Commit

Permalink
Handle rustc_middle cases of rustc::potential_query_instability lint
Browse files Browse the repository at this point in the history
  • Loading branch information
ismailarilik committed Oct 4, 2024
1 parent 11ee3a8 commit 0000613
Show file tree
Hide file tree
Showing 16 changed files with 64 additions and 71 deletions.
19 changes: 10 additions & 9 deletions compiler/rustc_data_structures/src/sharded.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::borrow::Borrow;
use std::collections::hash_map::RawEntryMut;
use std::hash::{Hash, Hasher};
use std::{iter, mem};

#[cfg(parallel_compiler)]
use either::Either;
use indexmap::map::RawEntryApiV1;
use indexmap::map::raw_entry_v1::RawEntryMut;

use crate::fx::{FxHashMap, FxHasher};
use crate::fx::{FxHasher, FxIndexMap};
#[cfg(parallel_compiler)]
use crate::sync::{CacheAligned, is_dyn_thread_safe};
use crate::sync::{Lock, LockGuard, Mode};
Expand Down Expand Up @@ -159,15 +160,15 @@ pub fn shards() -> usize {
1
}

pub type ShardedHashMap<K, V> = Sharded<FxHashMap<K, V>>;
pub type ShardedIndexMap<K, V> = Sharded<FxIndexMap<K, V>>;

impl<K: Eq, V> ShardedHashMap<K, V> {
impl<K: Eq, V> ShardedIndexMap<K, V> {
pub fn len(&self) -> usize {
self.lock_shards().map(|shard| shard.len()).sum()
}
}

impl<K: Eq + Hash + Copy> ShardedHashMap<K, ()> {
impl<K: Eq + Hash + Copy> ShardedIndexMap<K, ()> {
#[inline]
pub fn intern_ref<Q: ?Sized>(&self, value: &Q, make: impl FnOnce() -> K) -> K
where
Expand All @@ -176,7 +177,7 @@ impl<K: Eq + Hash + Copy> ShardedHashMap<K, ()> {
{
let hash = make_hash(value);
let mut shard = self.lock_shard_by_hash(hash);
let entry = shard.raw_entry_mut().from_key_hashed_nocheck(hash, value);
let entry = shard.raw_entry_mut_v1().from_key_hashed_nocheck(hash, value);

match entry {
RawEntryMut::Occupied(e) => *e.key(),
Expand All @@ -196,7 +197,7 @@ impl<K: Eq + Hash + Copy> ShardedHashMap<K, ()> {
{
let hash = make_hash(&value);
let mut shard = self.lock_shard_by_hash(hash);
let entry = shard.raw_entry_mut().from_key_hashed_nocheck(hash, &value);
let entry = shard.raw_entry_mut_v1().from_key_hashed_nocheck(hash, &value);

match entry {
RawEntryMut::Occupied(e) => *e.key(),
Expand All @@ -214,12 +215,12 @@ pub trait IntoPointer {
fn into_pointer(&self) -> *const ();
}

impl<K: Eq + Hash + Copy + IntoPointer> ShardedHashMap<K, ()> {
impl<K: Eq + Hash + Copy + IntoPointer> ShardedIndexMap<K, ()> {
pub fn contains_pointer_to<T: Hash + IntoPointer>(&self, value: &T) -> bool {
let hash = make_hash(&value);
let shard = self.lock_shard_by_hash(hash);
let value = value.into_pointer();
shard.raw_entry().from_hash(hash, |entry| entry.into_pointer() == value).is_some()
shard.raw_entry_v1().from_hash(hash, |entry| entry.into_pointer() == value).is_some()
}
}

Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_middle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
// tidy-alphabetical-start
#![allow(internal_features)]
#![allow(rustc::diagnostic_outside_of_impl)]
#![allow(rustc::potential_query_instability)]
#![allow(rustc::untranslatable_diagnostic)]
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
#![doc(rust_logo)]
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1920,7 +1920,7 @@ rustc_queries! {
query maybe_unused_trait_imports(_: ()) -> &'tcx FxIndexSet<LocalDefId> {
desc { "fetching potentially unused trait imports" }
}
query names_imported_by_glob_use(def_id: LocalDefId) -> &'tcx UnordSet<Symbol> {
query names_imported_by_glob_use(def_id: LocalDefId) -> &'tcx FxIndexSet<Symbol> {
desc { |tcx| "finding names imported by glob use for `{}`", tcx.def_path_str(def_id) }
}

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/query/on_disk_cache.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::hash_map::Entry;
use std::mem;

use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet};
use rustc_data_structures::memmap::Mmap;
use rustc_data_structures::sync::{HashMapExt, Lock, Lrc, RwLock};
use rustc_data_structures::unhash::UnhashMap;
Expand Down Expand Up @@ -54,7 +54,7 @@ pub struct OnDiskCache<'sess> {

// Collects all `QuerySideEffects` created during the current compilation
// session.
current_side_effects: Lock<FxHashMap<DepNodeIndex, QuerySideEffects>>,
current_side_effects: Lock<FxIndexMap<DepNodeIndex, QuerySideEffects>>,

source_map: &'sess SourceMap,
file_index_to_stable_id: FxHashMap<SourceFileIndex, EncodedSourceFileId>,
Expand Down
9 changes: 3 additions & 6 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::intern::Interned;
use rustc_data_structures::profiling::SelfProfilerRef;
use rustc_data_structures::sharded::{IntoPointer, ShardedHashMap};
use rustc_data_structures::sharded::{IntoPointer, ShardedIndexMap};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_data_structures::steal::Steal;
use rustc_data_structures::sync::{self, FreezeReadGuard, Lock, Lrc, RwLock, WorkerLocal};
#[cfg(parallel_compiler)]
use rustc_data_structures::sync::{DynSend, DynSync};
use rustc_data_structures::unord::UnordSet;
use rustc_errors::{
Applicability, Diag, DiagCtxtHandle, ErrorGuaranteed, LintDiagnostic, MultiSpan,
};
Expand Down Expand Up @@ -698,7 +697,7 @@ impl<'tcx> rustc_type_ir::inherent::Features<TyCtxt<'tcx>> for &'tcx rustc_featu
}
}

type InternedSet<'tcx, T> = ShardedHashMap<InternedInSet<'tcx, T>, ()>;
type InternedSet<'tcx, T> = ShardedIndexMap<InternedInSet<'tcx, T>, ()>;

pub struct CtxtInterners<'tcx> {
/// The arena that types, regions, etc. are allocated from.
Expand Down Expand Up @@ -3207,9 +3206,7 @@ pub fn provide(providers: &mut Providers) {
providers.maybe_unused_trait_imports =
|tcx, ()| &tcx.resolutions(()).maybe_unused_trait_imports;
providers.names_imported_by_glob_use = |tcx, id| {
tcx.arena.alloc(UnordSet::from(
tcx.resolutions(()).glob_map.get(&id).cloned().unwrap_or_default(),
))
tcx.arena.alloc(tcx.resolutions(()).glob_map.get(&id).cloned().unwrap_or_default())
};

providers.extern_mod_stmt_cnum =
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/ty/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::borrow::Cow;
use std::fmt::Write;
use std::ops::ControlFlow;

use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::fx::FxIndexMap;
use rustc_errors::{Applicability, Diag, DiagArgValue, IntoDiagArg, into_diag_arg_using_display};
use rustc_hir::def::DefKind;
use rustc_hir::def_id::DefId;
Expand Down Expand Up @@ -276,7 +276,7 @@ pub fn suggest_constraining_type_params<'a>(
param_names_and_constraints: impl Iterator<Item = (&'a str, &'a str, Option<DefId>)>,
span_to_replace: Option<Span>,
) -> bool {
let mut grouped = FxHashMap::default();
let mut grouped = FxIndexMap::default();
param_names_and_constraints.for_each(|(param_name, constraint, def_id)| {
grouped.entry(param_name).or_insert(Vec::new()).push((constraint, def_id))
});
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ pub struct ResolverGlobalCtxt {
pub extern_crate_map: FxHashMap<LocalDefId, CrateNum>,
pub maybe_unused_trait_imports: FxIndexSet<LocalDefId>,
pub module_children: LocalDefIdMap<Vec<ModChild>>,
pub glob_map: FxHashMap<LocalDefId, FxHashSet<Symbol>>,
pub glob_map: FxIndexMap<LocalDefId, FxIndexSet<Symbol>>,
pub main_def: Option<MainDefinition>,
pub trait_impls: FxIndexMap<DefId, Vec<LocalDefId>>,
/// A list of proc macro LocalDefIds, written out in the order in which
Expand Down
30 changes: 13 additions & 17 deletions compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::ops::{Deref, DerefMut};

use rustc_apfloat::Float;
use rustc_apfloat::ieee::{Double, Half, Quad, Single};
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
use rustc_data_structures::fx::{FxIndexMap, IndexEntry};
use rustc_data_structures::unord::UnordMap;
use rustc_hir as hir;
use rustc_hir::LangItem;
Expand Down Expand Up @@ -3338,8 +3338,8 @@ pub fn trimmed_def_paths(tcx: TyCtxt<'_>, (): ()) -> DefIdMap<Symbol> {

// Once constructed, unique namespace+symbol pairs will have a `Some(_)` entry, while
// non-unique pairs will have a `None` entry.
let unique_symbols_rev: &mut FxHashMap<(Namespace, Symbol), Option<DefId>> =
&mut FxHashMap::default();
let unique_symbols_rev: &mut FxIndexMap<(Namespace, Symbol), Option<DefId>> =
&mut FxIndexMap::default();

for symbol_set in tcx.resolutions(()).glob_map.values() {
for symbol in symbol_set {
Expand All @@ -3349,27 +3349,23 @@ pub fn trimmed_def_paths(tcx: TyCtxt<'_>, (): ()) -> DefIdMap<Symbol> {
}
}

for_each_def(tcx, |ident, ns, def_id| {
use std::collections::hash_map::Entry::{Occupied, Vacant};

match unique_symbols_rev.entry((ns, ident.name)) {
Occupied(mut v) => match v.get() {
None => {}
Some(existing) => {
if *existing != def_id {
v.insert(None);
}
for_each_def(tcx, |ident, ns, def_id| match unique_symbols_rev.entry((ns, ident.name)) {
IndexEntry::Occupied(mut v) => match v.get() {
None => {}
Some(existing) => {
if *existing != def_id {
v.insert(None);
}
},
Vacant(v) => {
v.insert(Some(def_id));
}
},
IndexEntry::Vacant(v) => {
v.insert(Some(def_id));
}
});

// Put the symbol from all the unique namespace+symbol pairs into `map`.
let mut map: DefIdMap<Symbol> = Default::default();
for ((_, symbol), opt_def_id) in unique_symbols_rev.drain() {
for ((_, symbol), opt_def_id) in unique_symbols_rev.drain(..) {
use std::collections::hash_map::Entry::{Occupied, Vacant};

if let Some(def_id) = opt_def_id {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1086,7 +1086,7 @@ pub struct Resolver<'ra, 'tcx> {
empty_disambiguator: u32,

/// Maps glob imports to the names of items actually imported.
glob_map: FxHashMap<LocalDefId, FxHashSet<Symbol>>,
glob_map: FxIndexMap<LocalDefId, FxIndexSet<Symbol>>,
glob_error: Option<ErrorGuaranteed>,
visibilities_for_hashing: Vec<(LocalDefId, ty::Visibility)>,
used_imports: FxHashSet<NodeId>,
Expand Down
2 changes: 1 addition & 1 deletion src/tools/clippy/clippy_lints/src/wildcard_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ impl LateLintPass<'_> for WildcardImports {
(span, false)
};

let mut imports = used_imports.items().map(ToString::to_string).into_sorted_stable_ord();
let mut imports: Vec<_> = used_imports.iter().map(ToString::to_string).collect();
let imports_string = if imports.len() == 1 {
imports.pop().unwrap()
} else if braced_glob {
Expand Down
10 changes: 5 additions & 5 deletions src/tools/clippy/tests/ui/wildcard_imports.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ extern crate wildcard_imports_helper;

use crate::fn_mod::foo;
use crate::mod_mod::inner_mod;
use crate::multi_fn_mod::{multi_bar, multi_foo, multi_inner_mod};
use crate::multi_fn_mod::{multi_foo, multi_bar, multi_inner_mod};
#[macro_use]
use crate::struct_mod::{A, inner_struct_mod};

#[allow(unused_imports)]
use wildcard_imports_helper::inner::inner_for_self_import;
use wildcard_imports_helper::inner::inner_for_self_import::inner_extern_bar;
use wildcard_imports_helper::{ExternA, extern_foo};
use wildcard_imports_helper::{extern_foo, ExternA};

use std::io::prelude::*;
use wildcard_imports_helper::extern_prelude::v1::*;
Expand Down Expand Up @@ -129,7 +129,7 @@ mod in_fn_test {

fn test_extern() {
use wildcard_imports_helper::inner::inner_for_self_import::{self, inner_extern_foo};
use wildcard_imports_helper::{ExternA, extern_foo};
use wildcard_imports_helper::{extern_foo, ExternA};

inner_for_self_import::inner_extern_foo();
inner_extern_foo();
Expand All @@ -148,7 +148,7 @@ mod in_fn_test {
}

fn test_extern_reexported() {
use wildcard_imports_helper::{ExternExportedEnum, ExternExportedStruct, extern_exported};
use wildcard_imports_helper::{extern_exported, ExternExportedStruct, ExternExportedEnum};

extern_exported();
let _ = ExternExportedStruct;
Expand Down Expand Up @@ -177,7 +177,7 @@ mod in_fn_test {
}

fn test_reexported() {
use crate::in_fn_test::{ExportedEnum, ExportedStruct, exported};
use crate::in_fn_test::{exported, ExportedStruct, ExportedEnum};

exported();
let _ = ExportedStruct;
Expand Down
10 changes: 5 additions & 5 deletions src/tools/clippy/tests/ui/wildcard_imports.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ error: usage of wildcard import
--> tests/ui/wildcard_imports.rs:17:5
|
LL | use crate::multi_fn_mod::*;
| ^^^^^^^^^^^^^^^^^^^^^^ help: try: `crate::multi_fn_mod::{multi_bar, multi_foo, multi_inner_mod}`
| ^^^^^^^^^^^^^^^^^^^^^^ help: try: `crate::multi_fn_mod::{multi_foo, multi_bar, multi_inner_mod}`

error: usage of wildcard import
--> tests/ui/wildcard_imports.rs:19:5
Expand All @@ -35,7 +35,7 @@ error: usage of wildcard import
--> tests/ui/wildcard_imports.rs:24:5
|
LL | use wildcard_imports_helper::*;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `wildcard_imports_helper::{ExternA, extern_foo}`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `wildcard_imports_helper::{extern_foo, ExternA}`

error: usage of wildcard import
--> tests/ui/wildcard_imports.rs:94:13
Expand All @@ -59,7 +59,7 @@ error: usage of wildcard import
--> tests/ui/wildcard_imports.rs:132:13
|
LL | use wildcard_imports_helper::*;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `wildcard_imports_helper::{ExternA, extern_foo}`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `wildcard_imports_helper::{extern_foo, ExternA}`

error: usage of wildcard import
--> tests/ui/wildcard_imports.rs:144:20
Expand All @@ -77,13 +77,13 @@ error: usage of wildcard import
--> tests/ui/wildcard_imports.rs:151:13
|
LL | use wildcard_imports_helper::*;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `wildcard_imports_helper::{ExternExportedEnum, ExternExportedStruct, extern_exported}`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `wildcard_imports_helper::{extern_exported, ExternExportedStruct, ExternExportedEnum}`

error: usage of wildcard import
--> tests/ui/wildcard_imports.rs:180:9
|
LL | use crate::in_fn_test::*;
| ^^^^^^^^^^^^^^^^^^^^ help: try: `crate::in_fn_test::{ExportedEnum, ExportedStruct, exported}`
| ^^^^^^^^^^^^^^^^^^^^ help: try: `crate::in_fn_test::{exported, ExportedStruct, ExportedEnum}`

error: usage of wildcard import
--> tests/ui/wildcard_imports.rs:189:9
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ extern crate wildcard_imports_helper;

use crate::fn_mod::foo;
use crate::mod_mod::inner_mod;
use crate::multi_fn_mod::{multi_bar, multi_foo, multi_inner_mod};
use crate::multi_fn_mod::{multi_foo, multi_bar, multi_inner_mod};
use crate::struct_mod::{A, inner_struct_mod};

#[allow(unused_imports)]
use wildcard_imports_helper::inner::inner_for_self_import::inner_extern_bar;
use wildcard_imports_helper::prelude::v1::*;
use wildcard_imports_helper::{ExternA, extern_foo};
use wildcard_imports_helper::{extern_foo, ExternA};

use std::io::prelude::*;

Expand Down Expand Up @@ -123,7 +123,7 @@ mod in_fn_test {

fn test_extern() {
use wildcard_imports_helper::inner::inner_for_self_import::{self, inner_extern_foo};
use wildcard_imports_helper::{ExternA, extern_foo};
use wildcard_imports_helper::{extern_foo, ExternA};

inner_for_self_import::inner_extern_foo();
inner_extern_foo();
Expand All @@ -142,7 +142,7 @@ mod in_fn_test {
}

fn test_extern_reexported() {
use wildcard_imports_helper::{ExternExportedEnum, ExternExportedStruct, extern_exported};
use wildcard_imports_helper::{extern_exported, ExternExportedStruct, ExternExportedEnum};

extern_exported();
let _ = ExternExportedStruct;
Expand Down Expand Up @@ -171,7 +171,7 @@ mod in_fn_test {
}

fn test_reexported() {
use crate::in_fn_test::{ExportedEnum, ExportedStruct, exported};
use crate::in_fn_test::{exported, ExportedStruct, ExportedEnum};

exported();
let _ = ExportedStruct;
Expand Down
Loading

0 comments on commit 0000613

Please sign in to comment.