Skip to content

Commit c26d965

Browse files
committed
Move report_cycle to rustc_query_system.
The call to `ty::print::with_forced_impl_filename_line` is done when constructing the description, at the construction of the QueryStackFrame.
1 parent 3897395 commit c26d965

File tree

5 files changed

+54
-64
lines changed

5 files changed

+54
-64
lines changed

compiler/rustc_query_impl/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ extern crate tracing;
1818

1919
use rustc_data_structures::fingerprint::Fingerprint;
2020
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
21-
use rustc_errors::{Diagnostic, Handler, Level};
21+
use rustc_errors::{Diagnostic, DiagnosticBuilder, Handler, Level};
2222
use rustc_hir::def_id::CrateNum;
2323
use rustc_index::vec::IndexVec;
2424
use rustc_middle::dep_graph;

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 6 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@ use rustc_middle::ty::query::on_disk_cache;
88
use rustc_middle::ty::tls::{self, ImplicitCtxt};
99
use rustc_middle::ty::{self, TyCtxt};
1010
use rustc_query_system::dep_graph::HasDepContext;
11-
use rustc_query_system::query::{CycleError, QueryJobId};
12-
use rustc_query_system::query::{QueryContext, QueryDescription, QueryMap, QueryStackFrame};
11+
use rustc_query_system::query::{QueryContext, QueryDescription, QueryJobId, QueryMap};
1312

1413
use rustc_data_structures::sync::Lock;
1514
use rustc_data_structures::thin_vec::ThinVec;
16-
use rustc_errors::{struct_span_err, Diagnostic, DiagnosticBuilder};
15+
use rustc_errors::Diagnostic;
1716
use rustc_serialize::opaque;
1817
use rustc_span::def_id::{DefId, LocalDefId};
19-
use rustc_span::Span;
2018

2119
#[derive(Copy, Clone)]
2220
pub struct QueryCtxt<'tcx> {
@@ -175,54 +173,6 @@ impl QueryContext for QueryCtxt<'tcx> {
175173
}
176174

