From d9137667574db8fd54e0766f492ff5eff236ab44 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Mon, 16 Mar 2026 19:17:58 +1100 Subject: [PATCH] Remove the `_description_fns` module. `rustc_queries` generates a macro and two modules. One of the modules looks like this: ``` mod _description_fns { ... #[allow(unused_variables)] pub fn hir_module_items<'tcx>(tcx: TyCtxt<'tcx>, key: LocalModDefId) -> String { format!("getting HIR module items in `{}`", tcx.def_path_str(key)) } ... } ``` Members of this module are then used in `TaggedQueryKey::description`. This commit removes the `_description_fns` module entirely. For each query we now instead generate a description closure that is used instead. This closure is passed in the modifiers list. This change simplifies `rustc_queries` quite a bit. It requires adding another query modifier, but query modifiers are how other query-specific details are already passed to the declarative macros, so it's more consistent. --- compiler/rustc_macros/src/query.rs | 63 +++++-------------- compiler/rustc_middle/src/query/plumbing.rs | 4 +- .../rustc_query_impl/src/dep_kind_vtables.rs | 1 + compiler/rustc_query_impl/src/query_impl.rs | 1 + 4 files changed, 18 insertions(+), 51 deletions(-) diff --git a/compiler/rustc_macros/src/query.rs b/compiler/rustc_macros/src/query.rs index a44efc97dc8f..6960920367ff 100644 --- a/compiler/rustc_macros/src/query.rs +++ b/compiler/rustc_macros/src/query.rs @@ -243,7 +243,7 @@ fn make_modifiers_stream(query: &Query) -> proc_macro2::TokenStream { arena_cache, cache_on_disk, depth_limit, - desc: _, + desc, eval_always, feedable, no_force, @@ -256,7 +256,18 @@ fn make_modifiers_stream(query: &Query) -> proc_macro2::TokenStream { let arena_cache = arena_cache.is_some(); let cache_on_disk = cache_on_disk.is_some(); let depth_limit = depth_limit.is_some(); - // `desc` is not handled here + let desc = { + // Put a description closure in the `desc` modifier. + let key_pat = &query.key_pat; + let key_ty = &query.key_ty; + let desc_expr_list = &desc.expr_list; + quote! { + { + #[allow(unused_variables)] + |tcx: TyCtxt<'tcx>, #key_pat: #key_ty| format!(#desc_expr_list) + } + } + }; let eval_always = eval_always.is_some(); let feedable = feedable.is_some(); let no_force = no_force.is_some(); @@ -276,7 +287,7 @@ fn make_modifiers_stream(query: &Query) -> proc_macro2::TokenStream { arena_cache: #arena_cache, cache_on_disk: #cache_on_disk, depth_limit: #depth_limit, - // `desc` is not handled here + desc: #desc, eval_always: #eval_always, feedable: #feedable, no_force: #no_force, @@ -314,37 +325,6 @@ fn doc_comment_from_desc(list: &Punctuated) -> Result(tcx: TyCtxt<'tcx>, #key_pat: #key_ty) -> String { - format!(#expr_list) - } - }; - - streams.description_fns_stream.extend(quote! { - #desc - }); -} - /// Add hints for rust-analyzer fn add_to_analyzer_stream(query: &Query, analyzer_stream: &mut proc_macro2::TokenStream) { // Add links to relevant modifiers @@ -419,7 +399,6 @@ pub(super) fn rustc_queries(input: TokenStream) -> TokenStream { let mut query_stream = quote! {}; let mut non_query_stream = quote! {}; - let mut helpers = HelperTokenStreams::default(); let mut analyzer_stream = quote! {}; let mut errors = quote! {}; @@ -472,11 +451,8 @@ fn #name(#key_ty) #return_ty } add_to_analyzer_stream(&query, &mut analyzer_stream); - make_helpers_for_query(&query, &mut helpers); } - let HelperTokenStreams { description_fns_stream } = helpers; - TokenStream::from(quote! { /// Higher-order macro that invokes the specified macro with (a) a list of all query /// signatures (including modifiers), and (b) a list of non-query names. This allows @@ -500,17 +476,6 @@ mod _analyzer_hints { #analyzer_stream } - /// Functions that format a human-readable description of each query - /// and its key, as specified by the `desc` query modifier. - /// - /// (The leading `_` avoids collisions with actual query names when - /// expanded in `rustc_middle::queries`, and makes this macro-generated - /// module easier to search for.) - pub mod _description_fns { - use super::*; - #description_fns_stream - } - #errors }) } diff --git a/compiler/rustc_middle/src/query/plumbing.rs b/compiler/rustc_middle/src/query/plumbing.rs index 2e1e614b8fb4..11f66f10cd54 100644 --- a/compiler/rustc_middle/src/query/plumbing.rs +++ b/compiler/rustc_middle/src/query/plumbing.rs @@ -301,6 +301,7 @@ fn $name:ident($($K:tt)*) -> $V:ty arena_cache: $arena_cache:literal, cache_on_disk: $cache_on_disk:literal, depth_limit: $depth_limit:literal, + desc: $desc:expr, eval_always: $eval_always:literal, feedable: $feedable:literal, no_force: $no_force:literal, @@ -435,8 +436,7 @@ pub fn query_name(&self) -> &'static str { pub fn description(&self, tcx: TyCtxt<'tcx>) -> String { let (name, description) = ty::print::with_no_queries!(match self { $( - TaggedQueryKey::$name(key) => - (stringify!($name), _description_fns::$name(tcx, *key)), + TaggedQueryKey::$name(key) => (stringify!($name), ($desc)(tcx, *key)), )* }); if tcx.sess.verbose_internals() { diff --git a/compiler/rustc_query_impl/src/dep_kind_vtables.rs b/compiler/rustc_query_impl/src/dep_kind_vtables.rs index b70fe3008cb1..e0d357586367 100644 --- a/compiler/rustc_query_impl/src/dep_kind_vtables.rs +++ b/compiler/rustc_query_impl/src/dep_kind_vtables.rs @@ -135,6 +135,7 @@ fn $name:ident($K:ty) -> $V:ty arena_cache: $arena_cache:literal, cache_on_disk: $cache_on_disk:literal, depth_limit: $depth_limit:literal, + desc: $desc:expr, eval_always: $eval_always:literal, feedable: $feedable:literal, no_force: $no_force:literal, diff --git a/compiler/rustc_query_impl/src/query_impl.rs b/compiler/rustc_query_impl/src/query_impl.rs index 2d3dae04181e..c1e7f3ef3cdb 100644 --- a/compiler/rustc_query_impl/src/query_impl.rs +++ b/compiler/rustc_query_impl/src/query_impl.rs @@ -19,6 +19,7 @@ fn $name:ident($K:ty) -> $V:ty arena_cache: $arena_cache:literal, cache_on_disk: $cache_on_disk:literal, depth_limit: $depth_limit:literal, + desc: $desc:expr, eval_always: $eval_always:literal, feedable: $feedable:literal, no_force: $no_force:literal,