Skip to content

Commit 755b2da

Browse files
Value recovery can take the whole CycleError
1 parent ca663b0 commit 755b2da

File tree

8 files changed

+54
-31
lines changed

8 files changed

+54
-31
lines changed

compiler/rustc_middle/src/query/plumbing.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub struct DynamicQuery<'tcx, C: QueryCache> {
5353
fn(tcx: TyCtxt<'tcx>, key: &C::Key, index: SerializedDepNodeIndex) -> bool,
5454
pub hash_result: HashResult<C::Value>,
5555
pub value_from_cycle_error:
56-
fn(tcx: TyCtxt<'tcx>, cycle: &[QueryInfo], guar: ErrorGuaranteed) -> C::Value,
56+
fn(tcx: TyCtxt<'tcx>, cycle_error: &CycleError, guar: ErrorGuaranteed) -> C::Value,
5757
pub format_value: fn(&C::Value) -> String,
5858
}
5959

compiler/rustc_middle/src/values.rs

+34-14
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,29 @@ use rustc_hir as hir;
66
use rustc_hir::def::{DefKind, Res};
77
use rustc_middle::ty::Representability;
88
use rustc_middle::ty::{self, Ty, TyCtxt};
9-
use rustc_query_system::query::QueryInfo;
9+
use rustc_query_system::query::CycleError;
1010
use rustc_query_system::Value;
1111
use rustc_span::def_id::LocalDefId;
1212
use rustc_span::{ErrorGuaranteed, Span};
1313

1414
use std::fmt::Write;
1515

1616
impl<'tcx> Value<TyCtxt<'tcx>> for Ty<'_> {
17-
fn from_cycle_error(tcx: TyCtxt<'tcx>, _: &[QueryInfo], guar: ErrorGuaranteed) -> Self {
17+
fn from_cycle_error(tcx: TyCtxt<'tcx>, _: &CycleError, guar: ErrorGuaranteed) -> Self {
1818
// SAFETY: This is never called when `Self` is not `Ty<'tcx>`.
1919
// FIXME: Represent the above fact in the trait system somehow.
2020
unsafe { std::mem::transmute::<Ty<'tcx>, Ty<'_>>(Ty::new_error(tcx, guar)) }
2121
}
2222
}
2323

2424
impl<'tcx> Value<TyCtxt<'tcx>> for Result<ty::EarlyBinder<Ty<'_>>, CyclePlaceholder> {
25-
fn from_cycle_error(_tcx: TyCtxt<'tcx>, _: &[QueryInfo], guar: ErrorGuaranteed) -> Self {
25+
fn from_cycle_error(_tcx: TyCtxt<'tcx>, _: &CycleError, guar: ErrorGuaranteed) -> Self {
2626
Err(CyclePlaceholder(guar))
2727
}
2828
}
2929

3030
impl<'tcx> Value<TyCtxt<'tcx>> for ty::SymbolName<'_> {
31-
fn from_cycle_error(tcx: TyCtxt<'tcx>, _: &[QueryInfo], _guar: ErrorGuaranteed) -> Self {
31+
fn from_cycle_error(tcx: TyCtxt<'tcx>, _: &CycleError, _guar: ErrorGuaranteed) -> Self {
3232
// SAFETY: This is never called when `Self` is not `SymbolName<'tcx>`.
3333
// FIXME: Represent the above fact in the trait system somehow.
3434
unsafe {
@@ -40,10 +40,14 @@ impl<'tcx> Value<TyCtxt<'tcx>> for ty::SymbolName<'_> {
4040
}
4141

4242
impl<'tcx> Value<TyCtxt<'tcx>> for ty::Binder<'_, ty::FnSig<'_>> {
43-
fn from_cycle_error(tcx: TyCtxt<'tcx>, stack: &[QueryInfo], guar: ErrorGuaranteed) -> Self {
43+
fn from_cycle_error(
44+
tcx: TyCtxt<'tcx>,
45+
cycle_error: &CycleError,
46+
guar: ErrorGuaranteed,
47+
) -> Self {
4448
let err = Ty::new_error(tcx, guar);
4549

46-
let arity = if let Some(frame) = stack.get(0)
50+
let arity = if let Some(frame) = cycle_error.cycle.get(0)
4751
&& frame.query.dep_kind == dep_kinds::fn_sig
4852
&& let Some(def_id) = frame.query.def_id
4953
&& let Some(node) = tcx.hir().get_if_local(def_id)
@@ -70,10 +74,14 @@ impl<'tcx> Value<TyCtxt<'tcx>> for ty::Binder<'_, ty::FnSig<'_>> {
7074
}
7175

7276
impl<'tcx> Value<TyCtxt<'tcx>> for Representability {
73-
fn from_cycle_error(tcx: TyCtxt<'tcx>, cycle: &[QueryInfo], _guar: ErrorGuaranteed) -> Self {
77+
fn from_cycle_error(
78+
tcx: TyCtxt<'tcx>,
79+
cycle_error: &CycleError,
80+
_guar: ErrorGuaranteed,
81+
) -> Self {
7482
let mut item_and_field_ids = Vec::new();
7583
let mut representable_ids = FxHashSet::default();
76-
for info in cycle {
84+
for info in &cycle_error.cycle {
7785
if info.query.dep_kind == dep_kinds::representability
7886
&& let Some(field_id) = info.query.def_id
7987
&& let Some(field_id) = field_id.as_local()
@@ -87,7 +95,7 @@ impl<'tcx> Value<TyCtxt<'tcx>> for Representability {
8795
item_and_field_ids.push((item_id.expect_local(), field_id));
8896
}
8997
}
90-
for info in cycle {
98+
for info in &cycle_error.cycle {
9199
if info.query.dep_kind == dep_kinds::representability_adt_ty
92100
&& let Some(def_id) = info.query.ty_adt_id
93101
&& let Some(def_id) = def_id.as_local()
@@ -102,19 +110,31 @@ impl<'tcx> Value<TyCtxt<'tcx>> for Representability {
102110
}
103111

104112
impl<'tcx> Value<TyCtxt<'tcx>> for ty::EarlyBinder<Ty<'_>> {
105-
fn from_cycle_error(tcx: TyCtxt<'tcx>, cycle: &[QueryInfo], guar: ErrorGuaranteed) -> Self {
106-
ty::EarlyBinder::bind(Ty::from_cycle_error(tcx, cycle, guar))
113+
fn from_cycle_error(
114+
tcx: TyCtxt<'tcx>,
115+
cycle_error: &CycleError,
116+
guar: ErrorGuaranteed,
117+
) -> Self {
118+
ty::EarlyBinder::bind(Ty::from_cycle_error(tcx, cycle_error, guar))
107119
}
108120
}
109121

110122
impl<'tcx> Value<TyCtxt<'tcx>> for ty::EarlyBinder<ty::Binder<'_, ty::FnSig<'_>>> {
111-
fn from_cycle_error(tcx: TyCtxt<'tcx>, cycle: &[QueryInfo], guar: ErrorGuaranteed) -> Self {
112-
ty::EarlyBinder::bind(ty::Binder::from_cycle_error(tcx, cycle, guar))
123+
fn from_cycle_error(
124+
tcx: TyCtxt<'tcx>,
125+
cycle_error: &CycleError,
126+
guar: ErrorGuaranteed,
127+
) -> Self {
128+
ty::EarlyBinder::bind(ty::Binder::from_cycle_error(tcx, cycle_error, guar))
113129
}
114130
}
115131

116132
impl<'tcx, T> Value<TyCtxt<'tcx>> for Result<T, &'_ ty::layout::LayoutError<'_>> {
117-
fn from_cycle_error(_tcx: TyCtxt<'tcx>, _cycle: &[QueryInfo], guar: ErrorGuaranteed) -> Self {
133+
fn from_cycle_error(
134+
_tcx: TyCtxt<'tcx>,
135+
_cycle_error: &CycleError,
136+
guar: ErrorGuaranteed,
137+
) -> Self {
118138
// tcx.arena.alloc cannot be used because we are not allowed to use &'tcx LayoutError under
119139
// min_specialization. Since this is an error path anyways, leaking doesn't matter (and really,
120140
// tcx.arena.alloc is pretty much equal to leaking).

compiler/rustc_query_impl/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use rustc_middle::ty::TyCtxt;
3535
use rustc_query_system::dep_graph::SerializedDepNodeIndex;
3636
use rustc_query_system::ich::StableHashingContext;
3737
use rustc_query_system::query::{
38-
get_query_incr, get_query_non_incr, HashResult, QueryCache, QueryConfig, QueryInfo, QueryMap,
38+
get_query_incr, get_query_non_incr, CycleError, HashResult, QueryCache, QueryConfig, QueryMap,
3939
QueryMode, QueryState,
4040
};
4141
use rustc_query_system::HandleCycleError;
@@ -144,10 +144,10 @@ where
144144
fn value_from_cycle_error(
145145
self,
146146
tcx: TyCtxt<'tcx>,
147-
cycle: &[QueryInfo],
147+
cycle_error: &CycleError,
148148
guar: ErrorGuaranteed,
149149
) -> Self::Value {
150-
(self.dynamic.value_from_cycle_error)(tcx, cycle, guar)
150+
(self.dynamic.value_from_cycle_error)(tcx, cycle_error, guar)
151151
}
152152

153153
#[inline(always)]

compiler/rustc_query_system/src/query/config.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::error::HandleCycleError;
55
use crate::ich::StableHashingContext;
66
use crate::query::caches::QueryCache;
77
use crate::query::DepNodeIndex;
8-
use crate::query::{QueryContext, QueryInfo, QueryState};
8+
use crate::query::{CycleError, QueryContext, QueryState};
99

1010
use rustc_data_structures::fingerprint::Fingerprint;
1111
use rustc_span::ErrorGuaranteed;
@@ -57,7 +57,7 @@ pub trait QueryConfig<Qcx: QueryContext>: Copy {
5757
fn value_from_cycle_error(
5858
self,
5959
tcx: Qcx::DepContext,
60-
cycle: &[QueryInfo],
60+
cycle_error: &CycleError,
6161
guar: ErrorGuaranteed,
6262
) -> Self::Value;
6363

compiler/rustc_query_system/src/query/job.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ pub fn deadlock(query_map: QueryMap, registry: &rayon_core::Registry) {
556556

557557
#[inline(never)]
558558
#[cold]
559-
pub(crate) fn report_cycle<'a>(
559+
pub fn report_cycle<'a>(
560560
sess: &'a Session,
561561
CycleError { usage, cycle: stack }: &CycleError,
562562
) -> DiagnosticBuilder<'a> {

compiler/rustc_query_system/src/query/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ pub use self::plumbing::*;
44
mod job;
55
#[cfg(parallel_compiler)]
66
pub use self::job::deadlock;
7-
pub use self::job::{print_query_stack, QueryInfo, QueryJob, QueryJobId, QueryJobInfo, QueryMap};
7+
pub use self::job::{
8+
print_query_stack, report_cycle, QueryInfo, QueryJob, QueryJobId, QueryJobInfo, QueryMap,
9+
};
810

911
mod caches;
1012
pub use self::caches::{

compiler/rustc_query_system/src/query/plumbing.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ where
134134
match query.handle_cycle_error() {
135135
Error => {
136136
let guar = error.emit();
137-
query.value_from_cycle_error(*qcx.dep_context(), &cycle_error.cycle, guar)
137+
query.value_from_cycle_error(*qcx.dep_context(), cycle_error, guar)
138138
}
139139
Fatal => {
140140
error.emit();
@@ -143,7 +143,7 @@ where
143143
}
144144
DelayBug => {
145145
let guar = error.delay_as_bug();
146-
query.value_from_cycle_error(*qcx.dep_context(), &cycle_error.cycle, guar)
146+
query.value_from_cycle_error(*qcx.dep_context(), cycle_error, guar)
147147
}
148148
Stash => {
149149
let guar = if let Some(root) = cycle_error.cycle.first()
@@ -154,7 +154,7 @@ where
154154
} else {
155155
error.emit()
156156
};
157-
query.value_from_cycle_error(*qcx.dep_context(), &cycle_error.cycle, guar)
157+
query.value_from_cycle_error(*qcx.dep_context(), cycle_error, guar)
158158
}
159159
}
160160
}
@@ -211,7 +211,7 @@ where
211211
}
212212

213213
#[derive(Clone, Debug)]
214-
pub(crate) struct CycleError {
214+
pub struct CycleError {
215215
/// The query and related span that uses the cycle.
216216
pub usage: Option<(Span, QueryStackFrame)>,
217217
pub cycle: Vec<QueryInfo>,
+6-5
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
use rustc_span::ErrorGuaranteed;
22

33
use crate::dep_graph::DepContext;
4-
use crate::query::QueryInfo;
4+
use crate::query::CycleError;
55

66
pub trait Value<Tcx: DepContext>: Sized {
7-
fn from_cycle_error(tcx: Tcx, cycle: &[QueryInfo], guar: ErrorGuaranteed) -> Self;
7+
fn from_cycle_error(tcx: Tcx, cycle_error: &CycleError, guar: ErrorGuaranteed) -> Self;
88
}
99

1010
impl<Tcx: DepContext, T> Value<Tcx> for T {
11-
default fn from_cycle_error(tcx: Tcx, cycle: &[QueryInfo], _guar: ErrorGuaranteed) -> T {
11+
default fn from_cycle_error(tcx: Tcx, cycle_error: &CycleError, _guar: ErrorGuaranteed) -> T {
1212
tcx.sess().dcx().abort_if_errors();
1313
// Ideally we would use `bug!` here. But bug! is only defined in rustc_middle, and it's
1414
// non-trivial to define it earlier.
1515
panic!(
16-
"<{} as Value>::from_cycle_error called without errors: {cycle:#?}",
17-
std::any::type_name::<T>()
16+
"<{} as Value>::from_cycle_error called without errors: {:#?}",
17+
std::any::type_name::<T>(),
18+
cycle_error.cycle,
1819
);
1920
}
2021
}

0 commit comments

Comments
 (0)