From a87de890fd1a797952ab49459aeca4ce598d8f15 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 29 Nov 2020 00:39:34 +0100 Subject: [PATCH] Move print_query_stack to rustc_query_system. --- compiler/rustc_query_impl/src/lib.rs | 2 +- compiler/rustc_query_impl/src/plumbing.rs | 34 +-------------- compiler/rustc_query_system/src/query/job.rs | 46 ++++++++++++++++++-- compiler/rustc_query_system/src/query/mod.rs | 2 +- 4 files changed, 46 insertions(+), 38 deletions(-) diff --git a/compiler/rustc_query_impl/src/lib.rs b/compiler/rustc_query_impl/src/lib.rs index 176c68cf0fc..e9314797fbd 100644 --- a/compiler/rustc_query_impl/src/lib.rs +++ b/compiler/rustc_query_impl/src/lib.rs @@ -18,7 +18,7 @@ extern crate tracing; use rustc_data_structures::fingerprint::Fingerprint; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; -use rustc_errors::{Diagnostic, DiagnosticBuilder, Handler, Level}; +use rustc_errors::{DiagnosticBuilder, Handler}; use rustc_hir::def_id::CrateNum; use rustc_index::vec::IndexVec; use rustc_middle::dep_graph; diff --git a/compiler/rustc_query_impl/src/plumbing.rs b/compiler/rustc_query_impl/src/plumbing.rs index 00392fbcf56..4618671ddf0 100644 --- a/compiler/rustc_query_impl/src/plumbing.rs +++ b/compiler/rustc_query_impl/src/plumbing.rs @@ -584,38 +584,8 @@ macro_rules! define_queries_struct { handler: &Handler, num_frames: Option, ) -> usize { - let query_map = self.try_collect_active_jobs(tcx); - - let mut current_query = query; - let mut i = 0; - - while let Some(query) = current_query { - if Some(i) == num_frames { - break; - } - let query_info = if let Some(info) = query_map.as_ref().and_then(|map| map.get(&query)) - { - info - } else { - break; - }; - let mut diag = Diagnostic::new( - Level::FailureNote, - &format!( - "#{} [{}] {}", - i, - query_info.info.query.name, - query_info.info.query.description, - ), - ); - diag.span = tcx.sess.source_map().guess_head_span(query_info.info.span).into(); - handler.force_print_diagnostic(diag); - - current_query = query_info.job.parent; - i += 1; - } - - i + let qcx = QueryCtxt { tcx, queries: self }; + rustc_query_system::query::print_query_stack(qcx, query, handler, num_frames) } $($(#[$attr])* diff --git a/compiler/rustc_query_system/src/query/job.rs b/compiler/rustc_query_system/src/query/job.rs index a30cd078d6c..30dce1e5018 100644 --- a/compiler/rustc_query_system/src/query/job.rs +++ b/compiler/rustc_query_system/src/query/job.rs @@ -1,8 +1,9 @@ +use crate::dep_graph::DepContext; use crate::query::plumbing::CycleError; -use crate::query::QueryStackFrame; +use crate::query::{QueryContext, QueryStackFrame}; use rustc_data_structures::fx::FxHashMap; -use rustc_errors::{struct_span_err, DiagnosticBuilder}; +use rustc_errors::{struct_span_err, Diagnostic, DiagnosticBuilder, Handler, Level}; use rustc_session::Session; use rustc_span::Span; @@ -13,8 +14,6 @@ use std::num::NonZeroU32; #[cfg(parallel_compiler)] use { - crate::dep_graph::DepContext, - crate::query::QueryContext, parking_lot::{Condvar, Mutex}, rustc_data_structures::fx::FxHashSet, rustc_data_structures::stable_hasher::{HashStable, StableHasher}, @@ -626,3 +625,42 @@ pub(crate) fn report_cycle<'a>( err } + +pub fn print_query_stack( + tcx: CTX, + mut current_query: Option>, + handler: &Handler, + num_frames: Option, +) -> usize { + // Be careful relying on global state here: this code is called from + // a panic hook, which means that the global `Handler` may be in a weird + // state if it was responsible for triggering the panic. + let mut i = 0; + let query_map = tcx.try_collect_active_jobs(); + + while let Some(query) = current_query { + if Some(i) == num_frames { + break; + } + let query_info = if let Some(info) = query_map.as_ref().and_then(|map| map.get(&query)) { + info + } else { + break; + }; + let mut diag = Diagnostic::new( + Level::FailureNote, + &format!( + "#{} [{}] {}", + i, query_info.info.query.name, query_info.info.query.description + ), + ); + diag.span = + tcx.dep_context().sess().source_map().guess_head_span(query_info.info.span).into(); + handler.force_print_diagnostic(diag); + + current_query = query_info.job.parent; + i += 1; + } + + i +} diff --git a/compiler/rustc_query_system/src/query/mod.rs b/compiler/rustc_query_system/src/query/mod.rs index 7b46674c743..81e2b4877fd 100644 --- a/compiler/rustc_query_system/src/query/mod.rs +++ b/compiler/rustc_query_system/src/query/mod.rs @@ -4,7 +4,7 @@ pub use self::plumbing::*; mod job; #[cfg(parallel_compiler)] pub use self::job::deadlock; -pub use self::job::{QueryInfo, QueryJob, QueryJobId, QueryJobInfo, QueryMap}; +pub use self::job::{print_query_stack, QueryInfo, QueryJob, QueryJobId, QueryJobInfo, QueryMap}; mod caches; pub use self::caches::{