Skip to content

Commit ecf0b12

Browse files
committed
Auto merge of #153694 - TKanX:bugfix/153391-cycle-error-key-param, r=nnethercote
fix(query): Pass Query Key to `value_from_cycle_error` ### Summary: Pass the query key directly to `value_from_cycle_error` so that `FromCycleError` impls (notably `FnSig`) can use the recovered query's `DefId` instead of relying on `cycle[0]`, which is arbitrarily rotated by the parallel deadlock handler. As suggested in [#153644 (comment)](#153644 (comment)). Closes #153391 r? @nnethercote cc @zetanumbers
2 parents d1ee5e5 + 3464048 commit ecf0b12

File tree

6 files changed

+74
-22
lines changed

6 files changed

+74
-22
lines changed

compiler/rustc_middle/src/query/plumbing.rs

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

139-
pub value_from_cycle_error:
140-
fn(tcx: TyCtxt<'tcx>, cycle_error: CycleError, guar: ErrorGuaranteed) -> C::Value,
139+
pub value_from_cycle_error: fn(
140+
tcx: TyCtxt<'tcx>,
141+
key: C::Key,
142+
cycle_error: CycleError,
143+
guar: ErrorGuaranteed,
144+
) -> C::Value,
141145
pub format_value: fn(&C::Value) -> String,
142146

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

compiler/rustc_query_impl/src/execution.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,17 +125,18 @@ where
125125
fn mk_cycle<'tcx, C: QueryCache>(
126126
query: &'tcx QueryVTable<'tcx, C>,
127127
tcx: TyCtxt<'tcx>,
128+
key: C::Key,
128129
cycle_error: CycleError,
129130
) -> C::Value {
130131
let error = report_cycle(tcx.sess, &cycle_error);
131132
match query.cycle_error_handling {
132133
CycleErrorHandling::Error => {
133134
let guar = error.emit();
134-
(query.value_from_cycle_error)(tcx, cycle_error, guar)
135+
(query.value_from_cycle_error)(tcx, key, cycle_error, guar)
135136
}
136137
CycleErrorHandling::DelayBug => {
137138
let guar = error.delay_as_bug();
138-
(query.value_from_cycle_error)(tcx, cycle_error, guar)
139+
(query.value_from_cycle_error)(tcx, key, cycle_error, guar)
139140
}
140141
}
141142
}
@@ -219,6 +220,7 @@ where
219220
fn cycle_error<'tcx, C: QueryCache>(
220221
query: &'tcx QueryVTable<'tcx, C>,
221222
tcx: TyCtxt<'tcx>,
223+
key: C::Key,
222224
try_execute: QueryJobId,
223225
span: Span,
224226
) -> (C::Value, Option<DepNodeIndex>) {
@@ -229,7 +231,7 @@ fn cycle_error<'tcx, C: QueryCache>(
229231
.expect("failed to collect active queries");
230232

231233
let error = find_cycle_in_stack(try_execute, job_map, &current_query_job(), span);
232-
(mk_cycle(query, tcx, error.lift()), None)
234+
(mk_cycle(query, tcx, key, error.lift()), None)
233235
}
234236

235237
#[inline(always)]
@@ -274,7 +276,7 @@ fn wait_for_query<'tcx, C: QueryCache>(
274276

275277
(v, Some(index))
276278
}
277-
Err(cycle) => (mk_cycle(query, tcx, cycle.lift()), None),
279+
Err(cycle) => (mk_cycle(query, tcx, key, cycle.lift()), None),
278280
}
279281
}
280282

@@ -337,7 +339,7 @@ fn try_execute_query<'tcx, C: QueryCache, const INCR: bool>(
337339

338340
// If we are single-threaded we know that we have cycle error,
339341
// so we just return the error.
340-
cycle_error(query, tcx, id, span)
342+
cycle_error(query, tcx, key, id, span)
341343
}
342344
}
343345
ActiveKeyStatus::Poisoned => FatalError.raise(),

compiler/rustc_query_impl/src/from_cycle_error.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,34 @@ use rustc_middle::query::erase::erase_val;
1515
use rustc_middle::ty::layout::{LayoutError, TyAndLayout};
1616
use rustc_middle::ty::{self, Ty, TyCtxt};
1717
use rustc_middle::{bug, span_bug};
18-
use rustc_span::def_id::LocalDefId;
18+
use rustc_span::def_id::{DefId, LocalDefId};
1919
use rustc_span::{ErrorGuaranteed, Span};
2020

2121
use crate::job::report_cycle;
2222

