Skip to content

Commit 6ca2af6

Browse files
committed
Use a function to create QueryStackDeferred to ensure context is Copy
1 parent 6319bb3 commit 6ca2af6

File tree

2 files changed

+48
-36
lines changed

2 files changed

+48
-36
lines changed

Diff for: compiler/rustc_query_impl/src/plumbing.rs

+41-33
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
//! manage the caches, and so forth.
44
55
use std::num::NonZero;
6-
use std::sync::Arc;
76

87
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
98
use rustc_data_structures::sync::{DynSend, DynSync};
@@ -312,6 +311,45 @@ macro_rules! should_ever_cache_on_disk {
312311
};
313312
}
314313

314+
fn create_query_frame_extra<'tcx, K: Key + Copy + 'tcx>(
315+
(tcx, key, kind, name, do_describe): (
316+
TyCtxt<'tcx>,
317+
K,
318+
DepKind,
319+
&'static str,
320+
fn(TyCtxt<'tcx>, K) -> String,
321+
),
322+
) -> QueryStackFrameExtra {
323+
let def_id = key.key_as_def_id();
324+
325+
// If reduced queries are requested, we may be printing a query stack due
326+
// to a panic. Avoid using `default_span` and `def_kind` in that case.
327+
let reduce_queries = with_reduced_queries();
328+
329+
// Avoid calling queries while formatting the description
330+
let description = ty::print::with_no_queries!(do_describe(tcx, key));
331+
let description = if tcx.sess.verbose_internals() {
332+
format!("{description} [{name:?}]")
333+
} else {
334+
description
335+
};
336+
let span = if kind == dep_graph::dep_kinds::def_span || reduce_queries {
337+
// The `def_span` query is used to calculate `default_span`,
338+
// so exit to avoid infinite recursion.
339+
None
340+
} else {
341+
Some(key.default_span(tcx))
342+
};
343+
344+
let def_kind = if kind == dep_graph::dep_kinds::def_kind || reduce_queries {
345+
// Try to avoid infinite recursion.
346+
None
347+
} else {
348+
def_id.and_then(|def_id| def_id.as_local()).map(|def_id| tcx.def_kind(def_id))
349+
};
350+
QueryStackFrameExtra::new(description, span, def_kind)
351+
}
352+
315353
pub(crate) fn create_query_frame<
316354
'tcx,
317355
K: Copy + DynSend + DynSync + Key + for<'a> HashStable<StableHashingContext<'a>> + 'tcx,
@@ -324,35 +362,6 @@ pub(crate) fn create_query_frame<
324362
) -> QueryStackFrame<QueryStackDeferred<'tcx>> {
325363
let def_id = key.key_as_def_id();
326364

327-
let extra = move || {
328-
// If reduced queries are requested, we may be printing a query stack due
329-
// to a panic. Avoid using `default_span` and `def_kind` in that case.
330-
let reduce_queries = with_reduced_queries();
331-
332-
// Avoid calling queries while formatting the description
333-
let description = ty::print::with_no_queries!(do_describe(tcx, key));
334-
let description = if tcx.sess.verbose_internals() {
335-
format!("{description} [{name:?}]")
336-
} else {
337-
description
338-
};
339-
let span = if kind == dep_graph::dep_kinds::def_span || reduce_queries {
340-
// The `def_span` query is used to calculate `default_span`,
341-
// so exit to avoid infinite recursion.
342-
None
343-
} else {
344-
Some(key.default_span(tcx))
345-
};
346-
347-
let def_kind = if kind == dep_graph::dep_kinds::def_kind || reduce_queries {
348-
// Try to avoid infinite recursion.
349-
None
350-
} else {
351-
def_id.and_then(|def_id| def_id.as_local()).map(|def_id| tcx.def_kind(def_id))
352-
};
353-
QueryStackFrameExtra::new(description, span, def_kind)
354-
};
355-
356365
let hash = || {
357366
tcx.with_stable_hashing_context(|mut hcx| {
358367
let mut hasher = StableHasher::new();
@@ -363,9 +372,8 @@ pub(crate) fn create_query_frame<
363372
};
364373
let def_id_for_ty_in_cycle = key.def_id_for_ty_in_cycle();
365374

366-
// SAFETY: None of the captures in `extra` have destructors that access 'tcx
367-
// as they don't have destructors.
368-
let info = unsafe { QueryStackDeferred::new(Arc::new(extra)) };
375+
let info =
376+
QueryStackDeferred::new((tcx, key, kind, name, do_describe), create_query_frame_extra);
369377

370378
QueryStackFrame::new(info, kind, hash, def_id, def_id_for_ty_in_cycle)
371379
}

Diff for: compiler/rustc_query_system/src/query/mod.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,14 @@ pub struct QueryStackDeferred<'tcx> {
108108
}
109109

110110
impl<'tcx> QueryStackDeferred<'tcx> {
111-
/// SAFETY: `extract` may not access 'tcx in its destructor.
112-
pub unsafe fn new(
113-
extract: Arc<dyn Fn() -> QueryStackFrameExtra + DynSync + DynSend + 'tcx>,
111+
pub fn new<C: Copy + DynSync + DynSend + 'tcx>(
112+
context: C,
113+
extract: fn(C) -> QueryStackFrameExtra,
114114
) -> Self {
115+
let extract: Arc<dyn Fn() -> QueryStackFrameExtra + DynSync + DynSend + 'tcx> =
116+
Arc::new(move || extract(context));
117+
// SAFETY: The `extract` closure does not access 'tcx in its destructor as the only
118+
// captured variable is `context` which is Copy and cannot have a destructor.
115119
Self { _dummy: PhantomData, extract: unsafe { transmute(extract) } }
116120
}
117121

0 commit comments

Comments
 (0)