Rename trait Key as trait QueryKey`

Because `Key` is extremely generic and hard to search for.

Also rename `LocalKey` and `AsLocalKey` similarly, for consistency.
This commit is contained in:
Nicholas Nethercote
2026-02-18 14:35:24 +11:00
parent 6ba5b1ad9c
commit fbea6ddcd2
7 changed files with 66 additions and 66 deletions
@@ -51,7 +51,7 @@
use std::hash::Hash;
use rustc_data_structures::fingerprint::{Fingerprint, PackedFingerprint};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, StableOrd, ToStableHashKey};
use rustc_data_structures::stable_hasher::{StableHasher, StableOrd, ToStableHashKey};
use rustc_hir::def_id::DefId;
use rustc_hir::definitions::DefPathHash;
use rustc_macros::{Decodable, Encodable, HashStable};
+56 -57
View File
@@ -20,9 +20,8 @@
#[derive(Copy, Clone, Debug)]
pub struct LocalCrate;
/// The `Key` trait controls what types can legally be used as the key
/// for a query.
pub trait Key: Sized {
/// Controls what types can legally be used as the key for a query.
pub trait QueryKey: Sized {
/// The type of in-memory cache to use for queries with this key type.
///
/// In practice the cache type must implement [`QueryCache`], though that
@@ -47,15 +46,15 @@ fn def_id_for_ty_in_cycle(&self) -> Option<DefId> {
}
}
pub trait AsLocalKey: Key {
type LocalKey;
pub trait AsLocalQueryKey: QueryKey {
type LocalQueryKey;
/// Given an instance of this key, what crate is it referring to?
/// This is used to find the provider.
fn as_local_key(&self) -> Option<Self::LocalKey>;
fn as_local_key(&self) -> Option<Self::LocalQueryKey>;
}
impl Key for () {
impl QueryKey for () {
type Cache<V> = SingleCache<V>;
fn default_span(&self, _: TyCtxt<'_>) -> Span {
@@ -63,37 +62,37 @@ fn default_span(&self, _: TyCtxt<'_>) -> Span {
}
}
impl<'tcx> Key for ty::InstanceKind<'tcx> {
impl<'tcx> QueryKey for ty::InstanceKind<'tcx> {
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
tcx.def_span(self.def_id())
}
}
impl<'tcx> Key for ty::Instance<'tcx> {
impl<'tcx> QueryKey for ty::Instance<'tcx> {
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
tcx.def_span(self.def_id())
}
}
impl<'tcx> Key for mir::interpret::GlobalId<'tcx> {
impl<'tcx> QueryKey for mir::interpret::GlobalId<'tcx> {
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
self.instance.default_span(tcx)
}
}
impl<'tcx> Key for (Ty<'tcx>, Option<ty::ExistentialTraitRef<'tcx>>) {
impl<'tcx> QueryKey for (Ty<'tcx>, Option<ty::ExistentialTraitRef<'tcx>>) {
fn default_span(&self, _: TyCtxt<'_>) -> Span {
DUMMY_SP
}
}
impl<'tcx> Key for ty::LitToConstInput<'tcx> {
impl<'tcx> QueryKey for ty::LitToConstInput<'tcx> {
fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
DUMMY_SP
}
}
impl Key for CrateNum {
impl QueryKey for CrateNum {
type Cache<V> = VecCache<Self, V, DepNodeIndex>;
fn default_span(&self, _: TyCtxt<'_>) -> Span {
@@ -101,16 +100,16 @@ fn default_span(&self, _: TyCtxt<'_>) -> Span {
}
}
impl AsLocalKey for CrateNum {
type LocalKey = LocalCrate;
impl AsLocalQueryKey for CrateNum {
type LocalQueryKey = LocalCrate;
#[inline(always)]
fn as_local_key(&self) -> Option<Self::LocalKey> {
fn as_local_key(&self) -> Option<Self::LocalQueryKey> {
(*self == LOCAL_CRATE).then_some(LocalCrate)
}
}
impl Key for OwnerId {
impl QueryKey for OwnerId {
type Cache<V> = VecCache<Self, V, DepNodeIndex>;
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
@@ -122,7 +121,7 @@ fn key_as_def_id(&self) -> Option<DefId> {
}
}
impl Key for LocalDefId {
impl QueryKey for LocalDefId {
type Cache<V> = VecCache<Self, V, DepNodeIndex>;
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
@@ -134,7 +133,7 @@ fn key_as_def_id(&self) -> Option<DefId> {
}
}
impl Key for DefId {
impl QueryKey for DefId {
type Cache<V> = DefIdCache<V>;
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
@@ -147,16 +146,16 @@ fn key_as_def_id(&self) -> Option<DefId> {
}
}
impl AsLocalKey for DefId {
type LocalKey = LocalDefId;
impl AsLocalQueryKey for DefId {
type LocalQueryKey = LocalDefId;
#[inline(always)]
fn as_local_key(&self) -> Option<Self::LocalKey> {
fn as_local_key(&self) -> Option<Self::LocalQueryKey> {
self.as_local()
}
}
impl Key for LocalModDefId {
impl QueryKey for LocalModDefId {
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
tcx.def_span(*self)
}
@@ -167,19 +166,19 @@ fn key_as_def_id(&self) -> Option<DefId> {
}
}
impl Key for SimplifiedType {
impl QueryKey for SimplifiedType {
fn default_span(&self, _: TyCtxt<'_>) -> Span {
DUMMY_SP
}
}
impl Key for (DefId, DefId) {
impl QueryKey for (DefId, DefId) {
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
self.1.default_span(tcx)
}
}
impl Key for (DefId, Ident) {
impl QueryKey for (DefId, Ident) {
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
tcx.def_span(self.0)
}
@@ -190,73 +189,73 @@ fn key_as_def_id(&self) -> Option<DefId> {
}
}
impl Key for (LocalDefId, LocalDefId, Ident) {
impl QueryKey for (LocalDefId, LocalDefId, Ident) {
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
self.1.default_span(tcx)
}
}
impl Key for (CrateNum, DefId) {
impl QueryKey for (CrateNum, DefId) {
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
self.1.default_span(tcx)
}
}
impl AsLocalKey for (CrateNum, DefId) {
type LocalKey = DefId;
impl AsLocalQueryKey for (CrateNum, DefId) {
type LocalQueryKey = DefId;
#[inline(always)]
fn as_local_key(&self) -> Option<Self::LocalKey> {
fn as_local_key(&self) -> Option<Self::LocalQueryKey> {
(self.0 == LOCAL_CRATE).then(|| self.1)
}
}
impl Key for (CrateNum, SimplifiedType) {
impl QueryKey for (CrateNum, SimplifiedType) {
fn default_span(&self, _: TyCtxt<'_>) -> Span {
DUMMY_SP
}
}
impl AsLocalKey for (CrateNum, SimplifiedType) {
type LocalKey = SimplifiedType;
impl AsLocalQueryKey for (CrateNum, SimplifiedType) {
type LocalQueryKey = SimplifiedType;
#[inline(always)]
fn as_local_key(&self) -> Option<Self::LocalKey> {
fn as_local_key(&self) -> Option<Self::LocalQueryKey> {
(self.0 == LOCAL_CRATE).then(|| self.1)
}
}
impl Key for (DefId, ty::SizedTraitKind) {
impl QueryKey for (DefId, ty::SizedTraitKind) {
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
self.0.default_span(tcx)
}
}
impl<'tcx> Key for GenericArgsRef<'tcx> {
impl<'tcx> QueryKey for GenericArgsRef<'tcx> {
fn default_span(&self, _: TyCtxt<'_>) -> Span {
DUMMY_SP
}
}
impl<'tcx> Key for (DefId, GenericArgsRef<'tcx>) {
impl<'tcx> QueryKey for (DefId, GenericArgsRef<'tcx>) {
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
self.0.default_span(tcx)
}
}
impl<'tcx> Key for ty::TraitRef<'tcx> {
impl<'tcx> QueryKey for ty::TraitRef<'tcx> {
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
tcx.def_span(self.def_id)
}
}
impl<'tcx> Key for GenericArg<'tcx> {
impl<'tcx> QueryKey for GenericArg<'tcx> {
fn default_span(&self, _: TyCtxt<'_>) -> Span {
DUMMY_SP
}
}
impl<'tcx> Key for Ty<'tcx> {
impl<'tcx> QueryKey for Ty<'tcx> {
fn default_span(&self, _: TyCtxt<'_>) -> Span {
DUMMY_SP
}
@@ -270,19 +269,19 @@ fn def_id_for_ty_in_cycle(&self) -> Option<DefId> {
}
}
impl<'tcx> Key for (Ty<'tcx>, Ty<'tcx>) {
impl<'tcx> QueryKey for (Ty<'tcx>, Ty<'tcx>) {
fn default_span(&self, _: TyCtxt<'_>) -> Span {
DUMMY_SP
}
}
impl<'tcx> Key for ty::Clauses<'tcx> {
impl<'tcx> QueryKey for ty::Clauses<'tcx> {
fn default_span(&self, _: TyCtxt<'_>) -> Span {
DUMMY_SP
}
}
impl<'tcx, T: Key> Key for ty::PseudoCanonicalInput<'tcx, T> {
impl<'tcx, T: QueryKey> QueryKey for ty::PseudoCanonicalInput<'tcx, T> {
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
self.value.default_span(tcx)
}
@@ -292,19 +291,19 @@ fn def_id_for_ty_in_cycle(&self) -> Option<DefId> {
}
}
impl Key for Symbol {
impl QueryKey for Symbol {
fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
DUMMY_SP
}
}
impl Key for Option<Symbol> {
impl QueryKey for Option<Symbol> {
fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
DUMMY_SP
}
}
impl<'tcx> Key for &'tcx OsStr {
impl<'tcx> QueryKey for &'tcx OsStr {
fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
DUMMY_SP
}
@@ -312,55 +311,55 @@ fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
/// Canonical query goals correspond to abstract trait operations that
/// are not tied to any crate in particular.
impl<'tcx, T: Clone> Key for CanonicalQueryInput<'tcx, T> {
impl<'tcx, T: Clone> QueryKey for CanonicalQueryInput<'tcx, T> {
fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
DUMMY_SP
}
}
impl<'tcx, T: Clone> Key for (CanonicalQueryInput<'tcx, T>, bool) {
impl<'tcx, T: Clone> QueryKey for (CanonicalQueryInput<'tcx, T>, bool) {
fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
DUMMY_SP
}
}
impl<'tcx> Key for (Ty<'tcx>, rustc_abi::VariantIdx) {
impl<'tcx> QueryKey for (Ty<'tcx>, rustc_abi::VariantIdx) {
fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
DUMMY_SP
}
}
impl<'tcx> Key for (ty::Predicate<'tcx>, traits::WellFormedLoc) {
impl<'tcx> QueryKey for (ty::Predicate<'tcx>, traits::WellFormedLoc) {
fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
DUMMY_SP
}
}
impl<'tcx> Key for (ty::PolyFnSig<'tcx>, &'tcx ty::List<Ty<'tcx>>) {
impl<'tcx> QueryKey for (ty::PolyFnSig<'tcx>, &'tcx ty::List<Ty<'tcx>>) {
fn default_span(&self, _: TyCtxt<'_>) -> Span {
DUMMY_SP
}
}
impl<'tcx> Key for (ty::Instance<'tcx>, &'tcx ty::List<Ty<'tcx>>) {
impl<'tcx> QueryKey for (ty::Instance<'tcx>, &'tcx ty::List<Ty<'tcx>>) {
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
self.0.default_span(tcx)
}
}
impl<'tcx> Key for ty::Value<'tcx> {
impl<'tcx> QueryKey for ty::Value<'tcx> {
fn default_span(&self, _: TyCtxt<'_>) -> Span {
DUMMY_SP
}
}
impl<'tcx> Key for (LocalExpnId, &'tcx TokenStream) {
impl<'tcx> QueryKey for (LocalExpnId, &'tcx TokenStream) {
fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
self.0.expn_data().call_site
}
}
impl<'tcx> Key for (ValidityRequirement, ty::PseudoCanonicalInput<'tcx, Ty<'tcx>>) {
impl<'tcx> QueryKey for (ValidityRequirement, ty::PseudoCanonicalInput<'tcx, Ty<'tcx>>) {
// Just forward to `Ty<'tcx>`
fn default_span(&self, _: TyCtxt<'_>) -> Span {
@@ -375,7 +374,7 @@ fn def_id_for_ty_in_cycle(&self) -> Option<DefId> {
}
}
impl<'tcx> Key for (ty::Instance<'tcx>, CollectionMode) {
impl<'tcx> QueryKey for (ty::Instance<'tcx>, CollectionMode) {
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
self.0.default_span(tcx)
}
+1 -1
View File
@@ -4,7 +4,7 @@
DefIdCache, DefaultCache, QueryCache, QueryCacheKey, SingleCache, VecCache,
};
pub use self::job::{QueryInfo, QueryJob, QueryJobId, QueryLatch, QueryWaiter};
pub use self::keys::{AsLocalKey, Key, LocalCrate};
pub use self::keys::{AsLocalQueryKey, LocalCrate, QueryKey};
pub use self::plumbing::{
ActiveKeyStatus, CycleError, CycleErrorHandling, EnsureMode, IntoQueryParam, QueryMode,
QueryState, TyCtxtAt, TyCtxtEnsureDone, TyCtxtEnsureOk,
+2 -2
View File
@@ -383,7 +383,7 @@ pub mod $name {
pub type LocalKey<'tcx> = if_separate_provide_extern!(
[$($modifiers)*]
(<Key<'tcx> as $crate::query::AsLocalKey>::LocalKey)
(<Key<'tcx> as $crate::query::AsLocalQueryKey>::LocalQueryKey)
(Key<'tcx>)
);
@@ -428,7 +428,7 @@ pub fn provided_to_erased<'tcx>(
}
pub type Storage<'tcx> =
<Key<'tcx> as $crate::query::Key>::Cache<Erased<Value<'tcx>>>;
<Key<'tcx> as $crate::query::QueryKey>::Cache<Erased<Value<'tcx>>>;
// Ensure that keys grow no larger than 88 bytes by accident.
// Increase this limit if necessary, but do try to keep the size low if possible
+1 -1
View File
@@ -4,7 +4,7 @@
};
use rustc_macros::{Diagnostic, Subdiagnostic};
use rustc_middle::mir::AssertKind;
use rustc_middle::query::Key;
use rustc_middle::query::QueryKey;
use rustc_middle::ty::TyCtxt;
use rustc_session::lint::{self, Lint};
use rustc_span::def_id::DefId;
+1 -1
View File
@@ -13,7 +13,7 @@
use rustc_middle::queries::{self, ExternProviders, Providers};
use rustc_middle::query::on_disk_cache::{CacheEncoder, EncodedDepNodeIndex, OnDiskCache};
use rustc_middle::query::plumbing::{QuerySystem, QueryVTable};
use rustc_middle::query::{AsLocalKey, QueryCache, QueryMode};
use rustc_middle::query::{AsLocalQueryKey, QueryCache, QueryMode};
use rustc_middle::ty::TyCtxt;
use rustc_span::Span;
+4 -3
View File
@@ -19,7 +19,8 @@
};
use rustc_middle::query::plumbing::QueryVTable;
use rustc_middle::query::{
Key, QueryCache, QueryJobId, QueryStackDeferred, QueryStackFrame, QueryStackFrameExtra, erase,
QueryCache, QueryJobId, QueryKey, QueryStackDeferred, QueryStackFrame, QueryStackFrameExtra,
erase,
};
use rustc_middle::ty::codec::TyEncoder;
use rustc_middle::ty::print::with_reduced_queries;
@@ -274,7 +275,7 @@ fn mk_query_stack_frame_extra<'tcx, Cache>(
) -> QueryStackFrameExtra
where
Cache: QueryCache,
Cache::Key: Key,
Cache::Key: QueryKey,
{
let def_id = key.key_as_def_id();
@@ -313,7 +314,7 @@ pub(crate) fn create_deferred_query_stack_frame<'tcx, C>(
) -> QueryStackFrame<QueryStackDeferred<'tcx>>
where
C: QueryCache,
C::Key: Key + DynSend + DynSync,
C::Key: QueryKey + DynSend + DynSync,
QueryVTable<'tcx, C>: DynSync,
{
let kind = vtable.dep_kind;