Skip to content

Commit 34244c1

Browse files
committed
Address comments
1 parent 157008d commit 34244c1

File tree

5 files changed

+33
-21
lines changed

5 files changed

+33
-21
lines changed

compiler/rustc_interface/src/util.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -193,15 +193,16 @@ pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce(CurrentGcx) -> R + Send,
193193
let query_map = current_gcx2.access(|gcx| {
194194
tls::enter_context(&tls::ImplicitCtxt::new(gcx), || {
195195
tls::with(|tcx| {
196-
let (query_map, complete) = QueryCtxt::new(tcx).collect_active_jobs();
197-
if !complete {
198-
// There was an unexpected error collecting all active jobs, which we need
199-
// to find cycles to break.
200-
// We want to avoid panicking in the deadlock handler, so we abort instead.
201-
eprintln!("internal compiler error: failed to get query map in deadlock handler, aborting process");
202-
process::abort();
196+
match QueryCtxt::new(tcx).collect_active_jobs() {
197+
Ok(query_map) => query_map,
198+
Err(_) => {
199+
// There was an unexpected error collecting all active jobs, which we need
200+
// to find cycles to break.
201+
// We want to avoid panicking in the deadlock handler, so we abort instead.
202+
eprintln!("internal compiler error: failed to get query map in deadlock handler, aborting process");
203+
process::abort();
204+
}
203205
}
204-
query_map
205206
})
206207
})
207208
});

compiler/rustc_query_impl/src/plumbing.rs

+16-9
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,20 @@ impl QueryContext for QueryCtxt<'_> {
7979
tls::with_related_context(self.tcx, |icx| icx.query)
8080
}
8181

82-
/// Returns a query map representing active query jobs and a bool being false
83-
/// if there was an error constructing the map.
84-
fn collect_active_jobs(self) -> (QueryMap, bool) {
82+
/// Returns a query map representing active query jobs.
83+
/// It returns an incomplete map as an error if it fails
84+
/// to take locks.
85+
fn collect_active_jobs(self) -> Result<QueryMap, QueryMap> {
8586
let mut jobs = QueryMap::default();
8687
let mut complete = true;
8788

8889
for collect in super::TRY_COLLECT_ACTIVE_JOBS.iter() {
89-
collect(self.tcx, &mut jobs, &mut complete);
90+
if collect(self.tcx, &mut jobs).is_none() {
91+
complete = false;
92+
}
9093
}
9194

92-
(jobs, complete)
95+
if complete { Ok(jobs) } else { Err(jobs) }
9396
}
9497

9598
// Interactions with on_disk_cache
@@ -143,7 +146,11 @@ impl QueryContext for QueryCtxt<'_> {
143146

144147
fn depth_limit_error(self, job: QueryJobId) {
145148
// FIXME: `collect_active_jobs` expects no locks to be held, which doesn't hold for this call.
146-
let (info, depth) = job.find_dep_kind_root(self.collect_active_jobs().0);
149+
let query_map = match self.collect_active_jobs() {
150+
Ok(query_map) => query_map,
151+
Err(query_map) => query_map,
152+
};
153+
let (info, depth) = job.find_dep_kind_root(query_map);
147154

148155
let suggested_limit = match self.recursion_limit() {
149156
Limit(0) => Limit(2),
@@ -681,7 +688,7 @@ macro_rules! define_queries {
681688
}
682689
}
683690

684-
pub(crate) fn try_collect_active_jobs<'tcx>(tcx: TyCtxt<'tcx>, qmap: &mut QueryMap, complete: &mut bool) {
691+
pub(crate) fn try_collect_active_jobs<'tcx>(tcx: TyCtxt<'tcx>, qmap: &mut QueryMap) -> Option<()> {
685692
let make_query = |tcx, key| {
686693
let kind = rustc_middle::dep_graph::dep_kinds::$name;
687694
let name = stringify!($name);
@@ -696,12 +703,12 @@ macro_rules! define_queries {
696703
// don't `unwrap()` here, just manually check for `None` and do best-effort error
697704
// reporting.
698705
if res.is_none() {
699-
*complete = false;
700706
tracing::warn!(
701707
"Failed to collect active jobs for query with name `{}`!",
702708
stringify!($name)
703709
);
704710
}
711+
res
705712
}
706713

707714
pub(crate) fn alloc_self_profile_query_strings<'tcx>(
@@ -761,7 +768,7 @@ macro_rules! define_queries {
761768

762769
// These arrays are used for iteration and can't be indexed by `DepKind`.
763770

764-
const TRY_COLLECT_ACTIVE_JOBS: &[for<'tcx> fn(TyCtxt<'tcx>, &mut QueryMap, &mut bool)] =
771+
const TRY_COLLECT_ACTIVE_JOBS: &[for<'tcx> fn(TyCtxt<'tcx>, &mut QueryMap) -> Option<()>] =
765772
&[$(query_impl::$name::try_collect_active_jobs),*];
766773

767774
const ALLOC_SELF_PROFILE_QUERY_STRINGS: &[

compiler/rustc_query_system/src/query/job.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,12 @@ pub fn print_query_stack<Qcx: QueryContext>(
588588
// state if it was responsible for triggering the panic.
589589
let mut count_printed = 0;
590590
let mut count_total = 0;
591-
let query_map = qcx.collect_active_jobs().0;
591+
592+
// Make use of a partial query map if we fail to take locks collecting active queries.
593+
let query_map = match qcx.collect_active_jobs() {
594+
Ok(query_map) => query_map,
595+
Err(query_map) => query_map,
596+
};
592597

593598
if let Some(ref mut file) = file {
594599
let _ = writeln!(file, "\n\nquery stack during panic:");

compiler/rustc_query_system/src/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ pub trait QueryContext: HasDepContext {
8686
/// Get the query information from the TLS context.
8787
fn current_query_job(self) -> Option<QueryJobId>;
8888

89-
fn collect_active_jobs(self) -> (QueryMap, bool);
89+
fn collect_active_jobs(self) -> Result<QueryMap, QueryMap>;
9090

9191
/// Load a side effect associated to the node in the previous session.
9292
fn load_side_effect(

compiler/rustc_query_system/src/query/plumbing.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,9 @@ where
250250
Q: QueryConfig<Qcx>,
251251
Qcx: QueryContext,
252252
{
253-
let (query_map, complete) = qcx.collect_active_jobs();
254253
// Ensure there was no errors collecting all active jobs.
255254
// We need the complete map to ensure we find a cycle to break.
256-
assert!(complete, "failed to collect active queries");
255+
let query_map = qcx.collect_active_jobs().expect("failed to collect active queries");
257256

258257
let error = try_execute.find_cycle_in_stack(query_map, &qcx.current_query_job(), span);
259258
(mk_cycle(query, qcx, error), None)

0 commit comments

Comments
 (0)