Skip to content

Use closures more consistently in dep_graph.rs.#153997

Open
nnethercote wants to merge 2 commits intorust-lang:mainfrom
nnethercote:closure-consistency
Open

Use closures more consistently in dep_graph.rs.#153997
nnethercote wants to merge 2 commits intorust-lang:mainfrom
nnethercote:closure-consistency

Conversation

@nnethercote
Copy link
Contributor

@nnethercote nnethercote commented Mar 17, 2026

This file has several methods that take a FnOnce() -> R closure:

  • DepGraph::with_ignore
  • DepGraph::with_query_deserialization
  • DepGraph::with_anon_task
  • DepGraphData::with_anon_task_inner

It also has two methods that take a faux closure via an A argument and a fn(TyCtxt<'tcx>, A) -> R argument:

  • DepGraph::with_task
  • DepGraphData::with_task

The rationale is that the faux closure exercises tight control over what state they have access to. This seems silly when (a) they are passed a TyCtxt, and (b) when similar nearby functions take real closures. And they are more awkward to use, e.g. requiring multiple arguments to be gathered into a tuple. This commit changes the faux closures to real closures.

The commit also changes the types and names of the closures from the awkward op: OP to the more standard f: F.

r? @Zalathar

@rustbot
Copy link
Collaborator

rustbot commented Mar 17, 2026

Some changes occurred in compiler/rustc_codegen_cranelift

cc @bjorn3

Some changes occurred in compiler/rustc_codegen_gcc

cc @antoyo, @GuillaumeGomez

@rustbot rustbot added the A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. label Mar 17, 2026
@rustbot rustbot added A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 17, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 17, 2026

Zalathar is not on the review rotation at the moment.
They may take a while to respond.

@nnethercote
Copy link
Contributor Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-bors bot pushed a commit that referenced this pull request Mar 17, 2026
Use closures more consistently in `dep_graph.rs`.
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Mar 17, 2026
@Zalathar
Copy link
Member

Based on comments, I think there was historically a desire to make sure that dep-graph tasks didn't accidentally pull in other untracked state via closure capture.

But given the existing inconsistency, and the fact that passing tcx gives access to the kitchen sink anyway, there probably isn't any harm in just using normal closures at this point.

@rust-bors
Copy link
Contributor

rust-bors bot commented Mar 17, 2026

☀️ Try build successful (CI)
Build commit: 283c322 (283c322d1c3e0fd24ba3187d32027674e634f7e5, parent: b711f95f86b6489b91fdc55c876ed5f95a8d4560)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (283c322): comparison URL.

Overall result: ❌ regressions - no action needed

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

@bors rollup=never
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.1% [0.1%, 0.1%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results (secondary -1.4%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-1.4% [-1.5%, -1.3%] 2
All ❌✅ (primary) - - 0

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

Results (secondary -0.0%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.0% [-0.0%, -0.0%] 1
All ❌✅ (primary) - - 0

Bootstrap: 481.409s -> 481.18s (-0.05%)
Artifact size: 394.91 MiB -> 396.71 MiB (0.45%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Mar 17, 2026
tcx: TyCtxt<'tcx>,
task_arg: A,
task_fn: fn(tcx: TyCtxt<'tcx>, task_arg: A) -> R,
f: F,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In terms of aesthetics/readability, I'm personally not fond of using tiny identifiers like f for this sort of callback, because they get drowned out by the rest of the code and make it harder to see where the actual work happens.

I would have gone with something like task_fn: impl FnOnce() -> R here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I grepped for \<f: impl Fn in compiler/ and got 210 hits, so it's a well-established idiom.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like I’m missing a step here, because I don’t understand how the reply relates to my comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're saying you don't like short identifiers like f and I'm saying there are many identifiers like that already in the code base.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More generally, I prefer f to something like task_fn, and existing style shows that f is common.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also prefer the longer style personally, both for callbacks and generic parameter names, despite the single-letter names being more common.
(Unless the callback or generic parameter truly has no additional attached semantics, like the callback in Iterator::map or arbitrary return type R in the example above.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t understand what benefit we’d be getting in exchange for hiding the callback point.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree that a name like f is hiding anything.

There are four with_* methods in this file: with_task, with_anon_task, with_anon_task_inner, with_query_deserialization, and with_ignore. What names do you suggest for the parameters? task_fn for all of them? Something else?

(Unless the callback or generic parameter truly has no additional attached semantics, like the callback in Iterator::map or arbitrary return type R in the example above.)

I think these examples do fall into this category. And I count at least 48 other with_* methods in compiler/ that use f as the closure parameter, across 16 crates.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

f is tiny.

It's physically hard to see, and physically hard to click on or navigate to, even when the editor or view is actively highlighting it.

Calling or forwarding the task function is one of the most important points of control flow in these complex higher-order functions, and such an important point should not be made difficult to find.

This file has several methods that take a `FnOnce() -> R` closure:
- `DepGraph::with_ignore`
- `DepGraph::with_query_deserialization`
- `DepGraph::with_anon_task`
- `DepGraphData::with_anon_task_inner`

It also has two methods that take a faux closure via an `A` argument and
a `fn(TyCtxt<'tcx>, A) -> R` argument:
- DepGraph::with_task
- DepGraphData::with_task

The rationale is that the faux closure exercises tight control over what
state they have access to. This seems silly when (a) they are passed a
`TyCtxt`, and (b) when similar nearby functions take real closures. And
they are more awkward to use, e.g. requiring multiple arguments to be
gathered into a tuple. This commit changes the faux closures to real
closures.

The commit also changes the types and names of the closures from
the awkward `op: OP` to the more standard `f: F`.
@nnethercote nnethercote force-pushed the closure-consistency branch from ba1d61e to b9f0815 Compare March 18, 2026 04:30
@rust-bors
Copy link
Contributor

rust-bors bot commented Mar 22, 2026

☔ The latest upstream changes (presumably #154122) made this pull request unmergeable. Please resolve the merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants