Skip to content

Commit 89ac765

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

4 files changed

Lines changed: 25 additions & 21 deletions

File tree

compiler/rustc_middle/src/query/plumbing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ pub struct QueryVTable<'tcx, C: QueryCache> {
139139
pub hash_value_fn: Option<fn(&mut StableHashingContext<'_>, &C::Value) -> Fingerprint>,
140140

141141
pub value_from_cycle_error:
142-
fn(tcx: TyCtxt<'tcx>, cycle_error: CycleError, guar: ErrorGuaranteed) -> C::Value,
142+
fn(tcx: TyCtxt<'tcx>, key: C::Key, cycle_error: CycleError, guar: ErrorGuaranteed) -> C::Value,
143143
pub format_value: fn(&C::Value) -> String,
144144

145145
/// 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: 14 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,15 @@ 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(tcx: TyCtxt<'tcx>, cycle_error: CycleError, guar: ErrorGuaranteed, key_def_id: Option<DefId>) -> Self;
2626
}
2727

2828
impl<'tcx, T> FromCycleError<'tcx> for T {
2929
default fn from_cycle_error(
3030
tcx: TyCtxt<'tcx>,
3131
cycle_error: CycleError,
3232
_guar: ErrorGuaranteed,
33+
_key_def_id: Option<DefId>,
3334
) -> T {
3435
let Some(guar) = tcx.sess.dcx().has_errors() else {
3536
bug!(
@@ -43,26 +44,24 @@ impl<'tcx, T> FromCycleError<'tcx> for T {
4344
}
4445

4546
impl<'tcx> FromCycleError<'tcx> for Ty<'_> {
46-
fn from_cycle_error(tcx: TyCtxt<'tcx>, _: CycleError, guar: ErrorGuaranteed) -> Self {
47+
fn from_cycle_error(tcx: TyCtxt<'tcx>, _: CycleError, guar: ErrorGuaranteed, _key_def_id: Option<DefId>) -> Self {
4748
// SAFETY: This is never called when `Self` is not `Ty<'tcx>`.
4849
// FIXME: Represent the above fact in the trait system somehow.
4950
unsafe { std::mem::transmute::<Ty<'tcx>, Ty<'_>>(Ty::new_error(tcx, guar)) }
5051
}
5152
}
5253

5354
impl<'tcx> FromCycleError<'tcx> for Result<ty::EarlyBinder<'_, Ty<'_>>, CyclePlaceholder> {
54-
fn from_cycle_error(_tcx: TyCtxt<'tcx>, _: CycleError, guar: ErrorGuaranteed) -> Self {
55+
fn from_cycle_error(_tcx: TyCtxt<'tcx>, _: CycleError, guar: ErrorGuaranteed, _key_def_id: Option<DefId>) -> Self {
5556
Err(CyclePlaceholder(guar))
5657
}
5758
}
5859

5960
impl<'tcx> FromCycleError<'tcx> for ty::Binder<'_, ty::FnSig<'_>> {
60-
fn from_cycle_error(tcx: TyCtxt<'tcx>, cycle_error: CycleError, guar: ErrorGuaranteed) -> Self {
61+
fn from_cycle_error(tcx: TyCtxt<'tcx>, _cycle_error: CycleError, guar: ErrorGuaranteed, key_def_id: Option<DefId>) -> Self {
6162
let err = Ty::new_error(tcx, guar);
6263

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
64+
let arity = if let Some(def_id) = key_def_id
6665
&& let Some(node) = tcx.hir_get_if_local(def_id)
6766
&& let Some(sig) = node.fn_sig()
6867
{
@@ -91,6 +90,7 @@ impl<'tcx> FromCycleError<'tcx> for Representability {
9190
tcx: TyCtxt<'tcx>,
9291
cycle_error: CycleError,
9392
_guar: ErrorGuaranteed,
93+
_key_def_id: Option<DefId>,
9494
) -> Self {
9595
let mut item_and_field_ids = Vec::new();
9696
let mut representable_ids = FxHashSet::default();
@@ -125,14 +125,14 @@ impl<'tcx> FromCycleError<'tcx> for Representability {
125125
}
126126

127127
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))
128+
fn from_cycle_error(tcx: TyCtxt<'tcx>, cycle_error: CycleError, guar: ErrorGuaranteed, key_def_id: Option<DefId>) -> Self {
129+
ty::EarlyBinder::bind(Ty::from_cycle_error(tcx, cycle_error, guar, key_def_id))
130130
}
131131
}
132132

133133
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))
134+
fn from_cycle_error(tcx: TyCtxt<'tcx>, cycle_error: CycleError, guar: ErrorGuaranteed, key_def_id: Option<DefId>) -> Self {
135+
ty::EarlyBinder::bind(ty::Binder::from_cycle_error(tcx, cycle_error, guar, key_def_id))
136136
}
137137
}
138138

@@ -141,6 +141,7 @@ impl<'tcx> FromCycleError<'tcx> for &[ty::Variance] {
141141
tcx: TyCtxt<'tcx>,
142142
cycle_error: CycleError,
143143
_guar: ErrorGuaranteed,
144+
_key_def_id: Option<DefId>,
144145
) -> Self {
145146
search_for_cycle_permutation(
146147
&cycle_error.cycle,
@@ -189,6 +190,7 @@ impl<'tcx, T> FromCycleError<'tcx> for Result<T, &'_ ty::layout::LayoutError<'_>
189190
tcx: TyCtxt<'tcx>,
190191
cycle_error: CycleError,
191192
_guar: ErrorGuaranteed,
193+
_key_def_id: Option<DefId>,
192194
) -> Self {
193195
let diag = search_for_cycle_permutation(
194196
&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)