Skip to content

Commit 2caef89

Browse files
committed
fix(query): Pass query key to value_from_cycle_error
1 parent d1c7945 commit 2caef89

4 files changed

Lines changed: 60 additions & 22 deletions

File tree

compiler/rustc_middle/src/query/plumbing.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,12 @@ pub struct QueryVTable<'tcx, C: QueryCache> {
138138
/// For `no_hash` queries, this function pointer is None.
139139
pub hash_value_fn: Option<fn(&mut StableHashingContext<'_>, &C::Value) -> Fingerprint>,
140140

141-
pub value_from_cycle_error:
142-
fn(tcx: TyCtxt<'tcx>, cycle_error: CycleError, guar: ErrorGuaranteed) -> C::Value,
141+
pub value_from_cycle_error: fn(
142+
tcx: TyCtxt<'tcx>,
143+
key: C::Key,
144+
cycle_error: CycleError,
145+
guar: ErrorGuaranteed,
146+
) -> C::Value,
143147
pub format_value: fn(&C::Value) -> String,
144148

145149
/// Formats a human-readable description of this query and its key, as

compiler/rustc_query_impl/src/execution.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,18 @@ where
9898
fn mk_cycle<'tcx, C: QueryCache>(
9999
query: &'tcx QueryVTable<'tcx, C>,
100100
tcx: TyCtxt<'tcx>,
101+
key: C::Key,
101102
cycle_error: CycleError,
102103
) -> C::Value {
103104
let error = report_cycle(tcx.sess, &cycle_error);
104105
match query.cycle_error_handling {
105106
CycleErrorHandling::Error => {
106107
let guar = error.emit();
107-
(query.value_from_cycle_error)(tcx, cycle_error, guar)
108+
(query.value_from_cycle_error)(tcx, key, cycle_error, guar)
108109
}
109110
CycleErrorHandling::DelayBug => {
110111
let guar = error.delay_as_bug();
111-
(query.value_from_cycle_error)(tcx, cycle_error, guar)
112+
(query.value_from_cycle_error)(tcx, key, cycle_error, guar)
112113
}
113114
CycleErrorHandling::Stash => {
114115
let guar = if let Some(root) = cycle_error.cycle.first()
@@ -118,7 +119,7 @@ fn mk_cycle<'tcx, C: QueryCache>(
118119
} else {
119120
error.emit()
120121
};
121-
(query.value_from_cycle_error)(tcx, cycle_error, guar)
122+
(query.value_from_cycle_error)(tcx, key, cycle_error, guar)
122123
}
123124
}
124125
}
@@ -202,6 +203,7 @@ where
202203
fn cycle_error<'tcx, C: QueryCache>(
203204
query: &'tcx QueryVTable<'tcx, C>,
204205
tcx: TyCtxt<'tcx>,
206+
key: C::Key,
205207
try_execute: QueryJobId,
206208
span: Span,
207209
) -> (C::Value, Option<DepNodeIndex>) {
@@ -212,7 +214,7 @@ fn cycle_error<'tcx, C: QueryCache>(
212214
.expect("failed to collect active queries");
213215

214216
let error = find_cycle_in_stack(try_execute, job_map, &current_query_job(), span);
215-
(mk_cycle(query, tcx, error.lift()), None)
217+
(mk_cycle(query, tcx, key, error.lift()), None)
216218
}
217219

218220
#[inline(always)]
@@ -257,7 +259,7 @@ fn wait_for_query<'tcx, C: QueryCache>(
257259

258260
(v, Some(index))
259261
}
260-
Err(cycle) => (mk_cycle(query, tcx, cycle.lift()), None),
262+
Err(cycle) => (mk_cycle(query, tcx, key, cycle.lift()), None),
261263
}
262264
}
263265

@@ -320,7 +322,7 @@ fn try_execute_query<'tcx, C: QueryCache, const INCR: bool>(
320322

321323
// If we are single-threaded we know that we have cycle error,
322324
// so we just return the error.
323-
cycle_error(query, tcx, id, span)
325+
cycle_error(query, tcx, key, id, span)
324326
}
325327
}
326328
ActiveKeyStatus::Poisoned => FatalError.raise(),

