]> git.lizzy.rs Git - rust.git/commitdiff
Move `try_on_disk_cache` out of the giant macro
authorJoshua Nelson <jnelson@cloudflare.com>
Wed, 24 Aug 2022 03:10:47 +0000 (22:10 -0500)
committerJoshua Nelson <jnelson@cloudflare.com>
Thu, 1 Sep 2022 23:42:26 +0000 (18:42 -0500)
compiler/rustc_query_impl/src/plumbing.rs

index b115381eb1c1dcf97a83409dfcbb5b7e4ecef4ab..e0707eee8ba66aea5e0dabf481f771b803648664 100644 (file)
@@ -7,7 +7,7 @@
 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
 use rustc_data_structures::sync::Lock;
 use rustc_errors::{Diagnostic, Handler};
-use rustc_middle::dep_graph::{self, DepKind, DepNodeIndex, SerializedDepNodeIndex};
+use rustc_middle::dep_graph::{self, DepKind, DepNode, DepNodeIndex, SerializedDepNodeIndex};
 use rustc_middle::ty::tls::{self, ImplicitCtxt};
 use rustc_middle::ty::{self, TyCtxt};
 use rustc_query_system::dep_graph::HasDepContext;
@@ -298,6 +298,23 @@ pub(crate) fn create_query_frame<
     QueryStackFrame::new(name, description, span, def_kind, hash)
 }
 
+pub(crate) fn try_load_from_on_disk_cache<'tcx, K, V>(
+    tcx: TyCtxt<'tcx>,
+    dep_node: DepNode,
+    recover: fn(TyCtxt<'tcx>, DepNode) -> Option<K>,
+    cache_on_disk: fn(TyCtxt<'tcx>, &K) -> bool,
+    do_query: fn(TyCtxt<'tcx>, K) -> V,
+) {
+    debug_assert!(tcx.dep_graph.is_green(&dep_node));
+
+    let key = recover(tcx, dep_node).unwrap_or_else(|| {
+        panic!("Failed to recover key for {:?} with hash {}", dep_node, dep_node.hash)
+    });
+    if cache_on_disk(tcx, &key) {
+        let _ = do_query(tcx, key);
+    }
+}
+
 // NOTE: `$V` isn't used here, but we still need to match on it so it can be passed to other macros
 // invoked by `rustc_query_append`.
 macro_rules! define_queries {
@@ -457,21 +474,12 @@ fn force_from_dep_node(tcx: TyCtxt<'_>, dep_node: DepNode) -> bool {
                     }
                 }
 
-                fn try_load_from_on_disk_cache(tcx: TyCtxt<'_>, dep_node: DepNode) {
-                    debug_assert!(tcx.dep_graph.is_green(&dep_node));
-
-                    let key = recover(tcx, dep_node).unwrap_or_else(|| panic!("Failed to recover key for {:?} with hash {}", dep_node, dep_node.hash));
-                    if queries::$name::cache_on_disk(tcx, &key) {
-                        let _ = tcx.$name(key);
-                    }
-                }
-
                 DepKindStruct {
                     is_anon,
                     is_eval_always,
                     fingerprint_style,
                     force_from_dep_node: Some(force_from_dep_node),
-                    try_load_from_on_disk_cache: Some(try_load_from_on_disk_cache),
+                    try_load_from_on_disk_cache: Some(|tcx, key| $crate::plumbing::try_load_from_on_disk_cache(tcx, key, recover, queries::$name::cache_on_disk, TyCtxt::$name)),
                 }
             })*
         }