1
Fork 0

Use a function to create QueryStackDeferred to ensure context is Copy

This commit is contained in:
John Kåre Alsaker 2025-03-26 13:04:06 +01:00
parent 6319bb38cc
commit 6ca2af6434
2 changed files with 48 additions and 36 deletions

View file

@ -3,7 +3,6 @@
//! manage the caches, and so forth. //! manage the caches, and so forth.
use std::num::NonZero; use std::num::NonZero;
use std::sync::Arc;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_data_structures::sync::{DynSend, DynSync}; use rustc_data_structures::sync::{DynSend, DynSync};
@ -312,19 +311,17 @@ macro_rules! should_ever_cache_on_disk {
}; };
} }
pub(crate) fn create_query_frame< fn create_query_frame_extra<'tcx, K: Key + Copy + 'tcx>(
'tcx, (tcx, key, kind, name, do_describe): (
K: Copy + DynSend + DynSync + Key + for<'a> HashStable<StableHashingContext<'a>> + 'tcx, TyCtxt<'tcx>,
>( K,
tcx: TyCtxt<'tcx>, DepKind,
do_describe: fn(TyCtxt<'tcx>, K) -> String, &'static str,
key: K, fn(TyCtxt<'tcx>, K) -> String,
kind: DepKind, ),
name: &'static str, ) -> QueryStackFrameExtra {
) -> QueryStackFrame<QueryStackDeferred<'tcx>> {
let def_id = key.key_as_def_id(); let def_id = key.key_as_def_id();
let extra = move || {
// If reduced queries are requested, we may be printing a query stack due // 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. // to a panic. Avoid using `default_span` and `def_kind` in that case.
let reduce_queries = with_reduced_queries(); let reduce_queries = with_reduced_queries();
@ -351,7 +348,19 @@ pub(crate) fn create_query_frame<
def_id.and_then(|def_id| def_id.as_local()).map(|def_id| tcx.def_kind(def_id)) def_id.and_then(|def_id| def_id.as_local()).map(|def_id| tcx.def_kind(def_id))
}; };
QueryStackFrameExtra::new(description, span, def_kind) QueryStackFrameExtra::new(description, span, def_kind)
}; }
pub(crate) fn create_query_frame<
'tcx,
K: Copy + DynSend + DynSync + Key + for<'a> HashStable<StableHashingContext<'a>> + 'tcx,
>(
tcx: TyCtxt<'tcx>,
do_describe: fn(TyCtxt<'tcx>, K) -> String,
key: K,
kind: DepKind,
name: &'static str,
) -> QueryStackFrame<QueryStackDeferred<'tcx>> {
let def_id = key.key_as_def_id();
let hash = || { let hash = || {
tcx.with_stable_hashing_context(|mut hcx| { tcx.with_stable_hashing_context(|mut hcx| {
@ -363,9 +372,8 @@ pub(crate) fn create_query_frame<
}; };
let def_id_for_ty_in_cycle = key.def_id_for_ty_in_cycle(); 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 let info =
// as they don't have destructors. QueryStackDeferred::new((tcx, key, kind, name, do_describe), create_query_frame_extra);
let info = unsafe { QueryStackDeferred::new(Arc::new(extra)) };
QueryStackFrame::new(info, kind, hash, def_id, def_id_for_ty_in_cycle) QueryStackFrame::new(info, kind, hash, def_id, def_id_for_ty_in_cycle)
} }

View file

@ -108,10 +108,14 @@ pub struct QueryStackDeferred<'tcx> {
} }
impl<'tcx> QueryStackDeferred<'tcx> { impl<'tcx> QueryStackDeferred<'tcx> {
/// SAFETY: `extract` may not access 'tcx in its destructor. pub fn new<C: Copy + DynSync + DynSend + 'tcx>(
pub unsafe fn new( context: C,
extract: Arc<dyn Fn() -> QueryStackFrameExtra + DynSync + DynSend + 'tcx>, extract: fn(C) -> QueryStackFrameExtra,
) -> Self { ) -> Self {
let extract: Arc<dyn Fn() -> QueryStackFrameExtra + DynSync + DynSend + 'tcx> =
Arc::new(move || extract(context));
// SAFETY: The `extract` closure does not access 'tcx in its destructor as the only
// captured variable is `context` which is Copy and cannot have a destructor.
Self { _dummy: PhantomData, extract: unsafe { transmute(extract) } } Self { _dummy: PhantomData, extract: unsafe { transmute(extract) } }
} }