about summary refs log tree commit diff
path: root/compiler/rustc_query_impl/src
diff options
context:
space:
mode:
authorJohn Kåre Alsaker <john.kare.alsaker@gmail.com>2025-03-26 13:04:06 +0100
committerJohn Kåre Alsaker <john.kare.alsaker@gmail.com>2025-03-26 13:09:36 +0100
commit6ca2af6434ec40ae91207628722fe6e731f6f358 (patch)
treecc99d09c2841408677e613cea1f544cae37869f7 /compiler/rustc_query_impl/src
parent6319bb38ccb316b193e35720c9df953e5ab01c22 (diff)
downloadrust-6ca2af6434ec40ae91207628722fe6e731f6f358.tar.gz
rust-6ca2af6434ec40ae91207628722fe6e731f6f358.zip
Use a function to create `QueryStackDeferred` to ensure context is Copy
Diffstat (limited to 'compiler/rustc_query_impl/src')
-rw-r--r--compiler/rustc_query_impl/src/plumbing.rs74
1 files changed, 41 insertions, 33 deletions
diff --git a/compiler/rustc_query_impl/src/plumbing.rs b/compiler/rustc_query_impl/src/plumbing.rs
index 7f1d466b869..3238c7a0912 100644
--- a/compiler/rustc_query_impl/src/plumbing.rs
+++ b/compiler/rustc_query_impl/src/plumbing.rs
@@ -3,7 +3,6 @@
 //! manage the caches, and so forth.
 
 use std::num::NonZero;
-use std::sync::Arc;
 
 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
 use rustc_data_structures::sync::{DynSend, DynSync};
@@ -312,6 +311,45 @@ macro_rules! should_ever_cache_on_disk {
     };
 }
 
+fn create_query_frame_extra<'tcx, K: Key + Copy + 'tcx>(
+    (tcx, key, kind, name, do_describe): (
+        TyCtxt<'tcx>,
+        K,
+        DepKind,
+        &'static str,
+        fn(TyCtxt<'tcx>, K) -> String,
+    ),
+) -> QueryStackFrameExtra {
+    let def_id = key.key_as_def_id();
+
+    // If reduced queries are requested, we may be printing a query stack due
+    // to a panic. Avoid using `default_span` and `def_kind` in that case.
+    let reduce_queries = with_reduced_queries();
+
+    // Avoid calling queries while formatting the description
+    let description = ty::print::with_no_queries!(do_describe(tcx, key));
+    let description = if tcx.sess.verbose_internals() {
+        format!("{description} [{name:?}]")
+    } else {
+        description
+    };
+    let span = if kind == dep_graph::dep_kinds::def_span || reduce_queries {
+        // The `def_span` query is used to calculate `default_span`,
+        // so exit to avoid infinite recursion.
+        None
+    } else {
+        Some(key.default_span(tcx))
+    };
+
+    let def_kind = if kind == dep_graph::dep_kinds::def_kind || reduce_queries {
+        // Try to avoid infinite recursion.
+        None
+    } else {
+        def_id.and_then(|def_id| def_id.as_local()).map(|def_id| tcx.def_kind(def_id))
+    };
+    QueryStackFrameExtra::new(description, span, def_kind)
+}
+
 pub(crate) fn create_query_frame<
     'tcx,
     K: Copy + DynSend + DynSync + Key + for<'a> HashStable<StableHashingContext<'a>> + 'tcx,
@@ -324,35 +362,6 @@ pub(crate) fn create_query_frame<
 ) -> QueryStackFrame<QueryStackDeferred<'tcx>> {
     let def_id = key.key_as_def_id();
 
-    let extra = move || {
-        // If reduced queries are requested, we may be printing a query stack due
-        // to a panic. Avoid using `default_span` and `def_kind` in that case.
-        let reduce_queries = with_reduced_queries();
-
-        // Avoid calling queries while formatting the description
-        let description = ty::print::with_no_queries!(do_describe(tcx, key));
-        let description = if tcx.sess.verbose_internals() {
-            format!("{description} [{name:?}]")
-        } else {
-            description
-        };
-        let span = if kind == dep_graph::dep_kinds::def_span || reduce_queries {
-            // The `def_span` query is used to calculate `default_span`,
-            // so exit to avoid infinite recursion.
-            None
-        } else {
-            Some(key.default_span(tcx))
-        };
-
-        let def_kind = if kind == dep_graph::dep_kinds::def_kind || reduce_queries {
-            // Try to avoid infinite recursion.
-            None
-        } else {
-            def_id.and_then(|def_id| def_id.as_local()).map(|def_id| tcx.def_kind(def_id))
-        };
-        QueryStackFrameExtra::new(description, span, def_kind)
-    };
-
     let hash = || {
         tcx.with_stable_hashing_context(|mut hcx| {
             let mut hasher = StableHasher::new();
@@ -363,9 +372,8 @@ pub(crate) fn create_query_frame<
     };
     let def_id_for_ty_in_cycle = key.def_id_for_ty_in_cycle();
 
-    // SAFETY: None of the captures in `extra` have destructors that access 'tcx
-    // as they don't have destructors.
-    let info = unsafe { QueryStackDeferred::new(Arc::new(extra)) };
+    let info =
+        QueryStackDeferred::new((tcx, key, kind, name, do_describe), create_query_frame_extra);
 
     QueryStackFrame::new(info, kind, hash, def_id, def_id_for_ty_in_cycle)
 }