2323
pub(crate) fn specialize_query_vtables<'tcx>(vtables: &mut QueryVTables<'tcx>) {
2424
vtables.type_of.value_from_cycle_error =
25-
|tcx, _, guar| erase_val(ty::EarlyBinder::bind(Ty::new_error(tcx, guar)));
25+
|tcx, _, _, guar| erase_val(ty::EarlyBinder::bind(Ty::new_error(tcx, guar)));
2626

2727
vtables.type_of_opaque_hir_typeck.value_from_cycle_error =
28-
|tcx, _, guar| erase_val(ty::EarlyBinder::bind(Ty::new_error(tcx, guar)));
28+
|tcx, _, _, guar| erase_val(ty::EarlyBinder::bind(Ty::new_error(tcx, guar)));
2929

3030
vtables.erase_and_anonymize_regions_ty.value_from_cycle_error =
31-
|tcx, _, guar| erase_val(Ty::new_error(tcx, guar));
31+
|tcx, _, _, guar| erase_val(Ty::new_error(tcx, guar));
3232

33-
vtables.fn_sig.value_from_cycle_error = |tcx, cycle, guar| erase_val(fn_sig(tcx, cycle, guar));
33+
vtables.fn_sig.value_from_cycle_error = |tcx, key, _, guar| erase_val(fn_sig(tcx, key, guar));
3434

3535
vtables.check_representability.value_from_cycle_error =
36-
|tcx, cycle, guar| check_representability(tcx, cycle, guar);
36+
|tcx, _, cycle, guar| check_representability(tcx, cycle, guar);
3737

3838
vtables.check_representability_adt_ty.value_from_cycle_error =
39-
|tcx, cycle, guar| check_representability(tcx, cycle, guar);
39+
|tcx, _, cycle, guar| check_representability(tcx, cycle, guar);
4040

4141
vtables.variances_of.value_from_cycle_error =
42-
|tcx, cycle, guar| erase_val(variances_of(tcx, cycle, guar));
42+
|tcx, _, cycle, guar| erase_val(variances_of(tcx, cycle, guar));
4343

4444
vtables.layout_of.value_from_cycle_error =
45-
|tcx, cycle, guar| erase_val(layout_of(tcx, cycle, guar));
45+
|tcx, _, cycle, guar| erase_val(layout_of(tcx, cycle, guar));
4646
}
4747

4848
pub(crate) fn default<'tcx>(tcx: TyCtxt<'tcx>, cycle_error: CycleError, query_name: &str) -> ! {
@@ -57,15 +57,12 @@ pub(crate) fn default<'tcx>(tcx: TyCtxt<'tcx>, cycle_error: CycleError, query_na
5757

5858
fn fn_sig<'tcx>(
5959
tcx: TyCtxt<'tcx>,
60-
cycle_error: CycleError,
60+
def_id: DefId,
6161
guar: ErrorGuaranteed,
6262
) -> ty::EarlyBinder<'tcx, ty::PolyFnSig<'tcx>> {
6363
let err = Ty::new_error(tcx, guar);
6464

65-
let arity = if let Some(info) = cycle_error.cycle.get(0)
66-
&& info.frame.dep_kind == DepKind::fn_sig
67-
&& let Some(def_id) = info.frame.def_id
68-
&& let Some(node) = tcx.hir_get_if_local(def_id)
65+
let arity = if let Some(node) = tcx.hir_get_if_local(def_id)
6966
&& let Some(sig) = node.fn_sig()
7067
{
7168
sig.decl.inputs.len()

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ macro_rules! define_queries {
487487
#[cfg(not($cache_on_disk))]
488488
is_loadable_from_disk_fn: |_tcx, _key, _index| false,
489489

490-
value_from_cycle_error: |tcx, cycle, _| {
490+
value_from_cycle_error: |tcx, _, cycle, _| {
491491
$crate::from_cycle_error::default(tcx, cycle, stringify!($name))
492492
},
493493

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Regression test for #153391.
2+
//
3+
//@ edition:2024
4+
//@ compile-flags: -Z threads=16
5+
//@ compare-output-by-lines
6+
//@ ignore-test (#142063)
7+
8+
trait A {
9+
fn g() -> B;
10+
//~^ ERROR expected a type, found a trait
11+
}
12+
13+
trait B {
14+
fn bar(&self, x: &A);
15+
//~^ ERROR expected a type, found a trait
16+
}
17+
18+
fn main() {}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
error[E0782]: expected a type, found a trait
2+
--> $DIR/fn-sig-cycle-ice-153391.rs:8:15
3+
|
4+
LL | fn g() -> B;
5+
| ^
6+
|
7+
help: `B` is dyn-incompatible, use `impl B` to return an opaque type, as long as you return a single underlying type
8+
|
9+
LL | fn g() -> impl B;
10+
| ++++
11+
12+
error[E0782]: expected a type, found a trait
13+
--> $DIR/fn-sig-cycle-ice-153391.rs:13:23
14+
|
15+
LL | fn bar(&self, x: &A);
16+
| ^
17+
|
18+
= note: `A` is dyn-incompatible, otherwise a trait object could be used
19+
help: use a new generic type parameter, constrained by `A`
20+
|
21+
LL - fn bar(&self, x: &A);
22+
LL + fn bar<T: A>(&self, x: &T);
23+
|
24+
help: you can also use an opaque type, but users won't be able to specify the type parameter when calling the `fn`, having to rely exclusively on type inference
25+
|
26+
LL | fn bar(&self, x: &impl A);
27+
| ++++
28+
29+
error: aborting due to 2 previous errors
30+
31+
For more information about this error, try `rustc --explain E0782`.

0 commit comments

Comments
 (0)