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,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< pub(crate) fn create_query_frame<
'tcx, 'tcx,
K: Copy + DynSend + DynSync + Key + for<'a> HashStable<StableHashingContext<'a>> + '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>> { ) -> 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
// 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 = || { let hash = || {
tcx.with_stable_hashing_context(|mut hcx| { tcx.with_stable_hashing_context(|mut hcx| {
let mut hasher = StableHasher::new(); 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(); 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) } }
} }