Skip to content

Commit df27092

Browse files
committed
Generalize query side effect handling to make future additions easier
1 parent f10bfbe commit df27092

File tree

6 files changed

+27
-21
lines changed

6 files changed

+27
-21
lines changed

compiler/rustc_interface/src/callbacks.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ fn track_span_parent(def_id: rustc_span::def_id::LocalDefId) {
4141
fn track_diagnostic<R>(diagnostic: DiagInner, f: &mut dyn FnMut(DiagInner) -> R) -> R {
4242
tls::with_context_opt(|icx| {
4343
if let Some(icx) = icx {
44-
if let Some(diagnostics) = icx.diagnostics {
45-
diagnostics.lock().extend(Some(diagnostic.clone()));
44+
if let Some(side_effects) = icx.side_effects {
45+
let diagnostic = diagnostic.clone();
46+
side_effects.lock().diagnostics.push(diagnostic);
4647
}
4748

4849
// Diagnostics are tracked, we can ignore the dependency.

compiler/rustc_middle/src/ty/context/tls.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use std::{mem, ptr};
22

33
use rustc_data_structures::sync::{self, Lock};
4-
use rustc_errors::DiagInner;
5-
use thin_vec::ThinVec;
4+
use rustc_query_system::query::QuerySideEffects;
65

76
use super::{GlobalCtxt, TyCtxt};
87
use crate::dep_graph::TaskDepsRef;
@@ -24,7 +23,7 @@ pub struct ImplicitCtxt<'a, 'tcx> {
2423

2524
/// Where to store diagnostics for the current query job, if any.
2625
/// This is updated by `JobOwner::start` in `ty::query::plumbing` when executing a query.
27-
pub diagnostics: Option<&'a Lock<ThinVec<DiagInner>>>,
26+
pub side_effects: Option<&'a Lock<QuerySideEffects>>,
2827

2928
/// Used to prevent queries from calling too deeply.
3029
pub query_depth: usize,
@@ -40,7 +39,7 @@ impl<'a, 'tcx> ImplicitCtxt<'a, 'tcx> {
4039
ImplicitCtxt {
4140
tcx,
4241
query: None,
43-
diagnostics: None,
42+
side_effects: None,
4443
query_depth: 0,
4544
task_deps: TaskDepsRef::Ignore,
4645
}

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use std::num::NonZero;
77
use rustc_data_structures::stable_hasher::{Hash64, HashStable, StableHasher};
88
use rustc_data_structures::sync::Lock;
99
use rustc_data_structures::unord::UnordMap;
10-
use rustc_errors::DiagInner;
1110
use rustc_index::Idx;
1211
use rustc_middle::bug;
1312
use rustc_middle::dep_graph::{
@@ -31,7 +30,6 @@ use rustc_query_system::{LayoutOfDepth, QueryOverflow};
3130
use rustc_serialize::{Decodable, Encodable};
3231
use rustc_session::Limit;
3332
use rustc_span::def_id::LOCAL_CRATE;
34-
use thin_vec::ThinVec;
3533

3634
use crate::QueryConfigRestored;
3735

@@ -127,7 +125,7 @@ impl QueryContext for QueryCtxt<'_> {
127125
self,
128126
token: QueryJobId,
129127
depth_limit: bool,
130-
diagnostics: Option<&Lock<ThinVec<DiagInner>>>,
128+
side_effects: Option<&Lock<QuerySideEffects>>,
131129
compute: impl FnOnce() -> R,
132130
) -> R {
133131
// The `TyCtxt` stored in TLS has the same global interner lifetime
@@ -142,7 +140,7 @@ impl QueryContext for QueryCtxt<'_> {
142140
let new_icx = ImplicitCtxt {
143141
tcx: self.tcx,
144142
query: Some(token),
145-
diagnostics,
143+
side_effects,
146144
query_depth: current_icx.query_depth + depth_limit as usize,
147145
task_deps: current_icx.task_deps,
148146
};
@@ -174,6 +172,16 @@ impl QueryContext for QueryCtxt<'_> {
174172
crate_name: self.crate_name(LOCAL_CRATE),
175173
});
176174
}
175+
176+
#[tracing::instrument(level = "trace", skip(self))]
177+
fn apply_side_effects(self, side_effects: QuerySideEffects) {
178+
let dcx = self.dep_context().sess().dcx();
179+
let QuerySideEffects { diagnostics } = side_effects;
180+
181+
for diagnostic in diagnostics {
182+
dcx.emit_diagnostic(diagnostic);
183+
}
184+
}
177185
}
178186

179187
pub(super) fn try_mark_green<'tcx>(tcx: TyCtxt<'tcx>, dep_node: &dep_graph::DepNode) -> bool {

compiler/rustc_query_system/src/dep_graph/graph.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -911,11 +911,7 @@ impl<D: Deps> DepGraphData<D> {
911911
// Promote the previous diagnostics to the current session.
912912
qcx.store_side_effects(dep_node_index, side_effects.clone());
913913

914-
let dcx = qcx.dep_context().sess().dcx();
915-
916-
for diagnostic in side_effects.diagnostics {
917-
dcx.emit_diagnostic(diagnostic);
918-
}
914+
qcx.apply_side_effects(side_effects);
919915
}
920916
}
921917
}

compiler/rustc_query_system/src/query/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub struct QuerySideEffects {
7575
/// Stores any diagnostics emitted during query execution.
7676
/// These diagnostics will be re-emitted if we mark
7777
/// the query as green.
78-
pub(super) diagnostics: ThinVec<DiagInner>,
78+
pub diagnostics: ThinVec<DiagInner>,
7979
}
8080

8181
impl QuerySideEffects {
@@ -107,6 +107,9 @@ pub trait QueryContext: HasDepContext {
107107
/// Register diagnostics for the given node, for use in next session.
108108
fn store_side_effects(self, dep_node_index: DepNodeIndex, side_effects: QuerySideEffects);
109109

110+
/// Actually execute the side effects
111+
fn apply_side_effects(self, side_effects: QuerySideEffects);
112+
110113
/// Register diagnostics for the given node, for use in next session.
111114
fn store_side_effects_for_anon_node(
112115
self,
@@ -121,7 +124,7 @@ pub trait QueryContext: HasDepContext {
121124
self,
122125
token: QueryJobId,
123126
depth_limit: bool,
124-
diagnostics: Option<&Lock<ThinVec<DiagInner>>>,
127+
side_effects: Option<&Lock<QuerySideEffects>>,
125128
compute: impl FnOnce() -> R,
126129
) -> R;
127130

compiler/rustc_query_system/src/query/plumbing.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use rustc_data_structures::sync::Lock;
1616
use rustc_data_structures::{outline, sync};
1717
use rustc_errors::{Diag, FatalError, StashKey};
1818
use rustc_span::{DUMMY_SP, Span};
19-
use thin_vec::ThinVec;
2019
use tracing::instrument;
2120

2221
use super::QueryConfig;
@@ -515,10 +514,10 @@ where
515514
}
516515

517516
let prof_timer = qcx.dep_context().profiler().query_provider();
518-
let diagnostics = Lock::new(ThinVec::new());
517+
let side_effects = Lock::new(QuerySideEffects::default());
519518

520519
let (result, dep_node_index) =
521-
qcx.start_query(job_id, query.depth_limit(), Some(&diagnostics), || {
520+
qcx.start_query(job_id, query.depth_limit(), Some(&side_effects), || {
522521
if query.anon() {
523522
return dep_graph_data.with_anon_task_inner(
524523
*qcx.dep_context(),
@@ -542,7 +541,7 @@ where
542541

543542
prof_timer.finish_with_query_invocation_id(dep_node_index.into());
544543

545-
let side_effects = QuerySideEffects { diagnostics: diagnostics.into_inner() };
544+
let side_effects = side_effects.into_inner();
546545

547546
if std::intrinsics::unlikely(side_effects.maybe_any()) {
548547
if query.anon() {

0 commit comments

Comments
 (0)