From 79334ac9618519248f79a2060e8c8b0825492c29 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 14 Jan 2026 12:24:54 +1100 Subject: [PATCH 1/2] Use an associated type default for `Key::Cache`. They currently aren't used because r-a didn't support them, but r-a support was recently merged in https://github.com/rust-lang/rust-analyzer/pull/21243. --- compiler/rustc_middle/src/query/keys.rs | 116 +----------------------- 1 file changed, 1 insertion(+), 115 deletions(-) diff --git a/compiler/rustc_middle/src/query/keys.rs b/compiler/rustc_middle/src/query/keys.rs index 4d914c42cfc62..18e04e552046b 100644 --- a/compiler/rustc_middle/src/query/keys.rs +++ b/compiler/rustc_middle/src/query/keys.rs @@ -28,15 +28,7 @@ pub trait Key: Sized { /// constraint is not enforced here. /// /// [`QueryCache`]: rustc_query_system::query::QueryCache - // N.B. Most of the keys down below have `type Cache = DefaultCache;`, - // it would be reasonable to use associated type defaults, to remove the duplication... - // - // ...But r-a doesn't support them yet and using a default here causes r-a to not infer - // return types of queries which is very annoying. Thus, until r-a support associated - // type defaults, please restrain from using them here <3 - // - // r-a issue: - type Cache; + type Cache = DefaultCache; /// In the event that a cycle occurs, if no explicit span has been /// given for a query with key `self`, what span should we use? @@ -71,8 +63,6 @@ impl Key for () { } impl<'tcx> Key for ty::InstanceKind<'tcx> { - type Cache = DefaultCache; - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { tcx.def_span(self.def_id()) } @@ -88,32 +78,24 @@ impl<'tcx> AsLocalKey for ty::InstanceKind<'tcx> { } impl<'tcx> Key for ty::Instance<'tcx> { - type Cache = DefaultCache; - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { tcx.def_span(self.def_id()) } } impl<'tcx> Key for mir::interpret::GlobalId<'tcx> { - type Cache = DefaultCache; - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { self.instance.default_span(tcx) } } impl<'tcx> Key for (Ty<'tcx>, Option>) { - type Cache = DefaultCache; - fn default_span(&self, _: TyCtxt<'_>) -> Span { DUMMY_SP } } impl<'tcx> Key for mir::interpret::LitToConstInput<'tcx> { - type Cache = DefaultCache; - fn default_span(&self, _tcx: TyCtxt<'_>) -> Span { DUMMY_SP } @@ -183,8 +165,6 @@ impl AsLocalKey for DefId { } impl Key for LocalModDefId { - type Cache = DefaultCache; - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { tcx.def_span(*self) } @@ -196,8 +176,6 @@ impl Key for LocalModDefId { } impl Key for ModDefId { - type Cache = DefaultCache; - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { tcx.def_span(*self) } @@ -218,56 +196,42 @@ impl AsLocalKey for ModDefId { } impl Key for SimplifiedType { - type Cache = DefaultCache; - fn default_span(&self, _: TyCtxt<'_>) -> Span { DUMMY_SP } } impl Key for (DefId, DefId) { - type Cache = DefaultCache; - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { self.1.default_span(tcx) } } impl<'tcx> Key for (ty::Instance<'tcx>, LocalDefId) { - type Cache = DefaultCache; - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { self.0.default_span(tcx) } } impl Key for (DefId, LocalDefId) { - type Cache = DefaultCache; - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { self.1.default_span(tcx) } } impl Key for (LocalDefId, DefId) { - type Cache = DefaultCache; - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { self.0.default_span(tcx) } } impl Key for (LocalDefId, LocalDefId) { - type Cache = DefaultCache; - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { self.0.default_span(tcx) } } impl Key for (DefId, Ident) { - type Cache = DefaultCache; - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { tcx.def_span(self.0) } @@ -279,16 +243,12 @@ impl Key for (DefId, Ident) { } impl Key for (LocalDefId, LocalDefId, Ident) { - type Cache = DefaultCache; - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { self.1.default_span(tcx) } } impl Key for (CrateNum, DefId) { - type Cache = DefaultCache; - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { self.1.default_span(tcx) } @@ -304,8 +264,6 @@ impl AsLocalKey for (CrateNum, DefId) { } impl Key for (CrateNum, SimplifiedType) { - type Cache = DefaultCache; - fn default_span(&self, _: TyCtxt<'_>) -> Span { DUMMY_SP } @@ -321,120 +279,90 @@ impl AsLocalKey for (CrateNum, SimplifiedType) { } impl Key for (DefId, SimplifiedType) { - type Cache = DefaultCache; - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { self.0.default_span(tcx) } } impl Key for (DefId, ty::SizedTraitKind) { - type Cache = DefaultCache; - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { self.0.default_span(tcx) } } impl<'tcx> Key for GenericArgsRef<'tcx> { - type Cache = DefaultCache; - fn default_span(&self, _: TyCtxt<'_>) -> Span { DUMMY_SP } } impl<'tcx> Key for (DefId, GenericArgsRef<'tcx>) { - type Cache = DefaultCache; - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { self.0.default_span(tcx) } } impl<'tcx> Key for (ty::UnevaluatedConst<'tcx>, ty::UnevaluatedConst<'tcx>) { - type Cache = DefaultCache; - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { (self.0).def.default_span(tcx) } } impl<'tcx> Key for (LocalDefId, DefId, GenericArgsRef<'tcx>) { - type Cache = DefaultCache; - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { self.0.default_span(tcx) } } impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::TraitRef<'tcx>) { - type Cache = DefaultCache; - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { tcx.def_span(self.1.def_id) } } impl<'tcx> Key for ty::ParamEnvAnd<'tcx, Ty<'tcx>> { - type Cache = DefaultCache; - fn default_span(&self, _tcx: TyCtxt<'_>) -> Span { DUMMY_SP } } impl<'tcx> Key for ty::TraitRef<'tcx> { - type Cache = DefaultCache; - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { tcx.def_span(self.def_id) } } impl<'tcx> Key for ty::PolyTraitRef<'tcx> { - type Cache = DefaultCache; - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { tcx.def_span(self.def_id()) } } impl<'tcx> Key for ty::PolyExistentialTraitRef<'tcx> { - type Cache = DefaultCache; - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { tcx.def_span(self.def_id()) } } impl<'tcx> Key for (ty::PolyTraitRef<'tcx>, ty::PolyTraitRef<'tcx>) { - type Cache = DefaultCache; - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { tcx.def_span(self.0.def_id()) } } impl<'tcx> Key for GenericArg<'tcx> { - type Cache = DefaultCache; - fn default_span(&self, _: TyCtxt<'_>) -> Span { DUMMY_SP } } impl<'tcx> Key for ty::Const<'tcx> { - type Cache = DefaultCache; - fn default_span(&self, _: TyCtxt<'_>) -> Span { DUMMY_SP } } impl<'tcx> Key for Ty<'tcx> { - type Cache = DefaultCache; - fn default_span(&self, _: TyCtxt<'_>) -> Span { DUMMY_SP } @@ -449,40 +377,30 @@ impl<'tcx> Key for Ty<'tcx> { } impl<'tcx> Key for TyAndLayout<'tcx> { - type Cache = DefaultCache; - fn default_span(&self, _: TyCtxt<'_>) -> Span { DUMMY_SP } } impl<'tcx> Key for (Ty<'tcx>, Ty<'tcx>) { - type Cache = DefaultCache; - fn default_span(&self, _: TyCtxt<'_>) -> Span { DUMMY_SP } } impl<'tcx> Key for ty::Clauses<'tcx> { - type Cache = DefaultCache; - fn default_span(&self, _: TyCtxt<'_>) -> Span { DUMMY_SP } } impl<'tcx> Key for ty::ParamEnv<'tcx> { - type Cache = DefaultCache; - fn default_span(&self, _: TyCtxt<'_>) -> Span { DUMMY_SP } } impl<'tcx, T: Key> Key for ty::PseudoCanonicalInput<'tcx, T> { - type Cache = DefaultCache; - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { self.value.default_span(tcx) } @@ -493,24 +411,18 @@ impl<'tcx, T: Key> Key for ty::PseudoCanonicalInput<'tcx, T> { } impl Key for Symbol { - type Cache = DefaultCache; - fn default_span(&self, _tcx: TyCtxt<'_>) -> Span { DUMMY_SP } } impl Key for Option { - type Cache = DefaultCache; - fn default_span(&self, _tcx: TyCtxt<'_>) -> Span { DUMMY_SP } } impl<'tcx> Key for &'tcx OsStr { - type Cache = DefaultCache; - fn default_span(&self, _tcx: TyCtxt<'_>) -> Span { DUMMY_SP } @@ -519,80 +431,60 @@ impl<'tcx> Key for &'tcx OsStr { /// 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> { - type Cache = DefaultCache; - fn default_span(&self, _tcx: TyCtxt<'_>) -> Span { DUMMY_SP } } impl<'tcx, T: Clone> Key for (CanonicalQueryInput<'tcx, T>, bool) { - type Cache = DefaultCache; - fn default_span(&self, _tcx: TyCtxt<'_>) -> Span { DUMMY_SP } } impl Key for (Symbol, u32, u32) { - type Cache = DefaultCache; - fn default_span(&self, _tcx: TyCtxt<'_>) -> Span { DUMMY_SP } } impl<'tcx> Key for (DefId, Ty<'tcx>, GenericArgsRef<'tcx>, ty::ParamEnv<'tcx>) { - type Cache = DefaultCache; - fn default_span(&self, _tcx: TyCtxt<'_>) -> Span { DUMMY_SP } } impl<'tcx> Key for (Ty<'tcx>, rustc_abi::VariantIdx) { - type Cache = DefaultCache; - fn default_span(&self, _tcx: TyCtxt<'_>) -> Span { DUMMY_SP } } impl<'tcx> Key for (ty::Predicate<'tcx>, traits::WellFormedLoc) { - type Cache = DefaultCache; - fn default_span(&self, _tcx: TyCtxt<'_>) -> Span { DUMMY_SP } } impl<'tcx> Key for (ty::PolyFnSig<'tcx>, &'tcx ty::List>) { - type Cache = DefaultCache; - fn default_span(&self, _: TyCtxt<'_>) -> Span { DUMMY_SP } } impl<'tcx> Key for (ty::Instance<'tcx>, &'tcx ty::List>) { - type Cache = DefaultCache; - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { self.0.default_span(tcx) } } impl<'tcx> Key for ty::Value<'tcx> { - type Cache = DefaultCache; - fn default_span(&self, _: TyCtxt<'_>) -> Span { DUMMY_SP } } impl Key for HirId { - type Cache = DefaultCache; - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { tcx.hir_span(*self) } @@ -604,8 +496,6 @@ impl Key for HirId { } impl Key for (LocalDefId, HirId) { - type Cache = DefaultCache; - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { tcx.hir_span(self.1) } @@ -617,8 +507,6 @@ impl Key for (LocalDefId, HirId) { } impl<'tcx> Key for (ValidityRequirement, ty::PseudoCanonicalInput<'tcx, Ty<'tcx>>) { - type Cache = DefaultCache; - // Just forward to `Ty<'tcx>` fn default_span(&self, _: TyCtxt<'_>) -> Span { @@ -634,8 +522,6 @@ impl<'tcx> Key for (ValidityRequirement, ty::PseudoCanonicalInput<'tcx, Ty<'tcx> } impl<'tcx> Key for (ty::Instance<'tcx>, CollectionMode) { - type Cache = DefaultCache; - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { self.0.default_span(tcx) } From 449f292ab4f80f0eb7ec19fef5a427ca146f49ae Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 14 Jan 2026 14:12:59 +1100 Subject: [PATCH 2/2] Remove unused `Key`/`AsLocalKey` impls. --- compiler/rustc_middle/src/query/keys.rs | 159 +----------------------- 1 file changed, 3 insertions(+), 156 deletions(-) diff --git a/compiler/rustc_middle/src/query/keys.rs b/compiler/rustc_middle/src/query/keys.rs index 18e04e552046b..5a491d46ccd2c 100644 --- a/compiler/rustc_middle/src/query/keys.rs +++ b/compiler/rustc_middle/src/query/keys.rs @@ -2,8 +2,8 @@ use std::ffi::OsStr; -use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE, LocalDefId, LocalModDefId, ModDefId}; -use rustc_hir::hir_id::{HirId, OwnerId}; +use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE, LocalDefId, LocalModDefId}; +use rustc_hir::hir_id::OwnerId; use rustc_query_system::dep_graph::DepNodeIndex; use rustc_query_system::query::{DefIdCache, DefaultCache, SingleCache, VecCache}; use rustc_span::{DUMMY_SP, Ident, Span, Symbol}; @@ -11,7 +11,7 @@ use rustc_span::{DUMMY_SP, Ident, Span, Symbol}; use crate::infer::canonical::CanonicalQueryInput; use crate::mir::mono::CollectionMode; use crate::ty::fast_reject::SimplifiedType; -use crate::ty::layout::{TyAndLayout, ValidityRequirement}; +use crate::ty::layout::ValidityRequirement; use crate::ty::{self, GenericArg, GenericArgsRef, Ty, TyCtxt}; use crate::{mir, traits}; @@ -68,15 +68,6 @@ impl<'tcx> Key for ty::InstanceKind<'tcx> { } } -impl<'tcx> AsLocalKey for ty::InstanceKind<'tcx> { - type LocalKey = Self; - - #[inline(always)] - fn as_local_key(&self) -> Option { - self.def_id().is_local().then(|| *self) - } -} - impl<'tcx> Key for ty::Instance<'tcx> { fn default_span(&self, tcx: TyCtxt<'_>) -> Span { tcx.def_span(self.def_id()) @@ -175,26 +166,6 @@ impl Key for LocalModDefId { } } -impl Key for ModDefId { - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { - tcx.def_span(*self) - } - - #[inline(always)] - fn key_as_def_id(&self) -> Option { - Some(self.to_def_id()) - } -} - -impl AsLocalKey for ModDefId { - type LocalKey = LocalModDefId; - - #[inline(always)] - fn as_local_key(&self) -> Option { - self.as_local() - } -} - impl Key for SimplifiedType { fn default_span(&self, _: TyCtxt<'_>) -> Span { DUMMY_SP @@ -207,30 +178,6 @@ impl Key for (DefId, DefId) { } } -impl<'tcx> Key for (ty::Instance<'tcx>, LocalDefId) { - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { - self.0.default_span(tcx) - } -} - -impl Key for (DefId, LocalDefId) { - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { - self.1.default_span(tcx) - } -} - -impl Key for (LocalDefId, DefId) { - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { - self.0.default_span(tcx) - } -} - -impl Key for (LocalDefId, LocalDefId) { - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { - self.0.default_span(tcx) - } -} - impl Key for (DefId, Ident) { fn default_span(&self, tcx: TyCtxt<'_>) -> Span { tcx.def_span(self.0) @@ -278,12 +225,6 @@ impl AsLocalKey for (CrateNum, SimplifiedType) { } } -impl Key for (DefId, SimplifiedType) { - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { - self.0.default_span(tcx) - } -} - impl Key for (DefId, ty::SizedTraitKind) { fn default_span(&self, tcx: TyCtxt<'_>) -> Span { self.0.default_span(tcx) @@ -302,66 +243,18 @@ impl<'tcx> Key for (DefId, GenericArgsRef<'tcx>) { } } -impl<'tcx> Key for (ty::UnevaluatedConst<'tcx>, ty::UnevaluatedConst<'tcx>) { - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { - (self.0).def.default_span(tcx) - } -} - -impl<'tcx> Key for (LocalDefId, DefId, GenericArgsRef<'tcx>) { - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { - self.0.default_span(tcx) - } -} - -impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::TraitRef<'tcx>) { - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { - tcx.def_span(self.1.def_id) - } -} - -impl<'tcx> Key for ty::ParamEnvAnd<'tcx, Ty<'tcx>> { - fn default_span(&self, _tcx: TyCtxt<'_>) -> Span { - DUMMY_SP - } -} - impl<'tcx> Key for ty::TraitRef<'tcx> { fn default_span(&self, tcx: TyCtxt<'_>) -> Span { tcx.def_span(self.def_id) } } -impl<'tcx> Key for ty::PolyTraitRef<'tcx> { - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { - tcx.def_span(self.def_id()) - } -} - -impl<'tcx> Key for ty::PolyExistentialTraitRef<'tcx> { - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { - tcx.def_span(self.def_id()) - } -} - -impl<'tcx> Key for (ty::PolyTraitRef<'tcx>, ty::PolyTraitRef<'tcx>) { - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { - tcx.def_span(self.0.def_id()) - } -} - impl<'tcx> Key for GenericArg<'tcx> { fn default_span(&self, _: TyCtxt<'_>) -> Span { DUMMY_SP } } -impl<'tcx> Key for ty::Const<'tcx> { - fn default_span(&self, _: TyCtxt<'_>) -> Span { - DUMMY_SP - } -} - impl<'tcx> Key for Ty<'tcx> { fn default_span(&self, _: TyCtxt<'_>) -> Span { DUMMY_SP @@ -376,12 +269,6 @@ impl<'tcx> Key for Ty<'tcx> { } } -impl<'tcx> Key for TyAndLayout<'tcx> { - fn default_span(&self, _: TyCtxt<'_>) -> Span { - DUMMY_SP - } -} - impl<'tcx> Key for (Ty<'tcx>, Ty<'tcx>) { fn default_span(&self, _: TyCtxt<'_>) -> Span { DUMMY_SP @@ -394,12 +281,6 @@ impl<'tcx> Key for ty::Clauses<'tcx> { } } -impl<'tcx> Key for ty::ParamEnv<'tcx> { - fn default_span(&self, _: TyCtxt<'_>) -> Span { - DUMMY_SP - } -} - impl<'tcx, T: Key> Key for ty::PseudoCanonicalInput<'tcx, T> { fn default_span(&self, tcx: TyCtxt<'_>) -> Span { self.value.default_span(tcx) @@ -442,18 +323,6 @@ impl<'tcx, T: Clone> Key for (CanonicalQueryInput<'tcx, T>, bool) { } } -impl Key for (Symbol, u32, u32) { - fn default_span(&self, _tcx: TyCtxt<'_>) -> Span { - DUMMY_SP - } -} - -impl<'tcx> Key for (DefId, Ty<'tcx>, GenericArgsRef<'tcx>, ty::ParamEnv<'tcx>) { - fn default_span(&self, _tcx: TyCtxt<'_>) -> Span { - DUMMY_SP - } -} - impl<'tcx> Key for (Ty<'tcx>, rustc_abi::VariantIdx) { fn default_span(&self, _tcx: TyCtxt<'_>) -> Span { DUMMY_SP @@ -484,28 +353,6 @@ impl<'tcx> Key for ty::Value<'tcx> { } } -impl Key for HirId { - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { - tcx.hir_span(*self) - } - - #[inline(always)] - fn key_as_def_id(&self) -> Option { - None - } -} - -impl Key for (LocalDefId, HirId) { - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { - tcx.hir_span(self.1) - } - - #[inline(always)] - fn key_as_def_id(&self) -> Option { - Some(self.0.into()) - } -} - impl<'tcx> Key for (ValidityRequirement, ty::PseudoCanonicalInput<'tcx, Ty<'tcx>>) { // Just forward to `Ty<'tcx>`