Skip to content

Commit b8c35ca

Browse files
committed
Auto merge of #102895 - Nilstrieb:query-cleanups, r=cjgillot
Get rid of `rustc_query_description!` **I am not entirely sure whether this is an improvement and would like to get your feedback on it.** Helps with #96524. Queries can provide an arbitrary expression for their description and their caching behavior. Before, these expressions where stored in a `rustc_query_description` macro emitted by the `rustc_queries` macro, and then used in `rustc_query_impl` to fill out the methods for the `QueryDescription` trait. Instead, we now emit two new modules from `rustc_queries` containing the functions with the expressions. `rustc_query_impl` calls these functions now instead of invoking the macro. Since we are now defining some of the functions in `rustc_middle::query`, we now need all the imports for the key types mthere as well. r? `@cjgillot`
2 parents c93ef33 + 24ce4cf commit b8c35ca

File tree

6 files changed

+51
-43
lines changed

6 files changed

+51
-43
lines changed

compiler/rustc_macros/src/query.rs

+28-24
Original file line numberDiff line numberDiff line change
@@ -237,27 +237,32 @@ fn doc_comment_from_desc(list: &Punctuated<Expr, token::Comma>) -> Result<Attrib
237237
}
238238

239239
/// Add the impl of QueryDescription for the query to `impls` if one is requested
240-
fn add_query_description_impl(query: &Query, impls: &mut proc_macro2::TokenStream) {
241-
let name = &query.name;
242-
let key = &query.key;
243-
let modifiers = &query.modifiers;
240+
fn add_query_desc_cached_impl(
241+
query: &Query,
242+
descs: &mut proc_macro2::TokenStream,
243+
cached: &mut proc_macro2::TokenStream,
244+
) {
245+
let Query { name, key, modifiers, .. } = &query;
244246

245247
// Find out if we should cache the query on disk
246248
let cache = if let Some((args, expr)) = modifiers.cache.as_ref() {
247249
let tcx = args.as_ref().map(|t| quote! { #t }).unwrap_or_else(|| quote! { _ });
248250
// expr is a `Block`, meaning that `{ #expr }` gets expanded
249251
// to `{ { stmts... } }`, which triggers the `unused_braces` lint.
252+
// we're taking `key` by reference, but some rustc types usually prefer being passed by value
250253
quote! {
251-
#[allow(unused_variables, unused_braces)]
254+
#[allow(unused_variables, unused_braces, rustc::pass_by_value)]
252255
#[inline]
253-
fn cache_on_disk(#tcx: TyCtxt<'tcx>, #key: &Self::Key) -> bool {
256+
pub fn #name<'tcx>(#tcx: TyCtxt<'tcx>, #key: &crate::ty::query::query_keys::#name<'tcx>) -> bool {
254257
#expr
255258
}
256259
}
257260
} else {
258261
quote! {
262+
// we're taking `key` by reference, but some rustc types usually prefer being passed by value
263+
#[allow(rustc::pass_by_value)]
259264
#[inline]
260-
fn cache_on_disk(_: TyCtxt<'tcx>, _: &Self::Key) -> bool {
265+
pub fn #name<'tcx>(_: TyCtxt<'tcx>, _: &crate::ty::query::query_keys::#name<'tcx>) -> bool {
261266
false
262267
}
263268
}
@@ -268,19 +273,20 @@ fn add_query_description_impl(query: &Query, impls: &mut proc_macro2::TokenStrea
268273

269274
let desc = quote! {
270275
#[allow(unused_variables)]
271-
fn describe(tcx: QueryCtxt<'tcx>, key: Self::Key) -> String {
272-
let (#tcx, #key) = (*tcx, key);
276+
pub fn #name<'tcx>(tcx: TyCtxt<'tcx>, key: crate::ty::query::query_keys::#name<'tcx>) -> String {
277+
let (#tcx, #key) = (tcx, key);
273278
::rustc_middle::ty::print::with_no_trimmed_paths!(
274279
format!(#desc)
275280
)
276281
}
277282
};
278283

279-
impls.extend(quote! {
280-
(#name) => {
281-
#desc
282-
#cache
283-
};
284+
descs.extend(quote! {
285+
#desc
286+
});
287+
288+
cached.extend(quote! {
289+
#cache
284290
});
285291
}
286292

@@ -289,7 +295,7 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream {
289295

290296
let mut query_stream = quote! {};
291297
let mut query_description_stream = quote! {};
292-
let mut cached_queries = quote! {};
298+
let mut query_cached_stream = quote! {};
293299

294300
for query in queries.0 {
295301
let Query { name, arg, modifiers, .. } = &query;
@@ -299,12 +305,6 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream {
299305
_ => quote! { #result_full },
300306
};
301307

302-
if modifiers.cache.is_some() {
303-
cached_queries.extend(quote! {
304-
#name,
305-
});
306-
}
307-
308308
let mut attributes = Vec::new();
309309

310310
macro_rules! passthrough {
@@ -350,7 +350,7 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream {
350350
[#attribute_stream] fn #name(#arg) #result,
351351
});
352352

353-
add_query_description_impl(&query, &mut query_description_stream);
353+
add_query_desc_cached_impl(&query, &mut query_description_stream, &mut query_cached_stream);
354354
}
355355

356356
TokenStream::from(quote! {
@@ -364,9 +364,13 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream {
364364
}
365365
}
366366

367-
#[macro_export]
368-
macro_rules! rustc_query_description {
367+
pub mod descs {
368+
use super::*;
369369
#query_description_stream
370370
}
371+
pub mod cached {
372+
use super::*;
373+
#query_cached_stream
374+
}
371375
})
372376
}

compiler/rustc_middle/src/query/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
//! ["Queries: demand-driven compilation"](https://rustc-dev-guide.rust-lang.org/query.html).
55
//! This chapter includes instructions for adding new queries.
66
7+
use crate::ty::{self, print::describe_as_module, TyCtxt};
8+
use rustc_span::def_id::LOCAL_CRATE;
9+
710
// Each of these queries corresponds to a function pointer field in the
811
// `Providers` struct for requesting a value of that type, and a method
912
// on `tcx: TyCtxt` (and `tcx.at(span)`) for doing that request in a way
@@ -1214,7 +1217,7 @@ rustc_queries! {
12141217
desc { |tcx| "finding all vtable entries for trait {}", tcx.def_path_str(key.def_id()) }
12151218
}
12161219

1217-
query vtable_trait_upcasting_coercion_new_vptr_slot(key: (ty::Ty<'tcx>, ty::Ty<'tcx>)) -> Option<usize> {
1220+
query vtable_trait_upcasting_coercion_new_vptr_slot(key: (Ty<'tcx>, Ty<'tcx>)) -> Option<usize> {
12181221
desc { |tcx| "finding the slot within vtable for trait object {} vtable ptr during trait upcasting coercion from {} vtable",
12191222
key.1, key.0 }
12201223
}

compiler/rustc_middle/src/ty/print/mod.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::ty::{self, DefIdTree, Ty, TyCtxt};
33

44
use rustc_data_structures::fx::FxHashSet;
55
use rustc_data_structures::sso::SsoHashSet;
6-
use rustc_hir::def_id::{CrateNum, DefId};
6+
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId};
77
use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData};
88

99
// `pretty` is a separate module only for organization.
@@ -325,3 +325,12 @@ impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for ty::Const<'tcx> {
325325
cx.print_const(*self)
326326
}
327327
}
328+
329+
// This is only used by query descriptions
330+
pub fn describe_as_module(def_id: LocalDefId, tcx: TyCtxt<'_>) -> String {
331+
if def_id.is_top_level_module() {
332+
"top-level module".to_string()
333+
} else {
334+
format!("module `{}`", tcx.def_path_str(def_id.to_def_id()))
335+
}
336+
}

compiler/rustc_query_impl/src/lib.rs

+1-10
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ use rustc_middle::arena::Arena;
2222
use rustc_middle::dep_graph::{self, DepKindStruct};
2323
use rustc_middle::ty::query::{query_keys, query_storage, query_stored, query_values};
2424
use rustc_middle::ty::query::{ExternProviders, Providers, QueryEngine};
25-
use rustc_middle::ty::{self, TyCtxt};
26-
use rustc_span::def_id::{LocalDefId, LOCAL_CRATE};
25+
use rustc_middle::ty::TyCtxt;
2726
use rustc_span::Span;
2827

2928
#[macro_use]
@@ -45,14 +44,6 @@ pub use on_disk_cache::OnDiskCache;
4544
mod profiling_support;
4645
pub use self::profiling_support::alloc_self_profile_query_strings;
4746

48-
fn describe_as_module(def_id: LocalDefId, tcx: TyCtxt<'_>) -> String {
49-
if def_id.is_top_level_module() {
50-
"top-level module".to_string()
51-
} else {
52-
format!("module `{}`", tcx.def_path_str(def_id.to_def_id()))
53-
}
54-
}
55-
5647
rustc_query_append! { define_queries! }
5748

5849
impl<'tcx> Queries<'tcx> {

compiler/rustc_query_impl/src/plumbing.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ pub(crate) fn create_query_frame<
298298
K: Copy + Key + for<'a> HashStable<StableHashingContext<'a>>,
299299
>(
300300
tcx: QueryCtxt<'tcx>,
301-
do_describe: fn(QueryCtxt<'tcx>, K) -> String,
301+
do_describe: fn(TyCtxt<'tcx>, K) -> String,
302302
key: K,
303303
kind: DepKind,
304304
name: &'static str,
@@ -307,7 +307,7 @@ pub(crate) fn create_query_frame<
307307
// Showing visible path instead of any path is not that important in production.
308308
let description = ty::print::with_no_visible_paths!(
309309
// Force filename-line mode to avoid invoking `type_of` query.
310-
ty::print::with_forced_impl_filename_line!(do_describe(tcx, key))
310+
ty::print::with_forced_impl_filename_line!(do_describe(tcx.tcx, key))
311311
);
312312
let description =
313313
if tcx.sess.verbose() { format!("{} [{}]", description, name) } else { description };
@@ -466,7 +466,10 @@ macro_rules! define_queries {
466466
}
467467

468468
impl<'tcx> QueryDescription<QueryCtxt<'tcx>> for queries::$name<'tcx> {
469-
rustc_query_description! { $name }
469+
#[inline]
470+
fn cache_on_disk(tcx: TyCtxt<'tcx>, key: &Self::Key) -> bool {
471+
::rustc_middle::query::cached::$name(tcx, key)
472+
}
470473

471474
type Cache = query_storage::$name<'tcx>;
472475

@@ -576,7 +579,7 @@ macro_rules! define_queries {
576579
use rustc_middle::ty::TyCtxt;
577580
use $crate::plumbing::{QueryStruct, QueryCtxt};
578581
use $crate::profiling_support::QueryKeyStringCache;
579-
use rustc_query_system::query::{QueryDescription, QueryMap};
582+
use rustc_query_system::query::QueryMap;
580583

581584
pub(super) const fn dummy_query_struct<'tcx>() -> QueryStruct<'tcx> {
582585
fn noop_try_collect_active_jobs(_: QueryCtxt<'_>, _: &mut QueryMap) -> Option<()> {
@@ -603,7 +606,7 @@ macro_rules! define_queries {
603606
let make_query = |tcx, key| {
604607
let kind = rustc_middle::dep_graph::DepKind::$name;
605608
let name = stringify!($name);
606-
$crate::plumbing::create_query_frame(tcx, super::queries::$name::describe, key, kind, name)
609+
$crate::plumbing::create_query_frame(tcx, rustc_middle::query::descs::$name, key, kind, name)
607610
};
608611
tcx.queries.$name.try_collect_active_jobs(
609612
tcx,

compiler/rustc_query_system/src/query/config.rs

-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ impl<CTX: QueryContext, K, V> QueryVTable<CTX, K, V> {
4949
pub trait QueryDescription<CTX: QueryContext>: QueryConfig {
5050
type Cache: QueryCache<Key = Self::Key, Stored = Self::Stored, Value = Self::Value>;
5151

52-
fn describe(tcx: CTX, key: Self::Key) -> String;
53-
5452
// Don't use this method to access query results, instead use the methods on TyCtxt
5553
fn query_state<'a>(tcx: CTX) -> &'a QueryState<Self::Key>
5654
where

0 commit comments

Comments
 (0)