Skip to content

Commit 75181dc

Browse files
committed
Error if we try to read dep during deserialization
1 parent 489296d commit 75181dc

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,17 @@ impl QueryContext for QueryCtxt<'_> {
8383
&self,
8484
token: QueryJobId<Self::DepKind>,
8585
diagnostics: Option<&Lock<ThinVec<Diagnostic>>>,
86+
read_allowed: bool,
8687
compute: impl FnOnce() -> R,
8788
) -> R {
8889
// The `TyCtxt` stored in TLS has the same global interner lifetime
8990
// as `self`, so we use `with_related_context` to relate the 'tcx lifetimes
9091
// when accessing the `ImplicitCtxt`.
9192
tls::with_related_context(**self, move |current_icx| {
93+
let mut old_read_allowed = false;
94+
if let Some(task_deps) = current_icx.task_deps {
95+
old_read_allowed = std::mem::replace(&mut task_deps.lock().read_allowed, read_allowed);
96+
}
9297
// Update the `ImplicitCtxt` to point to our new query job.
9398
let new_icx = ImplicitCtxt {
9499
tcx: **self,
@@ -99,9 +104,14 @@ impl QueryContext for QueryCtxt<'_> {
99104
};
100105

101106
// Use the `ImplicitCtxt` while we execute the query.
102-
tls::enter_context(&new_icx, |_| {
107+
let res = tls::enter_context(&new_icx, |_| {
103108
rustc_data_structures::stack::ensure_sufficient_stack(compute)
104-
})
109+
});
110+
111+
if let Some(task_deps) = new_icx.task_deps {
112+
task_deps.lock().read_allowed = old_read_allowed;
113+
}
114+
res
105115
})
106116
}
107117
}

compiler/rustc_query_system/src/dep_graph/graph.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ impl<K: DepKind> DepGraph<K> {
251251
reads: SmallVec::new(),
252252
read_set: Default::default(),
253253
phantom_data: PhantomData,
254+
read_allowed: true,
254255
}))
255256
};
256257
let result = K::with_deps(task_deps.as_ref(), || task(cx, arg));
@@ -362,6 +363,11 @@ impl<K: DepKind> DepGraph<K> {
362363
if let Some(task_deps) = task_deps {
363364
let mut task_deps = task_deps.lock();
364365
let task_deps = &mut *task_deps;
366+
367+
if !task_deps.read_allowed {
368+
panic!("Illegal read of: {:?}", dep_node_index);
369+
}
370+
365371
if cfg!(debug_assertions) {
366372
data.current.total_read_count.fetch_add(1, Relaxed);
367373
}
@@ -1115,6 +1121,7 @@ pub struct TaskDeps<K> {
11151121
reads: EdgesVec,
11161122
read_set: FxHashSet<DepNodeIndex>,
11171123
phantom_data: PhantomData<DepNode<K>>,
1124+
pub read_allowed: bool,
11181125
}
11191126

11201127
impl<K> Default for TaskDeps<K> {
@@ -1125,6 +1132,7 @@ impl<K> Default for TaskDeps<K> {
11251132
reads: EdgesVec::new(),
11261133
read_set: FxHashSet::default(),
11271134
phantom_data: PhantomData,
1135+
read_allowed: true,
11281136
}
11291137
}
11301138
}

compiler/rustc_query_system/src/query/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ pub trait QueryContext: HasDepContext {
142142
&self,
143143
token: QueryJobId<Self::DepKind>,
144144
diagnostics: Option<&Lock<ThinVec<Diagnostic>>>,
145+
read_allowed: bool,
145146
compute: impl FnOnce() -> R,
146147
) -> R;
147148
}

compiler/rustc_query_system/src/query/plumbing.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ where
440440
// Fast path for when incr. comp. is off.
441441
if !dep_graph.is_fully_enabled() {
442442
let prof_timer = tcx.dep_context().profiler().query_provider();
443-
let result = tcx.start_query(job_id, None, || query.compute(*tcx.dep_context(), key));
443+
let result = tcx.start_query(job_id, None, true, || query.compute(*tcx.dep_context(), key));
444444
let dep_node_index = dep_graph.next_virtual_depnode_index();
445445
prof_timer.finish_with_query_invocation_id(dep_node_index.into());
446446
return (result, dep_node_index);
@@ -453,7 +453,7 @@ where
453453

454454
// The diagnostics for this query will be promoted to the current session during
455455
// `try_mark_green()`, so we can ignore them here.
456-
if let Some(ret) = tcx.start_query(job_id, None, || {
456+
if let Some(ret) = tcx.start_query(job_id, None, false, || {
457457
try_load_from_disk_and_cache_in_memory(tcx, &key, &dep_node, query)
458458
}) {
459459
return ret;
@@ -463,7 +463,7 @@ where
463463
let prof_timer = tcx.dep_context().profiler().query_provider();
464464
let diagnostics = Lock::new(ThinVec::new());
465465

466-
let (result, dep_node_index) = tcx.start_query(job_id, Some(&diagnostics), || {
466+
let (result, dep_node_index) = tcx.start_query(job_id, Some(&diagnostics), true, || {
467467
if query.anon {
468468
return dep_graph.with_anon_task(*tcx.dep_context(), query.dep_kind, || {
469469
query.compute(*tcx.dep_context(), key)

0 commit comments

Comments
 (0)