Skip to content

Commit 4b66c54

Browse files
committed
Merge trait QueryCacheKey into trait QueryKey.
We have two traits governing query keys, for no particular reason. This commit combines them.
1 parent 1dc8751 commit 4b66c54

3 files changed

Lines changed: 14 additions & 16 deletions

File tree

compiler/rustc_middle/src/query/caches.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,21 @@
1-
use std::fmt::Debug;
2-
use std::hash::Hash;
31
use std::sync::OnceLock;
42

53
use rustc_data_structures::sharded::ShardedHashMap;
6-
use rustc_data_structures::stable_hasher::HashStable;
74
pub use rustc_data_structures::vec_cache::VecCache;
85
use rustc_hir::def_id::LOCAL_CRATE;
96
use rustc_index::Idx;
107
use rustc_span::def_id::{DefId, DefIndex};
118

129
use crate::dep_graph::DepNodeIndex;
13-
use crate::ich::StableHashingContext;
14-
15-
/// Traits that all query keys must satisfy.
16-
pub trait QueryCacheKey = Hash + Eq + Copy + Debug + for<'a> HashStable<StableHashingContext<'a>>;
10+
use crate::query::keys::QueryKey;
1711

1812
/// Trait for types that serve as an in-memory cache for query results,
1913
/// for a given key (argument) type and value (return) type.
2014
///
2115
/// Types implementing this trait are associated with actual key/value types
2216
/// by the `Cache` associated type of the `rustc_middle::query::Key` trait.
2317
pub trait QueryCache: Sized {
24-
type Key: QueryCacheKey;
18+
type Key: QueryKey;
2519
type Value: Copy;
2620

2721
/// Returns the cached value (and other information) associated with the
@@ -53,7 +47,7 @@ impl<K, V> Default for DefaultCache<K, V> {
5347

5448
impl<K, V> QueryCache for DefaultCache<K, V>
5549
where
56-
K: QueryCacheKey,
50+
K: QueryKey,
5751
V: Copy,
5852
{
5953
type Key = K;
@@ -180,7 +174,7 @@ where
180174

181175
impl<K, V> QueryCache for VecCache<K, V, DepNodeIndex>
182176
where
183-
K: Idx + QueryCacheKey,
177+
K: Idx + QueryKey,
184178
V: Copy,
185179
{
186180
type Key = K;

compiler/rustc_middle/src/query/keys.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
//! Defines the set of legal keys that can be used in queries.
22
33
use std::ffi::OsStr;
4+
use std::fmt::Debug;
5+
use std::hash::Hash;
46

57
use rustc_ast::tokenstream::TokenStream;
8+
use rustc_data_structures::stable_hasher::HashStable;
69
use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE, LocalDefId, LocalModDefId};
710
use rustc_hir::hir_id::OwnerId;
811
use rustc_span::{DUMMY_SP, Ident, LocalExpnId, Span, Symbol};
912

1013
use crate::dep_graph::DepNodeIndex;
14+
use crate::ich::StableHashingContext;
1115
use crate::infer::canonical::CanonicalQueryInput;
1216
use crate::mir::mono::CollectionMode;
1317
use crate::query::{DefIdCache, DefaultCache, SingleCache, VecCache};
@@ -20,8 +24,10 @@ use crate::{mir, traits};
2024
#[derive(Copy, Clone, Debug)]
2125
pub struct LocalCrate;
2226

27+
pub trait QueryKeyBounds = Copy + Debug + Eq + Hash + for<'a> HashStable<StableHashingContext<'a>>;
28+
2329
/// Controls what types can legally be used as the key for a query.
24-
pub trait QueryKey: Sized {
30+
pub trait QueryKey: Sized + QueryKeyBounds {
2531
/// The type of in-memory cache to use for queries with this key type.
2632
///
2733
/// In practice the cache type must implement [`QueryCache`], though that
@@ -311,13 +317,13 @@ impl<'tcx> QueryKey for &'tcx OsStr {
311317

312318
/// Canonical query goals correspond to abstract trait operations that
313319
/// are not tied to any crate in particular.
314-
impl<'tcx, T> QueryKey for CanonicalQueryInput<'tcx, T> {
320+
impl<'tcx, T: QueryKeyBounds> QueryKey for CanonicalQueryInput<'tcx, T> {
315321
fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
316322
DUMMY_SP
317323
}
318324
}
319325

320-
impl<'tcx, T> QueryKey for (CanonicalQueryInput<'tcx, T>, bool) {
326+
impl<'tcx, T: QueryKeyBounds> QueryKey for (CanonicalQueryInput<'tcx, T>, bool) {
321327
fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
322328
DUMMY_SP
323329
}

compiler/rustc_middle/src/query/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use rustc_hir::def_id::LocalDefId;
22

3-
pub use self::caches::{
4-
DefIdCache, DefaultCache, QueryCache, QueryCacheKey, SingleCache, VecCache,
5-
};
3+
pub use self::caches::{DefIdCache, DefaultCache, QueryCache, SingleCache, VecCache};
64
pub use self::job::{QueryInfo, QueryJob, QueryJobId, QueryLatch, QueryWaiter};
75
pub use self::keys::{AsLocalQueryKey, LocalCrate, QueryKey};
86
pub use self::plumbing::{

0 commit comments

Comments
 (0)