Skip to content

Commit 9d40b09

Browse files
committed
don't monomorphize with_anon_task for each query
1 parent 6a17902 commit 9d40b09

File tree

4 files changed

+37
-23
lines changed

4 files changed

+37
-23
lines changed

compiler/rustc_query_impl/src/plumbing.rs

+29-15
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,31 @@ impl<'tcx> HasDepContext for QueryCtxt<'tcx> {
5252
}
5353
}
5454

55-
impl QueryContext for QueryCtxt<'_> {
55+
impl<'tcx> QueryCtxt<'tcx> {
56+
// Define this closure separately from the one passed to `with_related_context` so it only has to be monomorphized once.
57+
fn make_icx<'a>(
58+
self,
59+
token: QueryJobId,
60+
depth_limit: bool,
61+
diagnostics: Option<&'a Lock<ThinVec<Diagnostic>>>,
62+
current_icx: &ImplicitCtxt<'a, 'tcx>,
63+
) -> ImplicitCtxt<'a, 'tcx> {
64+
if depth_limit && !self.recursion_limit().value_within_limit(current_icx.query_depth) {
65+
self.depth_limit_error(token);
66+
}
67+
68+
// Update the `ImplicitCtxt` to point to our new query job.
69+
ImplicitCtxt {
70+
tcx: *self,
71+
query: Some(token),
72+
diagnostics,
73+
query_depth: current_icx.query_depth + depth_limit as usize,
74+
task_deps: current_icx.task_deps,
75+
}
76+
}
77+
}
78+
79+
impl<'tcx> QueryContext for QueryCtxt<'tcx> {
5680
fn next_job_id(self) -> QueryJobId {
5781
QueryJobId(
5882
NonZeroU64::new(
@@ -110,21 +134,11 @@ impl QueryContext for QueryCtxt<'_> {
110134
// as `self`, so we use `with_related_context` to relate the 'tcx lifetimes
111135
// when accessing the `ImplicitCtxt`.
112136
tls::with_related_context(*self, move |current_icx| {
113-
if depth_limit && !self.recursion_limit().value_within_limit(current_icx.query_depth) {
114-
self.depth_limit_error(token);
115-
}
116-
117-
// Update the `ImplicitCtxt` to point to our new query job.
118-
let new_icx = ImplicitCtxt {
119-
tcx: *self,
120-
query: Some(token),
121-
diagnostics,
122-
query_depth: current_icx.query_depth + depth_limit as usize,
123-
task_deps: current_icx.task_deps,
124-
};
125-
126137
// Use the `ImplicitCtxt` while we execute the query.
127-
tls::enter_context(&new_icx, compute)
138+
tls::enter_context(
139+
&self.make_icx(token, depth_limit, diagnostics, current_icx),
140+
compute,
141+
)
128142
})
129143
}
130144

compiler/rustc_query_system/src/dep_graph/graph.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -377,14 +377,14 @@ impl<K: DepKind> DepGraph<K> {
377377

378378
/// Executes something within an "anonymous" task, that is, a task the
379379
/// `DepNode` of which is determined by the list of inputs it read from.
380-
pub fn with_anon_task<Tcx: DepContext<DepKind = K>, OP, R>(
380+
pub fn with_anon_task<Tcx: DepContext<DepKind = K>, R>(
381381
&self,
382382
cx: Tcx,
383383
dep_kind: K,
384-
op: OP,
384+
op: &mut dyn FnMut() -> R,
385385
) -> (R, DepNodeIndex)
386-
where
387-
OP: FnOnce() -> R,
386+
// where
387+
// OP: FnOnce() -> R,
388388
{
389389
debug_assert!(!cx.is_eval_always(dep_kind));
390390

compiler/rustc_query_system/src/query/plumbing.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ where
458458
let (result, dep_node_index) =
459459
qcx.start_query(job_id, query.depth_limit(), Some(&diagnostics), || {
460460
if query.anon() {
461-
return dep_graph.with_anon_task(*qcx.dep_context(), query.dep_kind(), || {
461+
return dep_graph.with_anon_task(*qcx.dep_context(), query.dep_kind(), &mut || {
462462
query.compute(qcx, key)
463463
});
464464
}

compiler/rustc_trait_selection/src/traits/select/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1417,12 +1417,12 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
14171417
self.check_recursion_depth(obligation.recursion_depth, error_obligation)
14181418
}
14191419

1420-
fn in_task<OP, R>(&mut self, op: OP) -> (R, DepNodeIndex)
1420+
fn in_task<OP, R>(&mut self, mut op: OP) -> (R, DepNodeIndex)
14211421
where
1422-
OP: FnOnce(&mut Self) -> R,
1422+
OP: FnMut(&mut Self) -> R,
14231423
{
14241424
let (result, dep_node) =
1425-
self.tcx().dep_graph.with_anon_task(self.tcx(), DepKind::TraitSelect, || op(self));
1425+
self.tcx().dep_graph.with_anon_task(self.tcx(), DepKind::TraitSelect, &mut || op(self));
14261426
self.tcx().dep_graph.read_index(dep_node);
14271427
(result, dep_node)
14281428
}

0 commit comments

Comments
 (0)