Skip to content

Commit aba0ff1

Browse files
authored
Unrolled build for #152153
Rollup merge of #152153 - Zalathar:descs, r=nnethercote Incorporate query description functions into `QueryVTable` Putting a `desc` function in each query vtable reduces the amount of parameter juggling required when creating query stack frames, because almost all of the necessary information can be found in the vtable. There should be no change to compiler output.
2 parents 0a13b43 + 4382206 commit aba0ff1

File tree

3 files changed

+47
-34
lines changed

3 files changed

+47
-34
lines changed

‎compiler/rustc_macros/src/query.rs‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,14 @@ pub(super) fn rustc_queries(input: TokenStream) -> TokenStream {
429429
$macro!(#feedable_queries);
430430
}
431431
}
432-
pub mod descs {
432+
433+
/// Functions that format a human-readable description of each query
434+
/// and its key, as specified by the `desc` query modifier.
435+
///
436+
/// (The leading `_` avoids collisions with actual query names when
437+
/// expanded in `rustc_middle::queries`, and makes this macro-generated
438+
/// module easier to search for.)
439+
pub mod _description_fns {
433440
use super::*;
434441
#query_description_stream
435442
}

‎compiler/rustc_middle/src/query/plumbing.rs‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ pub type IsLoadableFromDiskFn<'tcx, Key> =
3232

3333
/// Stores function pointers and other metadata for a particular query.
3434
///
35-
/// Used indirectly by query plumbing in `rustc_query_system`, via a trait.
35+
/// Used indirectly by query plumbing in `rustc_query_system` via a trait,
36+
/// and also used directly by query plumbing in `rustc_query_impl`.
3637
pub struct QueryVTable<'tcx, C: QueryCache> {
3738
pub name: &'static str,
3839
pub eval_always: bool,
@@ -52,6 +53,12 @@ pub struct QueryVTable<'tcx, C: QueryCache> {
5253
pub value_from_cycle_error:
5354
fn(tcx: TyCtxt<'tcx>, cycle_error: &CycleError, guar: ErrorGuaranteed) -> C::Value,
5455
pub format_value: fn(&C::Value) -> String,
56+
57+
/// Formats a human-readable description of this query and its key, as
58+
/// specified by the `desc` query modifier.
59+
///
60+
/// Used when reporting query cycle errors and similar problems.
61+
pub description_fn: fn(TyCtxt<'tcx>, C::Key) -> String,
5562
}
5663

5764
pub struct QuerySystemFns {

‎compiler/rustc_query_impl/src/plumbing.rs‎

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
99
use rustc_data_structures::sync::{DynSend, DynSync};
1010
use rustc_data_structures::unord::UnordMap;
1111
use rustc_hashes::Hash64;
12+
use rustc_hir::def_id::DefId;
1213
use rustc_hir::limit::Limit;
1314
use rustc_index::Idx;
1415
use rustc_middle::bug;
1516
use rustc_middle::dep_graph::{
16-
self, DepContext, DepKind, DepKindVTable, DepNode, DepNodeIndex, SerializedDepNodeIndex,
17-
dep_kinds,
17+
self, DepContext, DepKindVTable, DepNode, DepNodeIndex, SerializedDepNodeIndex, dep_kinds,
1818
};
1919
use rustc_middle::query::Key;
2020
use rustc_middle::query::on_disk_cache::{
2121
AbsoluteBytePos, CacheDecoder, CacheEncoder, EncodedDepNodeIndex,
2222
};
23+
use rustc_middle::query::plumbing::QueryVTable;
2324
use rustc_middle::ty::codec::TyEncoder;
2425
use rustc_middle::ty::print::with_reduced_queries;
2526
use rustc_middle::ty::tls::{self, ImplicitCtxt};
@@ -312,37 +313,36 @@ macro_rules! should_ever_cache_on_disk {
312313
};
313314
}
314315

315-
fn mk_query_stack_frame_extra<'tcx, K: Key + Copy + 'tcx>(
316-
(tcx, key, kind, name, do_describe): (
317-
TyCtxt<'tcx>,
318-
K,
319-
DepKind,
320-
&'static str,
321-
fn(TyCtxt<'tcx>, K) -> String,
322-
),
323-
) -> QueryStackFrameExtra {
316+
/// The deferred part of a deferred query stack frame.
317+
fn mk_query_stack_frame_extra<'tcx, Cache>(
318+
(tcx, vtable, key): (TyCtxt<'tcx>, &'tcx QueryVTable<'tcx, Cache>, Cache::Key),
319+
) -> QueryStackFrameExtra
320+
where
321+
Cache: QueryCache,
322+
Cache::Key: Key,
323+
{
324324
let def_id = key.key_as_def_id();
325325

326326
// If reduced queries are requested, we may be printing a query stack due
327327
// to a panic. Avoid using `default_span` and `def_kind` in that case.
328328
let reduce_queries = with_reduced_queries();
329329

330330
// Avoid calling queries while formatting the description
331-
let description = ty::print::with_no_queries!(do_describe(tcx, key));
331+
let description = ty::print::with_no_queries!((vtable.description_fn)(tcx, key));
332332
let description = if tcx.sess.verbose_internals() {
333-
format!("{description} [{name:?}]")
333+
format!("{description} [{name:?}]", name = vtable.name)
334334
} else {
335335
description
336336
};
337-
let span = if kind == dep_graph::dep_kinds::def_span || reduce_queries {
337+
let span = if vtable.dep_kind == dep_graph::dep_kinds::def_span || reduce_queries {
338338
// The `def_span` query is used to calculate `default_span`,
339339
// so exit to avoid infinite recursion.
340340
None
341341
} else {
342342
Some(key.default_span(tcx))
343343
};
344344

345-
let def_kind = if kind == dep_graph::dep_kinds::def_kind || reduce_queries {
345+
let def_kind = if vtable.dep_kind == dep_graph::dep_kinds::def_kind || reduce_queries {
346346
// Try to avoid infinite recursion.
347347
None
348348
} else {
@@ -351,29 +351,28 @@ fn mk_query_stack_frame_extra<'tcx, K: Key + Copy + 'tcx>(
351351
QueryStackFrameExtra::new(description, span, def_kind)
352352
}
353353

354-
pub(crate) fn create_query_frame<
355-
'tcx,
356-
K: Copy + DynSend + DynSync + Key + for<'a> HashStable<StableHashingContext<'a>> + 'tcx,
357-
>(
354+
pub(crate) fn create_deferred_query_stack_frame<'tcx, Cache>(
358355
tcx: TyCtxt<'tcx>,
359-
do_describe: fn(TyCtxt<'tcx>, K) -> String,
360-
key: K,
361-
kind: DepKind,
362-
name: &'static str,
363-
) -> QueryStackFrame<QueryStackDeferred<'tcx>> {
364-
let def_id = key.key_as_def_id();
356+
vtable: &'tcx QueryVTable<'tcx, Cache>,
357+
key: Cache::Key,
358+
) -> QueryStackFrame<QueryStackDeferred<'tcx>>
359+
where
360+
Cache: QueryCache,
361+
Cache::Key: Key + DynSend + DynSync + for<'a> HashStable<StableHashingContext<'a>> + 'tcx,
362+
{
363+
let kind = vtable.dep_kind;
365364

366365
let hash = tcx.with_stable_hashing_context(|mut hcx| {
367366
let mut hasher = StableHasher::new();
368367
kind.as_usize().hash_stable(&mut hcx, &mut hasher);
369368
key.hash_stable(&mut hcx, &mut hasher);
370369
hasher.finish::<Hash64>()
371370
});
372-
let def_id_for_ty_in_cycle = key.def_id_for_ty_in_cycle();
373371

374-
let info =
375-
QueryStackDeferred::new((tcx, key, kind, name, do_describe), mk_query_stack_frame_extra);
372+
let def_id: Option<DefId> = key.key_as_def_id();
373+
let def_id_for_ty_in_cycle: Option<DefId> = key.def_id_for_ty_in_cycle();
376374

375+
let info = QueryStackDeferred::new((tcx, vtable, key), mk_query_stack_frame_extra);
377376
QueryStackFrame::new(info, kind, hash, def_id, def_id_for_ty_in_cycle)
378377
}
379378

@@ -697,6 +696,7 @@ macro_rules! define_queries {
697696
},
698697
hash_result: hash_result!([$($modifiers)*][queries::$name::Value<'tcx>]),
699698
format_value: |value| format!("{:?}", erase::restore_val::<queries::$name::Value<'tcx>>(*value)),
699+
description_fn: $crate::queries::_description_fns::$name,
700700
}
701701
}
702702

@@ -742,10 +742,9 @@ macro_rules! define_queries {
742742
qmap: &mut QueryMap<'tcx>,
743743
require_complete: bool,
744744
) -> Option<()> {
745-
let make_frame = |tcx, key| {
746-
let kind = rustc_middle::dep_graph::dep_kinds::$name;
747-
let name = stringify!($name);
748-
$crate::plumbing::create_query_frame(tcx, queries::descs::$name, key, kind, name)
745+
let make_frame = |tcx: TyCtxt<'tcx>, key| {
746+
let vtable = &tcx.query_system.query_vtables.$name;
747+
$crate::plumbing::create_deferred_query_stack_frame(tcx, vtable, key)
749748
};
750749

751750
// Call `gather_active_jobs_inner` to do the actual work.

0 commit comments

Comments
 (0)