Skip to content

Commit d57577e

Browse files
committed
Remove redundant fields from QueryStackFrame
An explicit dep-kind is redundant here. If we want to find the query name, or check for specific queries, we can inspect the `TaggedQueryKey` instead. Similarly, in cases where the key is some kind of `DefId`, we can extract it directly from the `TaggedQueryKey`.
1 parent fe7294f commit d57577e

6 files changed

Lines changed: 38 additions & 48 deletions

File tree

compiler/rustc_middle/src/query/plumbing.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,18 @@ macro_rules! define_callbacks {
513513
}
514514

515515
impl<'tcx> TaggedQueryKey<'tcx> {
516+
/// Returns the name of the query this key is tagged with.
517+
///
518+
/// This is useful for error/debug output, but don't use it to check for
519+
/// specific query names. Instead, match on the `TaggedQueryKey` variant.
520+
pub fn query_name(&self) -> &'static str {
521+
match self {
522+
$(
523+
TaggedQueryKey::$name(_) => stringify!($name),
524+
)*
525+
}
526+
}
527+
516528
/// Formats a human-readable description of this query and its key, as
517529
/// specified by the `desc` query modifier.
518530
///
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
use std::fmt::Debug;
2-
3-
use rustc_span::def_id::DefId;
4-
5-
use crate::dep_graph::DepKind;
61
use crate::queries::TaggedQueryKey;
72

83
/// Description of a frame in the query stack.
94
///
105
/// This is mostly used in case of cycles for error reporting.
116
#[derive(Clone, Debug)]
127
pub struct QueryStackFrame<'tcx> {
8+
/// The query and key of the query method call that this stack frame
9+
/// corresponds to.
10+
///
11+
/// Code that doesn't care about the specific key can still use this to
12+
/// check which query it's for, or obtain the query's name.
1313
pub tagged_key: TaggedQueryKey<'tcx>,
14-
pub dep_kind: DepKind,
15-
pub def_id: Option<DefId>,
1614
}

