Skip to content

Commit a6ed44d

Browse files
committed
Reduce $K and $V usage in query macros.
Within `define_callbacks!`: - Use `Key<'tcx>` where possible instead of `$($K)*`. - Use `Value<'tcx>` where possible instead of `$V`. In the other `define_*!` macros: - Use `$K:ty` where possible instead of `$($K:tt)*`. - Add comments about unused `$K` and `$V` cases. - Add `()` key types to the special nodes in the `define_dep_nodes!` invocation for consistency with normal queries.
1 parent 7ad5e72 commit a6ed44d

3 files changed

Lines changed: 28 additions & 22 deletions

File tree

compiler/rustc_middle/src/dep_graph/dep_node.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -356,11 +356,12 @@ impl StableOrd for WorkProductId {
356356
const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
357357
}
358358

359+
// Note: `$K` and `$V` are unused but present so this can be called by `rustc_with_all_queries`.
359360
macro_rules! define_dep_nodes {
360361
(
361362
$(
362363
$(#[$attr:meta])*
363-
[$($modifiers:tt)*] fn $variant:ident($($K:tt)*) -> $V:ty,
364+
[$($modifiers:tt)*] fn $variant:ident($K:ty) -> $V:ty,
364365
)*
365366
) => {
366367

@@ -427,18 +428,18 @@ macro_rules! define_dep_nodes {
427428
}
428429

429430
// Create various data structures for each query, and also for a few things
430-
// that aren't queries.
431+
// that aren't queries. The key and return types aren't used, hence the use of `()`.
431432
rustc_with_all_queries!(define_dep_nodes![
432433
/// We use this for most things when incr. comp. is turned off.
433-
[] fn Null() -> (),
434+
[] fn Null(()) -> (),
434435
/// We use this to create a forever-red node.
435-
[] fn Red() -> (),
436-
[] fn SideEffect() -> (),
437-
[] fn AnonZeroDeps() -> (),
438-
[] fn TraitSelect() -> (),
439-
[] fn CompileCodegenUnit() -> (),
440-
[] fn CompileMonoItem() -> (),
441-
[] fn Metadata() -> (),
436+
[] fn Red(()) -> (),
437+
[] fn SideEffect(()) -> (),
438+
[] fn AnonZeroDeps(()) -> (),
439+
[] fn TraitSelect(()) -> (),
440+
[] fn CompileCodegenUnit(()) -> (),
441+
[] fn CompileMonoItem(()) -> (),
442+
[] fn Metadata(()) -> (),
442443
]);
443444

444445
// WARNING: `construct` is generic and does not know that `CompileCodegenUnit` takes `Symbol`s as keys.

compiler/rustc_middle/src/query/plumbing.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,8 @@ macro_rules! if_return_result_from_ensure_ok {
297297

298298
macro_rules! define_callbacks {
299299
(
300+
// You might expect the key to be `$K:ty`, but it needs to be `$($K:tt)*` so that
301+
// `query_helper_param_ty!` can match on specific type names.
300302
$(
301303
$(#[$attr:meta])*
302304
[$($modifiers:tt)*]
@@ -314,17 +316,17 @@ macro_rules! define_callbacks {
314316

315317
pub type LocalKey<'tcx> = if_separate_provide_extern!(
316318
[$($modifiers)*]
317-
(<$($K)* as $crate::query::AsLocalKey>::LocalKey)
318-
($($K)*)
319+
(<Key<'tcx> as $crate::query::AsLocalKey>::LocalKey)
320+
(Key<'tcx>)
319321
);
320322

321323
/// This type alias specifies the type returned from query providers and the type
322324
/// used for decoding. For regular queries this is the declared returned type `V`,
323325
/// but `arena_cache` will use `<V as ArenaCached>::Provided` instead.
324326
pub type ProvidedValue<'tcx> = if_arena_cache!(
325327
[$($modifiers)*]
326-
(<$V as $crate::query::arena_cached::ArenaCached<'tcx>>::Provided)
327-
($V)
328+
(<Value<'tcx> as $crate::query::arena_cached::ArenaCached<'tcx>>::Provided)
329+
(Value<'tcx>)
328330
);
329331

330332
/// This helper function takes a value returned by the query provider
@@ -341,7 +343,9 @@ macro_rules! define_callbacks {
341343
let value: Value<'tcx> = if_arena_cache!(
342344
[$($modifiers)*]
343345
{
344-
<$V as $crate::query::arena_cached::ArenaCached>::alloc_in_arena(
346+
<Value<'tcx> as $crate::query::arena_cached::ArenaCached>::
347+
alloc_in_arena
348+
(
345349
tcx,
346350
&tcx.query_system.arenas.$name,
347351
provided_value,
@@ -356,7 +360,8 @@ macro_rules! define_callbacks {
356360
erase::erase_val(value)
357361
}
358362

359-
pub type Storage<'tcx> = <$($K)* as $crate::query::Key>::Cache<Erased<$V>>;
363+
pub type Storage<'tcx> =
364+
<Key<'tcx> as $crate::query::Key>::Cache<Erased<Value<'tcx>>>;
360365

361366
// Ensure that keys grow no larger than 88 bytes by accident.
362367
// Increase this limit if necessary, but do try to keep the size low if possible
@@ -496,7 +501,7 @@ macro_rules! define_callbacks {
496501
#[derive(Default)]
497502
pub struct QueryStates<'tcx> {
498503
$(
499-
pub $name: $crate::query::QueryState<'tcx, $($K)*>,
504+
pub $name: $crate::query::QueryState<'tcx, $name::Key<'tcx>>,
500505
)*
501506
}
502507

@@ -573,16 +578,17 @@ macro_rules! define_callbacks {
573578
};
574579
}
575580

581+
// Note: `$V` is unused but present so this can be called by `rustc_with_all_queries`.
576582
macro_rules! define_feedable {
577583
(
578584
$(
579585
$(#[$attr:meta])*
580586
[$($modifiers:tt)*]
581-
fn $name:ident($($K:tt)*) -> $V:ty,
587+
fn $name:ident($K:ty) -> $V:ty,
582588
)*
583589
) => {
584590
$(
585-
impl<'tcx, K: $crate::query::IntoQueryParam<$($K)*> + Copy> TyCtxtFeed<'tcx, K> {
591+
impl<'tcx, K: $crate::query::IntoQueryParam<$K> + Copy> TyCtxtFeed<'tcx, K> {
586592
$(#[$attr])*
587593
#[inline(always)]
588594
pub fn $name(self, value: $name::ProvidedValue<'tcx>) {

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -473,13 +473,12 @@ pub(crate) fn force_from_dep_node_inner<'tcx, C: QueryCache, const FLAGS: QueryF
473473
}
474474
}
475475

476-
// NOTE: `$V` isn't used here, but we still need to match on it so it can be passed to other macros
477-
// invoked by `rustc_with_all_queries`.
476+
// Note: `$K` and `$V` are unused but present so this can be called by `rustc_with_all_queries`.
478477
macro_rules! define_queries {
479478
(
480479
$(
481480
$(#[$attr:meta])*
482-
[$($modifiers:tt)*] fn $name:ident($($K:tt)*) -> $V:ty,
481+
[$($modifiers:tt)*] fn $name:ident($K:ty) -> $V:ty,
483482
)*
484483
) => {
485484

0 commit comments

Comments
 (0)