Skip to content

Commit 253c696

Browse files
committed
Make non-queries available to rustc_with_all_queries.
`rustc_with_all_queries` currently provides information about all queries. Also, a caller can provide an ad hoc list of extra non-queries. This is used by `define_queries` for non-query dep kinds: `Null`, `Red`, etc. This is pretty hacky. This commit changes `rustc_with_all_queries` so that the non-queries information is available to all callers. (Some callers ignore the non-query information.) This is done by adding `non_query` entries to the primary list of queries in `rustc_queries!`.
1 parent 28b5c1c commit 253c696

5 files changed

Lines changed: 118 additions & 56 deletions

File tree

compiler/rustc_macros/src/query.rs

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use syn::{
1010
};
1111

1212
mod kw {
13+
syn::custom_keyword!(non_query);
1314
syn::custom_keyword!(query);
1415
}
1516

@@ -54,12 +55,37 @@ struct Query {
5455
modifiers: QueryModifiers,
5556
}
5657

57-
impl Parse for Query {
58+
/// Declaration of a non-query dep kind.
59+
/// ```ignore (illustrative)
60+
/// /// Doc comment for `MyNonQuery`.
61+
/// // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doc_comments
62+
/// non_query MyNonQuery
63+
/// // ^^^^^^^^^^ name
64+
/// ```
65+
struct NonQuery {
66+
doc_comments: Vec<Attribute>,
67+
name: Ident,
68+
}
69+
70+
enum QueryEntry {
71+
Query(Query),
72+
NonQuery(NonQuery),
73+
}
74+
75+
impl Parse for QueryEntry {
5876
fn parse(input: ParseStream<'_>) -> Result<Self> {
5977
let mut doc_comments = check_attributes(input.call(Attribute::parse_outer)?)?;
6078

79+
// Try the non-query case first.
80+
if input.parse::<kw::non_query>().is_ok() {
81+
let name: Ident = input.parse()?;
82+
return Ok(QueryEntry::NonQuery(NonQuery { doc_comments, name }));
83+
}
84+
6185
// Parse the query declaration. Like `query type_of(key: DefId) -> Ty<'tcx>`
62-
input.parse::<kw::query>()?;
86+
if input.parse::<kw::query>().is_err() {
87+
return Err(input.error("expected `query` or `non_query`"));
88+
}
6389
let name: Ident = input.parse()?;
6490

6591
// `(key: DefId)`
@@ -84,7 +110,7 @@ impl Parse for Query {
84110
doc_comments.push(doc_comment_from_desc(&modifiers.desc.expr_list)?);
85111
}
86112

87-
Ok(Query { doc_comments, modifiers, name, key_pat, key_ty, return_ty })
113+
Ok(QueryEntry::Query(Query { doc_comments, modifiers, name, key_pat, key_ty, return_ty }))
88114
}
89115
}
90116

@@ -375,9 +401,9 @@ fn add_to_analyzer_stream(query: &Query, analyzer_stream: &mut proc_macro2::Toke
375401
// macro producing a higher order macro that has all its token in the macro declaration we lose
376402
// any meaningful spans, resulting in rust-analyzer being unable to make the connection between
377403
// the query name and the corresponding providers field. The trick to fix this is to have
378-
// `rustc_queries` emit a field access with the given name's span which allows it to successfully
379-
// show references / go to definition to the corresponding provider assignment which is usually
380-
// the more interesting place.
404+
// `rustc_queries` emit a field access with the given name's span which allows it to
405+
// successfully show references / go to definition to the corresponding provider assignment
406+
// which is usually the more interesting place.
381407
let ra_hint = quote! {
382408
let crate::query::Providers { #name: _, .. };
383409
};
@@ -393,9 +419,10 @@ fn add_to_analyzer_stream(query: &Query, analyzer_stream: &mut proc_macro2::Toke
393419
}
394420

395421
pub(super) fn rustc_queries(input: TokenStream) -> TokenStream {
396-
let queries = parse_macro_input!(input as List<Query>);
422+
let queries = parse_macro_input!(input as List<QueryEntry>);
397423

398424
let mut query_stream = quote! {};
425+
let mut non_query_stream = quote! {};
399426
let mut helpers = HelperTokenStreams::default();
400427
let mut analyzer_stream = quote! {};
401428
let mut errors = quote! {};
@@ -411,6 +438,18 @@ pub(super) fn rustc_queries(input: TokenStream) -> TokenStream {
411438
}
412439

413440
for query in queries.0 {
441+
let query = match query {
442+
QueryEntry::Query(query) => query,
443+
QueryEntry::NonQuery(NonQuery { doc_comments, name }) => {
444+
// Get the exceptional non-query case out of the way first.
445+
non_query_stream.extend(quote! {
446+
#(#doc_comments)*
447+
#name,
448+
});
449+
continue;
450+
}
451+
};
452+
414453
let Query { doc_comments, name, key_ty, return_ty, modifiers, .. } = &query;
415454

416455
// Normalize an absent return type into `-> ()` to make macro-rules parsing easier.
@@ -486,24 +525,18 @@ pub(super) fn rustc_queries(input: TokenStream) -> TokenStream {
486525
let HelperTokenStreams { description_fns_stream, cache_on_disk_if_fns_stream } = helpers;
487526

488527
TokenStream::from(quote! {
489-
/// Higher-order macro that invokes the specified macro with a prepared
490-
/// list of all query signatures (including modifiers).
491-
///
492-
/// This allows multiple simpler macros to each have access to the list
493-
/// of queries.
528+
/// Higher-order macro that invokes the specified macro with (a) a list of all query
529+
/// signatures (including modifiers), and (b) a list of non-query names. This allows
530+
/// multiple simpler macros to each have access to these lists.
494531
#[macro_export]
495532
macro_rules! rustc_with_all_queries {
496533
(
497-
// The macro to invoke once, on all queries (plus extras).
534+
// The macro to invoke once, on all queries and non-queries.
498535
$macro:ident!
499-
500-
// Within [], an optional list of extra "query" signatures to
501-
// pass to the given macro, in addition to the actual queries.
502-
$( [$($extra_fake_queries:tt)*] )?
503536
) => {
504537
$macro! {
505-
$( $($extra_fake_queries)* )?
506-
#query_stream
538+
queries { #query_stream }
539+
non_queries { #non_query_stream }
507540
}
508541
}
509542
}

compiler/rustc_middle/src/dep_graph/dep_node.rs

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -270,15 +270,26 @@ impl StableOrd for WorkProductId {
270270
// Note: `$K` and `$V` are unused but present so this can be called by `rustc_with_all_queries`.
271271
macro_rules! define_dep_nodes {
272272
(
273-
$(
274-
$(#[$attr:meta])*
275-
[$($modifiers:tt)*] fn $variant:ident($K:ty) -> $V:ty,
276-
)*
273+
queries {
274+
$(
275+
$(#[$q_attr:meta])*
276+
[$($modifiers:tt)*]
277+
fn $q_name:ident($K:ty) -> $V:ty,
278+
)*
279+
}
280+
non_queries {
281+
$(
282+
$(#[$nq_attr:meta])*
283+
$nq_name:ident,
284+
)*
285+
}
277286
) => {
278-
279287
#[macro_export]
280288
macro_rules! make_dep_kind_array {
281-
($mod:ident) => {[ $($mod::$variant()),* ]};
289+
($mod:ident) => {[
290+
$( $mod::$nq_name(), )*
291+
$( $mod::$q_name(), )*
292+
]};
282293
}
283294

284295
/// This enum serves as an index into arrays built by `make_dep_kind_array`.
@@ -290,14 +301,18 @@ macro_rules! define_dep_nodes {
290301
#[allow(non_camel_case_types)]
291302
#[repr(u16)] // Must be kept in sync with the rest of `DepKind`.
292303
pub enum DepKind {
293-
$( $( #[$attr] )* $variant),*
304+
$( $(#[$nq_attr])* $nq_name, )*
305+
$( $(#[$q_attr])* $q_name, )*
294306
}
295307

296308
// This computes the number of dep kind variants. Along the way, it sanity-checks that the
297309
// discriminants of the variants have been assigned consecutively from 0 so that they can
298310
// be used as a dense index, and that all discriminants fit in a `u16`.
299311
pub(crate) const DEP_KIND_NUM_VARIANTS: u16 = {
300-
let deps = &[$(DepKind::$variant,)*];
312+
let deps = &[
313+
$(DepKind::$nq_name,)*
314+
$(DepKind::$q_name,)*
315+
];
301316
let mut i = 0;
302317
while i < deps.len() {
303318
if i != deps[i].as_usize() {
@@ -311,7 +326,8 @@ macro_rules! define_dep_nodes {
311326

312327
pub(super) fn dep_kind_from_label_string(label: &str) -> Result<DepKind, ()> {
313328
match label {
314-
$( stringify!($variant) => Ok(self::DepKind::$variant), )*
329+
$( stringify!($nq_name) => Ok(self::DepKind::$nq_name), )*
330+
$( stringify!($q_name) => Ok(self::DepKind::$q_name), )*
315331
_ => Err(()),
316332
}
317333
}
@@ -320,27 +336,14 @@ macro_rules! define_dep_nodes {
320336
/// DepNode groups for tests.
321337
#[expect(non_upper_case_globals)]
322338
pub mod label_strs {
323-
$( pub const $variant: &str = stringify!($variant); )*
339+
$( pub const $nq_name: &str = stringify!($nq_name); )*
340+
$( pub const $q_name: &str = stringify!($q_name); )*
324341
}
325342
};
326343
}
327344

328-
// Create various data structures for each query, and also for a few things
329-
// that aren't queries. The key and return types aren't used, hence the use of `()`.
330-
rustc_with_all_queries!(define_dep_nodes![
331-
/// We use this for most things when incr. comp. is turned off.
332-
[] fn Null(()) -> (),
333-
/// We use this to create a forever-red node.
334-
[] fn Red(()) -> (),
335-
/// We use this to create a side effect node.
336-
[] fn SideEffect(()) -> (),
337-
/// We use this to create the anon node with zero dependencies.
338-
[] fn AnonZeroDeps(()) -> (),
339-
[] fn TraitSelect(()) -> (),
340-
[] fn CompileCodegenUnit(()) -> (),
341-
[] fn CompileMonoItem(()) -> (),
342-
[] fn Metadata(()) -> (),
343-
]);
345+
// Create various data structures for each query, and also for a few things that aren't queries.
346+
rustc_with_all_queries! { define_dep_nodes! }
344347

345348
// WARNING: `construct` is generic and does not know that `CompileCodegenUnit` takes `Symbol`s as keys.
346349
// Be very careful changing this type signature!

compiler/rustc_middle/src/queries.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2771,6 +2771,23 @@ rustc_queries! {
27712771
cache_on_disk_if { *cnum == LOCAL_CRATE }
27722772
separate_provide_extern
27732773
}
2774+
2775+
//-----------------------------------------------------------------------------
2776+
// "Non-queries" are special dep kinds that are not queries.
2777+
//-----------------------------------------------------------------------------
2778+
2779+
/// We use this for most things when incr. comp. is turned off.
2780+
non_query Null
2781+
/// We use this to create a forever-red node.
2782+
non_query Red
2783+
/// We use this to create a side effect node.
2784+
non_query SideEffect
2785+
/// We use this to create the anon node with zero dependencies.
2786+
non_query AnonZeroDeps
2787+
non_query TraitSelect
2788+
non_query CompileCodegenUnit
2789+
non_query CompileMonoItem
2790+
non_query Metadata
27742791
}
27752792

27762793
rustc_with_all_queries! { define_callbacks! }

compiler/rustc_middle/src/query/plumbing.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -378,11 +378,15 @@ macro_rules! define_callbacks {
378378
(
379379
// You might expect the key to be `$K:ty`, but it needs to be `$($K:tt)*` so that
380380
// `query_helper_param_ty!` can match on specific type names.
381-
$(
382-
$(#[$attr:meta])*
383-
[$($modifiers:tt)*]
384-
fn $name:ident($($K:tt)*) -> $V:ty,
385-
)*
381+
queries {
382+
$(
383+
$(#[$attr:meta])*
384+
[$($modifiers:tt)*]
385+
fn $name:ident($($K:tt)*) -> $V:ty,
386+
)*
387+
}
388+
// Non-queries are unused here.
389+
non_queries { $($_:tt)* }
386390
) => {
387391
$(
388392
#[allow(unused_lifetimes)]

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -414,15 +414,20 @@ pub(crate) fn force_from_dep_node_inner<'tcx, Q: GetQueryVTable<'tcx>>(
414414
}
415415
}
416416

417-
// Note: `$K` and `$V` are unused but present so this can be called by `rustc_with_all_queries`.
418417
macro_rules! define_queries {
419418
(
420-
$(
421-
$(#[$attr:meta])*
422-
[$($modifiers:tt)*] fn $name:ident($K:ty) -> $V:ty,
423-
)*
419+
// Note: `$K` and `$V` are unused but present so this can be called by
420+
// `rustc_with_all_queries`.
421+
queries {
422+
$(
423+
$(#[$attr:meta])*
424+
[$($modifiers:tt)*]
425+
fn $name:ident($K:ty) -> $V:ty,
426+
)*
427+
}
428+
// Non-queries are unused here.
429+
non_queries { $($_:tt)* }
424430
) => {
425-
426431
pub(crate) mod query_impl { $(pub(crate) mod $name {
427432
use super::super::*;
428433
use ::rustc_middle::query::erase::{self, Erased};

0 commit comments

Comments
 (0)