compiler/rustc_query_impl/src/execution.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_errors::FatalError;
99
use rustc_middle::dep_graph::{DepGraphData, DepNodeKey, SerializedDepNodeIndex};
1010
use rustc_middle::query::{
1111
ActiveKeyStatus, CycleError, EnsureMode, QueryCache, QueryJob, QueryJobId, QueryKey,
12-
QueryLatch, QueryMode, QueryState, QueryVTable,
12+
QueryLatch, QueryMode, QueryStackFrame, QueryState, QueryVTable,
1313
};
1414
use rustc_middle::ty::TyCtxt;
1515
use rustc_middle::verify_ich::incremental_verify_ich;
@@ -73,8 +73,9 @@ fn collect_active_query_jobs_inner<'tcx, C>(
7373
let mut collect_shard_jobs = |shard: &HashTable<(C::Key, ActiveKeyStatus<'tcx>)>| {
7474
for (key, status) in shard.iter() {
7575
if let ActiveKeyStatus::Started(job) = status {
76-
// This function is safe to call with the shard locked because it is very simple.
77-
let frame = crate::plumbing::create_query_stack_frame(query, *key);
76+
// It's fine to call `create_tagged_key` with the shard locked,
77+
// because it's just a `TaggedQueryKey` variant constructor.
78+
let frame = QueryStackFrame { tagged_key: (query.create_tagged_key)(*key) };
7879
job_map.insert(job.id, QueryJobInfo { frame, job: job.clone() });
7980
}
8081
}

compiler/rustc_query_impl/src/from_cycle_error.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use rustc_errors::codes::*;
88
use rustc_errors::{Applicability, Diag, MultiSpan, pluralize, struct_span_code_err};
99
use rustc_hir as hir;
1010
use rustc_hir::def::{DefKind, Res};
11-
use rustc_middle::dep_graph::DepKind;
1211
use rustc_middle::queries::{QueryVTables, TaggedQueryKey};
1312
use rustc_middle::query::CycleError;
1413
use rustc_middle::query::erase::erase_val;
@@ -77,11 +76,10 @@ fn check_representability<'tcx>(tcx: TyCtxt<'tcx>, cycle_error: CycleError<'tcx>
7776
let mut item_and_field_ids = Vec::new();
7877
let mut representable_ids = FxHashSet::default();
7978
for frame in &cycle_error.cycle {
80-
if frame.node.dep_kind == DepKind::check_representability
81-
&& let Some(field_id) = frame.node.def_id
82-
&& let Some(field_id) = field_id.as_local()
83-
&& let Some(DefKind::Field) = frame.node.tagged_key.def_kind(tcx)
79+
if let TaggedQueryKey::check_representability(def_id) = frame.node.tagged_key
80+
&& tcx.def_kind(def_id) == DefKind::Field
8481
{
82+
let field_id: LocalDefId = def_id;
8583
let parent_id = tcx.parent(field_id.to_def_id());
8684
let item_id = match tcx.def_kind(parent_id) {
8785
DefKind::Variant => tcx.parent(parent_id),
@@ -110,8 +108,7 @@ fn variances_of<'tcx>(tcx: TyCtxt<'tcx>, cycle_error: CycleError<'tcx>) -> &'tcx
110108
&cycle_error.cycle,
111109
|cycle| {
112110
if let Some(frame) = cycle.get(0)
113-
&& frame.node.dep_kind == DepKind::variances_of
114-
&& let Some(def_id) = frame.node.def_id
111+
&& let TaggedQueryKey::variances_of(def_id) = frame.node.tagged_key
115112
{
116113
let n = tcx.generics_of(def_id).own_params.len();
117114
ControlFlow::Break(tcx.arena.alloc_from_iter(iter::repeat_n(ty::Bivariant, n)))

compiler/rustc_query_impl/src/job.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use std::io::Write;
2-
use std::iter;
32
use std::ops::ControlFlow;
43
use std::sync::Arc;
4+
use std::{iter, mem};
55

66
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
77
use rustc_errors::{Diag, DiagCtxtHandle};
88
use rustc_hir::def::DefKind;
9+
use rustc_middle::queries::TaggedQueryKey;
910
use rustc_middle::query::{
1011
CycleError, QueryJob, QueryJobId, QueryLatch, QueryStackFrame, QueryWaiter,
1112
};
@@ -88,8 +89,8 @@ pub(crate) fn find_cycle_in_stack<'tcx>(
8889
panic!("did not find a cycle")
8990
}
9091

91-
/// Finds the job closest to the root with a `DepKind` matching the `DepKind` of `id` and returns
92-
/// information about it.
92+
/// Finds the query job closest to the root that is for the same query method as `id`
93+
/// (but not necessarily the same query key), and returns information about it.
9394
#[cold]
9495
#[inline(never)]
9596
pub(crate) fn find_dep_kind_root<'tcx>(
@@ -99,12 +100,14 @@ pub(crate) fn find_dep_kind_root<'tcx>(
99100
) -> (Span, String, usize) {
100101
let mut depth = 1;
101102
let mut info = &job_map.map[&id];
102-
let dep_kind = info.frame.dep_kind;
103+
// Two query stack frames are for the same query method if they have the same
104+
// `TaggedQueryKey` discriminant.
105+
let expected_query = mem::discriminant::<TaggedQueryKey<'tcx>>(&info.frame.tagged_key);
103106
let mut last_info = info;
104107

105108
while let Some(id) = info.job.parent {
106109
info = &job_map.map[&id];
107-
if info.frame.dep_kind == dep_kind {
110+
if mem::discriminant(&info.frame.tagged_key) == expected_query {
108111
depth += 1;
109112
last_info = info;
110113
}
@@ -420,8 +423,8 @@ pub fn print_query_stack<'tcx>(
420423
if Some(count_printed) < limit_frames || limit_frames.is_none() {
421424
// Only print to stderr as many stack frames as `num_frames` when present.
422425
dcx.struct_failure_note(format!(
423-
"#{} [{:?}] {}",
424-
count_printed, query_info.frame.dep_kind, description
426+
"#{count_printed} [{query_name}] {description}",
427+
query_name = query_info.frame.tagged_key.query_name(),
425428
))
426429
.with_span(query_info.job.span)
427430
.emit();
@@ -431,8 +434,8 @@ pub fn print_query_stack<'tcx>(
431434
if let Some(ref mut file) = file {
432435
let _ = writeln!(
433436
file,
434-
"#{} [{:?}] {}",
435-
count_total, query_info.frame.dep_kind, description
437+
"#{count_total} [{query_name}] {description}",
438+
query_name = query_info.frame.tagged_key.query_name(),
436439
);
437440
}
438441

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use std::num::NonZero;
22

3-
use rustc_data_structures::sync::{DynSend, DynSync};
43
use rustc_data_structures::unord::UnordMap;
5-
use rustc_hir::def_id::DefId;
64
use rustc_hir::limit::Limit;
75
use rustc_index::Idx;
86
use rustc_middle::bug;
@@ -13,9 +11,7 @@ use rustc_middle::query::erase::{Erasable, Erased};
1311
use rustc_middle::query::on_disk_cache::{
1412
AbsoluteBytePos, CacheDecoder, CacheEncoder, EncodedDepNodeIndex,
1513
};
16-
use rustc_middle::query::{
17-
QueryCache, QueryJobId, QueryKey, QueryMode, QueryStackFrame, QueryVTable, erase,
18-
};
14+
use rustc_middle::query::{QueryCache, QueryJobId, QueryMode, QueryVTable, erase};
1915
use rustc_middle::ty::TyCtxt;
2016
use rustc_middle::ty::codec::TyEncoder;
2117
use rustc_middle::ty::tls::{self, ImplicitCtxt};
@@ -83,23 +79,6 @@ pub(crate) fn start_query<R>(
8379
})
8480
}
8581

86-
pub(crate) fn create_query_stack_frame<'tcx, C>(
87-
vtable: &'tcx QueryVTable<'tcx, C>,
88-
key: C::Key,
89-
) -> QueryStackFrame<'tcx>
90-
where
91-
C: QueryCache<Key: QueryKey + DynSend + DynSync>,
92-
QueryVTable<'tcx, C>: DynSync,
93-
{
94-
let def_id: Option<DefId> = key.key_as_def_id();
95-
96-
QueryStackFrame {
97-
tagged_key: (vtable.create_tagged_key)(key),
98-
dep_kind: vtable.dep_kind,
99-
def_id,
100-
}
101-
}
102-
10382
pub(crate) fn encode_query_values<'tcx>(
10483
tcx: TyCtxt<'tcx>,
10584
encoder: &mut CacheEncoder<'_, 'tcx>,

0 commit comments

Comments
 (0)