]> git.lizzy.rs Git - rust.git/commitdiff
Error if we try to read dep during deserialization
authorAaron Hill <aa1ronham@gmail.com>
Tue, 14 Dec 2021 02:56:30 +0000 (20:56 -0600)
committerAaron Hill <aa1ronham@gmail.com>
Thu, 23 Dec 2021 18:38:53 +0000 (13:38 -0500)
compiler/rustc_query_impl/src/plumbing.rs
compiler/rustc_query_system/src/dep_graph/graph.rs
compiler/rustc_query_system/src/query/mod.rs
compiler/rustc_query_system/src/query/plumbing.rs

index 6d76d09f6190e21d5fa56fe8c17be3c3299df2ab..5ebeae35964dee11b8bbbaadfb116c81b62c9592 100644 (file)
@@ -83,12 +83,17 @@ fn start_query<R>(
         &self,
         token: QueryJobId<Self::DepKind>,
         diagnostics: Option<&Lock<ThinVec<Diagnostic>>>,
+        read_allowed: bool,
         compute: impl FnOnce() -> R,
     ) -> R {
         // The `TyCtxt` stored in TLS has the same global interner lifetime
         // as `self`, so we use `with_related_context` to relate the 'tcx lifetimes
         // when accessing the `ImplicitCtxt`.
         tls::with_related_context(**self, move |current_icx| {
+            let mut old_read_allowed = false;
+            if let Some(task_deps) = current_icx.task_deps {
+                old_read_allowed = std::mem::replace(&mut task_deps.lock().read_allowed, read_allowed);
+            }
             // Update the `ImplicitCtxt` to point to our new query job.
             let new_icx = ImplicitCtxt {
                 tcx: **self,
@@ -99,9 +104,14 @@ fn start_query<R>(
             };
 
             // Use the `ImplicitCtxt` while we execute the query.
-            tls::enter_context(&new_icx, |_| {
+            let res = tls::enter_context(&new_icx, |_| {
                 rustc_data_structures::stack::ensure_sufficient_stack(compute)
-            })
+            });
+
+            if let Some(task_deps) = new_icx.task_deps {
+                task_deps.lock().read_allowed = old_read_allowed;
+            }
+            res
         })
     }
 }
index a8be1ca34c04f0f0e8cd59a8777d90e15a6eae13..64dcc6f37f14409f937620d97d953d77a7194991 100644 (file)
@@ -251,6 +251,7 @@ fn with_task_impl<Ctxt: HasDepContext<DepKind = K>, A: Debug, R>(
                 reads: SmallVec::new(),
                 read_set: Default::default(),
                 phantom_data: PhantomData,
+                read_allowed: true,
             }))
         };
         let result = K::with_deps(task_deps.as_ref(), || task(cx, arg));
@@ -362,6 +363,11 @@ pub fn read_index(&self, dep_node_index: DepNodeIndex) {
                 if let Some(task_deps) = task_deps {
                     let mut task_deps = task_deps.lock();
                     let task_deps = &mut *task_deps;
+
+                    if !task_deps.read_allowed {
+                        panic!("Illegal read of: {:?}", dep_node_index);
+                    }
+
                     if cfg!(debug_assertions) {
                         data.current.total_read_count.fetch_add(1, Relaxed);
                     }
@@ -1115,6 +1121,7 @@ pub struct TaskDeps<K> {
     reads: EdgesVec,
     read_set: FxHashSet<DepNodeIndex>,
     phantom_data: PhantomData<DepNode<K>>,
+    pub read_allowed: bool,
 }
 
 impl<K> Default for TaskDeps<K> {
@@ -1125,6 +1132,7 @@ fn default() -> Self {
             reads: EdgesVec::new(),
             read_set: FxHashSet::default(),
             phantom_data: PhantomData,
+            read_allowed: true,
         }
     }
 }
index a2f7843baaa680b2c1b52cf679646d470102f0ae..265e0b80d7c934f2029e63643a276d04ba801bb9 100644 (file)
@@ -142,6 +142,7 @@ fn start_query<R>(
         &self,
         token: QueryJobId<Self::DepKind>,
         diagnostics: Option<&Lock<ThinVec<Diagnostic>>>,
+        read_allowed: bool,
         compute: impl FnOnce() -> R,
     ) -> R;
 }
index b08db39e245a80ecb59dbd2a078e1bc3b9d83219..e4a177cbffb2b8790ae58d13974d99496beb9b58 100644 (file)
@@ -440,7 +440,7 @@ fn execute_job<CTX, K, V>(
     // Fast path for when incr. comp. is off.
     if !dep_graph.is_fully_enabled() {
         let prof_timer = tcx.dep_context().profiler().query_provider();
-        let result = tcx.start_query(job_id, None, || query.compute(*tcx.dep_context(), key));
+        let result = tcx.start_query(job_id, None, true, || query.compute(*tcx.dep_context(), key));
         let dep_node_index = dep_graph.next_virtual_depnode_index();
         prof_timer.finish_with_query_invocation_id(dep_node_index.into());
         return (result, dep_node_index);
@@ -453,7 +453,7 @@ fn execute_job<CTX, K, V>(
 
         // The diagnostics for this query will be promoted to the current session during
         // `try_mark_green()`, so we can ignore them here.
-        if let Some(ret) = tcx.start_query(job_id, None, || {
+        if let Some(ret) = tcx.start_query(job_id, None, false, || {
             try_load_from_disk_and_cache_in_memory(tcx, &key, &dep_node, query)
         }) {
             return ret;
@@ -463,7 +463,7 @@ fn execute_job<CTX, K, V>(
     let prof_timer = tcx.dep_context().profiler().query_provider();
     let diagnostics = Lock::new(ThinVec::new());
 
-    let (result, dep_node_index) = tcx.start_query(job_id, Some(&diagnostics), || {
+    let (result, dep_node_index) = tcx.start_query(job_id, Some(&diagnostics), true, || {
         if query.anon {
             return dep_graph.with_anon_task(*tcx.dep_context(), query.dep_kind, || {
                 query.compute(*tcx.dep_context(), key)