177175
impl<'tcx> QueryCtxt<'tcx> {
178-
#[inline(never)]
179-
#[cold]
180-
pub(super) fn report_cycle(
181-
self,
182-
CycleError { usage, cycle: stack }: CycleError,
183-
) -> DiagnosticBuilder<'tcx> {
184-
assert!(!stack.is_empty());
185-
186-
let fix_span = |span: Span, query: &QueryStackFrame| {
187-
self.sess.source_map().guess_head_span(query.default_span(span))
188-
};
189-
190-
// Disable naming impls with types in this path, since that
191-
// sometimes cycles itself, leading to extra cycle errors.
192-
// (And cycle errors around impls tend to occur during the
193-
// collect/coherence phases anyhow.)
194-
ty::print::with_forced_impl_filename_line(|| {
195-
let span = fix_span(stack[1 % stack.len()].span, &stack[0].query);
196-
let mut err = struct_span_err!(
197-
self.sess,
198-
span,
199-
E0391,
200-
"cycle detected when {}",
201-
stack[0].query.description
202-
);
203-
204-
for i in 1..stack.len() {
205-
let query = &stack[i].query;
206-
let span = fix_span(stack[(i + 1) % stack.len()].span, query);
207-
err.span_note(span, &format!("...which requires {}...", query.description));
208-
}
209-
210-
err.note(&format!(
211-
"...which again requires {}, completing the cycle",
212-
stack[0].query.description
213-
));
214-
215-
if let Some((span, query)) = usage {
216-
err.span_note(
217-
fix_span(span, &query),
218-
&format!("cycle used when {}", query.description),
219-
);
220-
}
221-
222-
err
223-
})
224-
}
225-
226176
pub(super) fn encode_query_results(
227177
self,
228178
encoder: &mut on_disk_cache::CacheEncoder<'a, 'tcx, opaque::FileEncoder>,
@@ -302,16 +252,16 @@ pub struct QueryStruct {
302252

303253
macro_rules! handle_cycle_error {
304254
([][$tcx: expr, $error:expr]) => {{
305-
$tcx.report_cycle($error).emit();
255+
$error.emit();
306256
Value::from_cycle_error($tcx)
307257
}};
308258
([fatal_cycle $($rest:tt)*][$tcx:expr, $error:expr]) => {{
309-
$tcx.report_cycle($error).emit();
259+
$error.emit();
310260
$tcx.sess.abort_if_errors();
311261
unreachable!()
312262
}};
313263
([cycle_delay_bug $($rest:tt)*][$tcx:expr, $error:expr]) => {{
314-
$tcx.report_cycle($error).delay_as_bug();
264+
$error.delay_as_bug();
315265
Value::from_cycle_error($tcx)
316266
}};
317267
([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*][$($args:tt)*]) => {
@@ -459,7 +409,7 @@ macro_rules! define_queries {
459409

460410
fn handle_cycle_error(
461411
tcx: QueryCtxt<'tcx>,
462-
error: CycleError,
412+
mut error: DiagnosticBuilder<'_>,
463413
) -> Self::Value {
464414
handle_cycle_error!([$($modifiers)*][tcx, error])
465415
}

compiler/rustc_query_system/src/query/config.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
use crate::dep_graph::DepNode;
44
use crate::dep_graph::SerializedDepNodeIndex;
55
use crate::query::caches::QueryCache;
6-
use crate::query::plumbing::CycleError;
76
use crate::query::{QueryCacheStore, QueryContext, QueryState};
87

98
use rustc_data_structures::fingerprint::Fingerprint;
9+
use rustc_errors::DiagnosticBuilder;
1010
use std::fmt::Debug;
1111
use std::hash::Hash;
1212

@@ -27,7 +27,7 @@ pub(crate) struct QueryVtable<CTX: QueryContext, K, V> {
2727
pub compute: fn(CTX, K) -> V,
2828

2929
pub hash_result: fn(&mut CTX::StableHashingContext, &V) -> Option<Fingerprint>,
30-
pub handle_cycle_error: fn(CTX, CycleError) -> V,
30+
pub handle_cycle_error: fn(CTX, DiagnosticBuilder<'_>) -> V,
3131
pub cache_on_disk: fn(CTX, &K, Option<&V>) -> bool,
3232
pub try_load_from_disk: fn(CTX, SerializedDepNodeIndex) -> Option<V>,
3333
}
@@ -52,8 +52,8 @@ impl<CTX: QueryContext, K, V> QueryVtable<CTX, K, V> {
5252
(self.hash_result)(hcx, value)
5353
}
5454

55-
pub(crate) fn handle_cycle_error(&self, tcx: CTX, error: CycleError) -> V {
56-
(self.handle_cycle_error)(tcx, error)
55+
pub(crate) fn handle_cycle_error(&self, tcx: CTX, diag: DiagnosticBuilder<'_>) -> V {
56+
(self.handle_cycle_error)(tcx, diag)
5757
}
5858

5959
pub(crate) fn cache_on_disk(&self, tcx: CTX, key: &K, value: Option<&V>) -> bool {
@@ -90,7 +90,7 @@ pub trait QueryAccessors<CTX: QueryContext>: QueryConfig {
9090
result: &Self::Value,
9191
) -> Option<Fingerprint>;
9292

93-
fn handle_cycle_error(tcx: CTX, error: CycleError) -> Self::Value;
93+
fn handle_cycle_error(tcx: CTX, diag: DiagnosticBuilder<'_>) -> Self::Value;
9494
}
9595

9696
pub trait QueryDescription<CTX: QueryContext>: QueryAccessors<CTX> {

compiler/rustc_query_system/src/query/job.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use crate::query::plumbing::CycleError;
22
use crate::query::QueryStackFrame;
33

44
use rustc_data_structures::fx::FxHashMap;
5+
use rustc_errors::{struct_span_err, DiagnosticBuilder};
6+
use rustc_session::Session;
57
use rustc_span::Span;
68

79
use std::convert::TryFrom;
@@ -590,3 +592,37 @@ pub fn deadlock<CTX: QueryContext>(tcx: CTX, registry: &rayon_core::Registry) {
590592

591593
on_panic.disable();
592594
}
595+
596+
#[inline(never)]
597+
#[cold]
598+
pub(crate) fn report_cycle<'a>(
599+
sess: &'a Session,
600+
CycleError { usage, cycle: stack }: CycleError,
601+
) -> DiagnosticBuilder<'a> {
602+
assert!(!stack.is_empty());
603+
604+
let fix_span = |span: Span, query: &QueryStackFrame| {
605+
sess.source_map().guess_head_span(query.default_span(span))
606+
};
607+
608+
let span = fix_span(stack[1 % stack.len()].span, &stack[0].query);
609+
let mut err =
610+
struct_span_err!(sess, span, E0391, "cycle detected when {}", stack[0].query.description);
611+
612+
for i in 1..stack.len() {
613+
let query = &stack[i].query;
614+
let span = fix_span(stack[(i + 1) % stack.len()].span, query);
615+
err.span_note(span, &format!("...which requires {}...", query.description));
616+
}
617+
618+
err.note(&format!(
619+
"...which again requires {}, completing the cycle",
620+
stack[0].query.description
621+
));
622+
623+
if let Some((span, query)) = usage {
624+
err.span_note(fix_span(span, &query), &format!("cycle used when {}", query.description));
625+
}
626+
627+
err
628+
}

compiler/rustc_query_system/src/query/plumbing.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ use crate::dep_graph::{DepContext, DepKind, DepNode};
66
use crate::dep_graph::{DepNodeIndex, SerializedDepNodeIndex};
77
use crate::query::caches::QueryCache;
88
use crate::query::config::{QueryDescription, QueryVtable, QueryVtableExt};
9-
use crate::query::job::{QueryInfo, QueryJob, QueryJobId, QueryJobInfo, QueryShardJobId};
9+
use crate::query::job::{
10+
report_cycle, QueryInfo, QueryJob, QueryJobId, QueryJobInfo, QueryShardJobId,
11+
};
1012
use crate::query::{QueryContext, QueryMap, QueryStackFrame};
1113

1214
#[cfg(not(parallel_compiler))]
@@ -245,6 +247,7 @@ where
245247
&tcx.current_query_job(),
246248
span,
247249
);
250+
let error = report_cycle(tcx.dep_context().sess(), error);
248251
let value = query.handle_cycle_error(tcx, error);
249252
cache.cache.store_nocache(value)
250253
}));
@@ -256,6 +259,7 @@ where
256259
let result = latch.wait_on(tcx.current_query_job(), span);
257260

258261
if let Err(cycle) = result {
262+
let cycle = report_cycle(tcx.dep_context().sess(), cycle);
259263
let value = query.handle_cycle_error(tcx, cycle);
260264
let value = cache.cache.store_nocache(value);
261265
return TryGetJob::Cycle(value);
@@ -352,7 +356,7 @@ where
352356
}
353357

354358
#[derive(Clone)]
355-
pub struct CycleError {
359+
pub(crate) struct CycleError {
356360
/// The query and related span that uses the cycle.
357361
pub usage: Option<(Span, QueryStackFrame)>,
358362
pub cycle: Vec<QueryInfo>,

0 commit comments

Comments
 (0)