Skip to content

Commit 83c4577

Browse files
committed
Auto merge of #153907 - Zalathar:stack-frame, r=<try>
Remove redundant fields from `QueryStackFrame`
2 parents 9f0615c + ff1f6ea commit 83c4577

File tree

6 files changed

+37
-46
lines changed

6 files changed

+37
-46
lines changed

compiler/rustc_middle/src/query/plumbing.rs

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

513513
impl<'tcx> TaggedQueryKey<'tcx> {
514+
/// Returns the name of the query this key is tagged with.
515+
///
516+
/// This is useful for error/debug output, but don't use it to check for
517+
/// specific query names. Instead, match on the `TaggedQueryKey` variant.
518+
pub fn query_name(&self) -> &'static str {
519+
match self {
520+
$(
521+
TaggedQueryKey::$name(_) => stringify!($name),
522+
)*
523+
}
524+
}
525+
514526
/// Formats a human-readable description of this query and its key, as
515527
/// specified by the `desc` query modifier.
516528
///
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
@@ -10,7 +10,7 @@ use rustc_middle::dep_graph::{DepGraphData, DepNodeKey, SerializedDepNodeIndex};
1010
use rustc_middle::query::plumbing::QueryVTable;
1111
use rustc_middle::query::{
1212
ActiveKeyStatus, CycleError, EnsureMode, QueryCache, QueryJob, QueryJobId, QueryKey,
13-
QueryLatch, QueryMode, QueryState,
13+
QueryLatch, QueryMode, QueryStackFrame, QueryState,
1414
};
1515
use rustc_middle::ty::TyCtxt;
1616
use rustc_middle::verify_ich::incremental_verify_ich;
@@ -89,8 +89,9 @@ fn gather_active_jobs<'tcx, C>(
8989
let mut gather_shard_jobs = |shard: &HashTable<(C::Key, ActiveKeyStatus<'tcx>)>| {
9090
for (key, status) in shard.iter() {
9191
if let ActiveKeyStatus::Started(job) = status {
92-
// This function is safe to call with the shard locked because it is very simple.
93-
let frame = crate::plumbing::create_query_stack_frame(query, *key);
92+
// It's fine to call `create_tagged_key` with the shard locked,
93+
// because it's just a `TaggedQueryKey` variant constructor.
94+
let frame = QueryStackFrame { tagged_key: (query.create_tagged_key)(*key) };
9495
job_map.insert(job.id, QueryJobInfo { frame, job: job.clone() });
9596
}
9697
}

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 info in &cycle_error.cycle {
80-
if info.frame.dep_kind == DepKind::check_representability
81-
&& let Some(field_id) = info.frame.def_id
82-
&& let Some(field_id) = field_id.as_local()
83-
&& let Some(DefKind::Field) = info.frame.tagged_key.def_kind(tcx)
79+
if let TaggedQueryKey::check_representability(def_id) = info.frame.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(info) = cycle.get(0)
113-
&& info.frame.dep_kind == DepKind::variances_of
114-
&& let Some(def_id) = info.frame.def_id
111+
&& let TaggedQueryKey::variances_of(def_id) = info.frame.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: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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,8 +88,8 @@ pub(crate) fn find_cycle_in_stack<'tcx>(
8888
panic!("did not find a cycle")
8989
}
9090

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

105107
while let Some(id) = info.job.parent {
106108
info = &job_map.map[&id];
107-
if info.frame.dep_kind == dep_kind {
109+
if mem::discriminant(&info.frame.tagged_key) == expected_query {
108110
depth += 1;
109111
last_info = info;
110112
}
@@ -420,8 +422,8 @@ pub fn print_query_stack<'tcx>(
420422
if Some(count_printed) < limit_frames || limit_frames.is_none() {
421423
// Only print to stderr as many stack frames as `num_frames` when present.
422424
dcx.struct_failure_note(format!(
423-
"#{} [{:?}] {}",
424-
count_printed, query_info.frame.dep_kind, description
425+
"#{count_printed} [{query_name}] {description}",
426+
query_name = query_info.frame.tagged_key.query_name(),
425427
))
426428
.with_span(query_info.job.span)
427429
.emit();
@@ -431,8 +433,8 @@ pub fn print_query_stack<'tcx>(
431433
if let Some(ref mut file) = file {
432434
let _ = writeln!(
433435
file,
434-
"#{} [{:?}] {}",
435-
count_total, query_info.frame.dep_kind, description
436+
"#{count_total} [{query_name}] {description}",
437+
query_name = query_info.frame.tagged_key.query_name(),
436438
);
437439
}
438440

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 1 addition & 20 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;
@@ -14,7 +12,7 @@ use rustc_middle::query::on_disk_cache::{
1412
AbsoluteBytePos, CacheDecoder, CacheEncoder, EncodedDepNodeIndex,
1513
};
1614
use rustc_middle::query::plumbing::QueryVTable;
17-
use rustc_middle::query::{QueryCache, QueryJobId, QueryKey, QueryMode, QueryStackFrame, erase};
15+
use rustc_middle::query::{QueryCache, QueryJobId, QueryMode, erase};
1816
use rustc_middle::ty::TyCtxt;
1917
use rustc_middle::ty::codec::TyEncoder;
2018
use rustc_middle::ty::tls::{self, ImplicitCtxt};
@@ -82,23 +80,6 @@ pub(crate) fn start_query<R>(
8280
})
8381
}
8482

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

0 commit comments

Comments
 (0)