Skip to content

Commit bc1b6b8

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 5142af7 commit bc1b6b8

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
@@ -324,11 +324,12 @@ impl StableOrd for WorkProductId {
324324
const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
325325
}
326326

327+
// Note: `$K` and `$V` are unused but present so this can be called by `rustc_with_all_queries`.
327328
macro_rules! define_dep_nodes {
328329
(
329330
$(
330331
$(#[$attr:meta])*
331-
[$($modifiers:tt)*] fn $variant:ident($($K:tt)*) -> $V:ty,
332+
[$($modifiers:tt)*] fn $variant:ident($K:ty) -> $V:ty,
332333
)*
333334
) => {
334335

@@ -382,20 +383,20 @@ macro_rules! define_dep_nodes {
382383
}
383384

384385
// Create various data structures for each query, and also for a few things
385-
// that aren't queries.
386+
// that aren't queries. The key and return types aren't used, hence the use of `()`.
386387
rustc_with_all_queries!(define_dep_nodes![
387388
/// We use this for most things when incr. comp. is turned off.
388-
[] fn Null() -> (),
389+
[] fn Null(()) -> (),
389390
/// We use this to create a forever-red node.
390-
[] fn Red() -> (),
391+
[] fn Red(()) -> (),
391392
/// We use this to create a side effect node.
392-
[] fn SideEffect() -> (),
393+
[] fn SideEffect(()) -> (),
393394
/// We use this to create the anon node with zero dependencies.
394-
[] fn AnonZeroDeps() -> (),
395-
[] fn TraitSelect() -> (),
396-
[] fn CompileCodegenUnit() -> (),
397-
[] fn CompileMonoItem() -> (),
398-
[] fn Metadata() -> (),
395+
[] fn AnonZeroDeps(()) -> (),
396+
[] fn TraitSelect(()) -> (),
397+
[] fn CompileCodegenUnit(()) -> (),
398+
[] fn CompileMonoItem(()) -> (),
399+
[] fn Metadata(()) -> (),
399400
]);
400401

401402
// 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
@@ -471,13 +471,12 @@ pub(crate) fn force_from_dep_node_inner<'tcx, C: QueryCache, const FLAGS: QueryF
471471
}
472472
}
473473

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

0 commit comments

Comments
 (0)