Skip to content

Commit fed1612

Browse files
committed
Move diagnostics out from QueryJob
1 parent 14d0f37 commit fed1612

File tree

3 files changed

+42
-30
lines changed

3 files changed

+42
-30
lines changed

src/librustc/ty/context.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -1623,7 +1623,8 @@ impl<'gcx: 'tcx, 'tcx> GlobalCtxt<'gcx> {
16231623
ty::tls::with_related_context(tcx.global_tcx(), |icx| {
16241624
let new_icx = ty::tls::ImplicitCtxt {
16251625
tcx,
1626-
query: icx.query.clone(),
1626+
query: icx.query,
1627+
diagnostics: icx.diagnostics,
16271628
layout_depth: icx.layout_depth,
16281629
task: icx.task,
16291630
};
@@ -1896,6 +1897,10 @@ pub mod tls {
18961897
/// ty::query::plumbing when executing a query
18971898
pub query: Option<LrcRef<'a, query::QueryJob<'gcx>>>,
18981899

1900+
/// Where to store diagnostics for the current query job, if any.
1901+
/// This is updated by start_job in ty::query::plumbing when executing a query
1902+
pub diagnostics: Option<&'a Lock<Option<Box<Vec<Diagnostic>>>>>,
1903+
18991904
/// Used to prevent layout from recursing too deeply.
19001905
pub layout_depth: usize,
19011906

@@ -1971,8 +1976,8 @@ pub mod tls {
19711976
fn track_diagnostic(diagnostic: &Diagnostic) {
19721977
with_context_opt(|icx| {
19731978
if let Some(icx) = icx {
1974-
if let Some(ref query) = icx.query {
1975-
let mut diagnostics = query.diagnostics.lock();
1979+
if let Some(ref diagnostics) = icx.diagnostics {
1980+
let mut diagnostics = diagnostics.lock();
19761981
if diagnostics.is_none() {
19771982
*diagnostics = Some(Box::new(Vec::new()));
19781983
}
@@ -2042,6 +2047,7 @@ pub mod tls {
20422047
let icx = ImplicitCtxt {
20432048
tcx,
20442049
query: None,
2050+
diagnostics: None,
20452051
layout_depth: 0,
20462052
task: &OpenTask::Ignore,
20472053
};
@@ -2070,6 +2076,7 @@ pub mod tls {
20702076
};
20712077
let icx = ImplicitCtxt {
20722078
query: None,
2079+
diagnostics: None,
20732080
tcx,
20742081
layout_depth: 0,
20752082
task: &OpenTask::Ignore,

src/librustc/ty/query/job.rs

-13
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ use ty::query::{
2424
config::QueryDescription,
2525
};
2626
use ty::context::TyCtxt;
27-
use errors::Diagnostic;
2827
use std::process;
2928
use std::{fmt, ptr};
3029

@@ -55,9 +54,6 @@ pub struct QueryJob<'tcx> {
5554
/// The parent query job which created this job and is implicitly waiting on it.
5655
pub parent: Option<Lrc<QueryJob<'tcx>>>,
5756

58-
/// Diagnostic messages which are emitted while the query executes
59-
pub diagnostics: Lock<Option<Box<Vec<Diagnostic>>>>,
60-
6157
/// The latch which is used to wait on this job
6258
#[cfg(parallel_queries)]
6359
latch: QueryLatch<'tcx>,
@@ -67,22 +63,13 @@ impl<'tcx> QueryJob<'tcx> {
6763
/// Creates a new query job
6864
pub fn new(info: QueryInfo<'tcx>, parent: Option<Lrc<QueryJob<'tcx>>>) -> Self {
6965
QueryJob {
70-
diagnostics: Lock::new(None),
7166
info,
7267
parent,
7368
#[cfg(parallel_queries)]
7469
latch: QueryLatch::new(),
7570
}
7671
}
7772

78-
#[inline(always)]
79-
pub fn extract_diagnostics(&self) -> Option<Box<Vec<Diagnostic>>> {
80-
// FIXME: Find a way to remove this lock access since we should have
81-
// ownership of the content back now. Other crates may free the Lrc though
82-
// and the, but only after we replace this.
83-
mem::replace(&mut *self.diagnostics.lock(), None)
84-
}
85-
8673
/// Awaits for the query job to complete.
8774
///
8875
/// For single threaded rustc there's no concurrent jobs running, so if we are waiting for any

src/librustc/ty/query/plumbing.rs

+32-14
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use dep_graph::{DepNodeIndex, DepNode, DepKind, DepNodeColor, OpenTask};
1616
use errors::DiagnosticBuilder;
1717
use errors::Level;
18+
use errors::Diagnostic;
1819
use ty::tls;
1920
use ty::{TyCtxt};
2021
use ty::query::Query;
@@ -289,13 +290,15 @@ impl<'a, 'tcx, Q: QueryDescription<'tcx>> JobOwner<'a, 'tcx, Q> {
289290
&self,
290291
tcx: TyCtxt<'_, 'tcx, 'tcx>,
291292
task: &OpenTask,
293+
diagnostics: Option<&Lock<Option<Box<Vec<Diagnostic>>>>>,
292294
key: Q::Key,
293295
) -> Q::Value
294296
{
295297
// Update the ImplicitCtxt to point to our new query job
296298
let new_icx = tls::ImplicitCtxt {
297299
tcx,
298300
query: Some(LrcRef::new(&self.job)),
301+
diagnostics,
299302
layout_depth: self.layout_depth,
300303
task,
301304
};
@@ -305,6 +308,17 @@ impl<'a, 'tcx, Q: QueryDescription<'tcx>> JobOwner<'a, 'tcx, Q> {
305308
Q::compute(tcx, key)
306309
})
307310
}
311+
312+
}
313+
314+
#[inline(always)]
315+
fn with_diagnostics<F, R>(f: F) -> (R, Option<Box<Vec<Diagnostic>>>)
316+
where
317+
F: FnOnce(Option<&Lock<Option<Box<Vec<Diagnostic>>>>>) -> R
318+
{
319+
let diagnostics = Lock::new(None);
320+
let result = f(Some(&diagnostics));
321+
(result, diagnostics.into_inner())
308322
}
309323

310324
impl<'a, 'tcx, Q: QueryDescription<'tcx>> Drop for JobOwner<'a, 'tcx, Q> {
@@ -489,8 +503,10 @@ impl<'a, 'gcx> TyCtxt<'a, 'gcx, 'gcx> {
489503
profq_msg!(self, ProfileQueriesMsg::ProviderBegin);
490504
self.sess.profiler(|p| p.start_activity(Q::CATEGORY));
491505

492-
let res = self.dep_graph.with_anon_open_task(dep_node.kind, |open_task| {
493-
job.compute(self, open_task, key)
506+
let (res, diag) = with_diagnostics(|diagnostics| {
507+
self.dep_graph.with_anon_open_task(dep_node.kind, |open_task| {
508+
job.compute(self, open_task, diagnostics, key)
509+
})
494510
});
495511

496512
self.sess.profiler(|p| p.end_activity(Q::CATEGORY));
@@ -499,7 +515,7 @@ impl<'a, 'gcx> TyCtxt<'a, 'gcx, 'gcx> {
499515

500516
self.dep_graph.read_index(dep_node_index);
501517

502-
if let Some(diagnostics) = job.job.extract_diagnostics() {
518+
if let Some(diagnostics) = diag {
503519
self.queries.on_disk_cache
504520
.store_diagnostics_for_anon_node(dep_node_index, diagnostics);
505521
}
@@ -573,7 +589,7 @@ impl<'a, 'gcx> TyCtxt<'a, 'gcx, 'gcx> {
573589
// try_mark_green(), so we can ignore them here.
574590
// The dep-graph for this computation is already in
575591
// place so we pass OpenTask::Ignore.
576-
job.compute(self, &OpenTask::Ignore, key)
592+
job.compute(self, &OpenTask::Ignore, None, key)
577593
};
578594

579595
// If -Zincremental-verify-ich is specified, re-hash results from
@@ -647,15 +663,17 @@ impl<'a, 'gcx> TyCtxt<'a, 'gcx, 'gcx> {
647663
p.record_query(Q::CATEGORY);
648664
});
649665

650-
let (result, dep_node_index) = if dep_node.kind.is_eval_always() {
651-
self.dep_graph.with_eval_always_task(self, dep_node, |task| {
652-
job.compute(self, task, key)
653-
})
654-
} else {
655-
self.dep_graph.with_query_task(self, dep_node, |task| {
656-
job.compute(self, task, key)
657-
})
658-
};
666+
let ((result, dep_node_index), diagnostics) = with_diagnostics(|diagnostics| {
667+
if dep_node.kind.is_eval_always() {
668+
self.dep_graph.with_eval_always_task(self, dep_node, |task| {
669+
job.compute(self, task, diagnostics, key)
670+
})
671+
} else {
672+
self.dep_graph.with_query_task(self, dep_node, |task| {
673+
job.compute(self, task, diagnostics, key)
674+
})
675+
}
676+
});
659677

660678
self.sess.profiler(|p| p.end_activity(Q::CATEGORY));
661679
profq_msg!(self, ProfileQueriesMsg::ProviderEnd);
@@ -665,7 +683,7 @@ impl<'a, 'gcx> TyCtxt<'a, 'gcx, 'gcx> {
665683
}
666684

667685
if dep_node.kind != ::dep_graph::DepKind::Null {
668-
if let Some(diagnostics) = job.job.extract_diagnostics() {
686+
if let Some(diagnostics) = diagnostics {
669687
self.queries.on_disk_cache
670688
.store_diagnostics(dep_node_index, diagnostics);
671689
}

0 commit comments

Comments
 (0)