compiler/rustc_query_impl/src/from_cycle_error.rs

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_middle::query::CycleError;
1212
use rustc_middle::query::plumbing::CyclePlaceholder;
1313
use rustc_middle::ty::{self, Representability, Ty, TyCtxt};
1414
use rustc_middle::{bug, span_bug};
15-
use rustc_span::def_id::LocalDefId;
15+
use rustc_span::def_id::{DefId, LocalDefId};
1616
use rustc_span::{ErrorGuaranteed, Span};
1717

1818
use crate::job::report_cycle;
@@ -22,14 +22,20 @@ pub(crate) trait FromCycleError<'tcx>: Sized {
2222
///
2323
/// Note: the default impl calls `raise_fatal`, ending compilation immediately! Only a few
2424
/// types override this with a non-fatal impl.
25-
fn from_cycle_error(tcx: TyCtxt<'tcx>, cycle_error: CycleError, guar: ErrorGuaranteed) -> Self;
25+
fn from_cycle_error(
26+
tcx: TyCtxt<'tcx>,
27+
cycle_error: CycleError,
28+
guar: ErrorGuaranteed,
29+
key_def_id: Option<DefId>,
30+
) -> Self;
2631
}
2732

2833
impl<'tcx, T> FromCycleError<'tcx> for T {
2934
default fn from_cycle_error(
3035
tcx: TyCtxt<'tcx>,
3136
cycle_error: CycleError,
3237
_guar: ErrorGuaranteed,
38+
_key_def_id: Option<DefId>,
3339
) -> T {
3440
let Some(guar) = tcx.sess.dcx().has_errors() else {
3541
bug!(
@@ -43,26 +49,39 @@ impl<'tcx, T> FromCycleError<'tcx> for T {
4349
}
4450

4551
impl<'tcx> FromCycleError<'tcx> for Ty<'_> {
46-
fn from_cycle_error(tcx: TyCtxt<'tcx>, _: CycleError, guar: ErrorGuaranteed) -> Self {
52+
fn from_cycle_error(
53+
tcx: TyCtxt<'tcx>,
54+
_: CycleError,
55+
guar: ErrorGuaranteed,
56+
_key_def_id: Option<DefId>,
57+
) -> Self {
4758
// SAFETY: This is never called when `Self` is not `Ty<'tcx>`.
4859
// FIXME: Represent the above fact in the trait system somehow.
4960
unsafe { std::mem::transmute::<Ty<'tcx>, Ty<'_>>(Ty::new_error(tcx, guar)) }
5061
}
5162
}
5263

5364
impl<'tcx> FromCycleError<'tcx> for Result<ty::EarlyBinder<'_, Ty<'_>>, CyclePlaceholder> {
54-
fn from_cycle_error(_tcx: TyCtxt<'tcx>, _: CycleError, guar: ErrorGuaranteed) -> Self {
65+
fn from_cycle_error(
66+
_tcx: TyCtxt<'tcx>,
67+
_: CycleError,
68+
guar: ErrorGuaranteed,
69+
_key_def_id: Option<DefId>,
70+
) -> Self {
5571
Err(CyclePlaceholder(guar))
5672
}
5773
}
5874

5975
impl<'tcx> FromCycleError<'tcx> for ty::Binder<'_, ty::FnSig<'_>> {
60-
fn from_cycle_error(tcx: TyCtxt<'tcx>, cycle_error: CycleError, guar: ErrorGuaranteed) -> Self {
76+
fn from_cycle_error(
77+
tcx: TyCtxt<'tcx>,
78+
_cycle_error: CycleError,
79+
guar: ErrorGuaranteed,
80+
key_def_id: Option<DefId>,
81+
) -> Self {
6182
let err = Ty::new_error(tcx, guar);
6283

63-
let arity = if let Some(info) = cycle_error.cycle.get(0)
64-
&& info.frame.dep_kind == DepKind::fn_sig
65-
&& let Some(def_id) = info.frame.def_id
84+
let arity = if let Some(def_id) = key_def_id
6685
&& let Some(node) = tcx.hir_get_if_local(def_id)
6786
&& let Some(sig) = node.fn_sig()
6887
{
@@ -91,6 +110,7 @@ impl<'tcx> FromCycleError<'tcx> for Representability {
91110
tcx: TyCtxt<'tcx>,
92111
cycle_error: CycleError,
93112
_guar: ErrorGuaranteed,
113+
_key_def_id: Option<DefId>,
94114
) -> Self {
95115
let mut item_and_field_ids = Vec::new();
96116
let mut representable_ids = FxHashSet::default();
@@ -125,14 +145,24 @@ impl<'tcx> FromCycleError<'tcx> for Representability {
125145
}
126146

127147
impl<'tcx> FromCycleError<'tcx> for ty::EarlyBinder<'_, Ty<'_>> {
128-
fn from_cycle_error(tcx: TyCtxt<'tcx>, cycle_error: CycleError, guar: ErrorGuaranteed) -> Self {
129-
ty::EarlyBinder::bind(Ty::from_cycle_error(tcx, cycle_error, guar))
148+
fn from_cycle_error(
149+
tcx: TyCtxt<'tcx>,
150+
cycle_error: CycleError,
151+
guar: ErrorGuaranteed,
152+
key_def_id: Option<DefId>,
153+
) -> Self {
154+
ty::EarlyBinder::bind(Ty::from_cycle_error(tcx, cycle_error, guar, key_def_id))
130155
}
131156
}
132157

133158
impl<'tcx> FromCycleError<'tcx> for ty::EarlyBinder<'_, ty::Binder<'_, ty::FnSig<'_>>> {
134-
fn from_cycle_error(tcx: TyCtxt<'tcx>, cycle_error: CycleError, guar: ErrorGuaranteed) -> Self {
135-
ty::EarlyBinder::bind(ty::Binder::from_cycle_error(tcx, cycle_error, guar))
159+
fn from_cycle_error(
160+
tcx: TyCtxt<'tcx>,
161+
cycle_error: CycleError,
162+
guar: ErrorGuaranteed,
163+
key_def_id: Option<DefId>,
164+
) -> Self {
165+
ty::EarlyBinder::bind(ty::Binder::from_cycle_error(tcx, cycle_error, guar, key_def_id))
136166
}
137167
}
138168

@@ -141,6 +171,7 @@ impl<'tcx> FromCycleError<'tcx> for &[ty::Variance] {
141171
tcx: TyCtxt<'tcx>,
142172
cycle_error: CycleError,
143173
_guar: ErrorGuaranteed,
174+
_key_def_id: Option<DefId>,
144175
) -> Self {
145176
search_for_cycle_permutation(
146177
&cycle_error.cycle,
@@ -189,6 +220,7 @@ impl<'tcx, T> FromCycleError<'tcx> for Result<T, &'_ ty::layout::LayoutError<'_>
189220
tcx: TyCtxt<'tcx>,
190221
cycle_error: CycleError,
191222
_guar: ErrorGuaranteed,
223+
_key_def_id: Option<DefId>,
192224
) -> Self {
193225
let diag = search_for_cycle_permutation(
194226
&cycle_error.cycle,

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,9 +467,9 @@ macro_rules! define_queries {
467467
#[cfg(not($cache_on_disk))]
468468
is_loadable_from_disk_fn: |_tcx, _key, _index| false,
469469

470-
value_from_cycle_error: |tcx, cycle, guar| {
470+
value_from_cycle_error: |tcx, key, cycle, guar| {
471471
let result: queries::$name::Value<'tcx> =
472-
FromCycleError::from_cycle_error(tcx, cycle, guar);
472+
FromCycleError::from_cycle_error(tcx, cycle, guar, rustc_middle::query::QueryKey::key_as_def_id(&key));
473473
erase::erase_val(result)
474474
},
475475

0 commit comments

Comments
 (0)