Improve move error diagnostic for AsyncFn closures#150572
Merged
rust-bors[bot] merged 1 commit intorust-lang:mainfrom Jan 29, 2026
Merged
Improve move error diagnostic for AsyncFn closures#150572rust-bors[bot] merged 1 commit intorust-lang:mainfrom
AsyncFn closures#150572rust-bors[bot] merged 1 commit intorust-lang:mainfrom
Conversation
Collaborator
This comment has been minimized.
This comment has been minimized.
d65f71a to
9ef09ef
Compare
mati865
reviewed
Jan 5, 2026
Member
|
@rustbot reroll Hopefully another reviewer will have better knowledge about #150572 (comment) |
lcnr
reviewed
Jan 19, 2026
This comment was marked as outdated.
This comment was marked as outdated.
9ef09ef to
d6a8c72
Compare
This comment was marked as outdated.
This comment was marked as outdated.
This comment has been minimized.
This comment has been minimized.
48c7d9e to
0f81d39
Compare
Collaborator
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
lcnr
reviewed
Jan 27, 2026
lcnr
reviewed
Jan 27, 2026
0f81d39 to
5088318
Compare
lcnr
reviewed
Jan 28, 2026
Contributor
|
after nit @bors delegate+ |
Contributor
|
✌️ @heathdutton, you can now approve this pull request! If @lcnr told you to " |
When an async closure captures a variable by move but is constrained to `AsyncFn` or `AsyncFnMut`, the error message now explains that the closure kind is the issue and points to the trait bound, similar to the existing diagnostic for `Fn`/`FnMut` closures.
5088318 to
f5f2ca0
Compare
Contributor
Author
|
@bors r=lcnr |
Contributor
Zalathar
added a commit
to Zalathar/rust
that referenced
this pull request
Jan 29, 2026
…iagnostic-v2, r=lcnr
Improve move error diagnostic for `AsyncFn` closures
When an async closure captures a variable by move but is constrained to `AsyncFn` or `AsyncFnMut`, the error message now explains that the closure kind is the issue and points to the trait bound, similar to the existing diagnostic for `Fn`/`FnMut` closures.
**Before:**
```
error[E0507]: cannot move out of `foos` which is behind a shared reference
--> src/lib.rs:12:20
|
11 | async fn foo(foos: &mut [&mut Foo]) -> Result<(), ()> {
| ---- move occurs because `foos` has type...
12 | in_transaction(async || -> Result<(), ()> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ `foos` is moved here
13 | for foo in foos {
| ---- variable moved due to use in coroutine
```
**After:**
```
error[E0507]: cannot move out of `y`, a captured variable in an `AsyncFn` closure
--> src/lib.rs:9:10
|
LL | let y = vec![format!("World")];
| - captured outer variable
LL | call(async || {
| ^^^^^^^^ captured by this `AsyncFn` closure
...
help: `AsyncFn` and `AsyncFnMut` closures require captured values to be able
to be consumed multiple times, but `AsyncFnOnce` closures may consume
them only once
--> src/lib.rs:5:27
|
LL | fn call<F>(_: F) where F: AsyncFn() {}
| ^^^^^^^^^
```
Fixes rust-lang#150174
rust-bors bot
pushed a commit
that referenced
this pull request
Jan 29, 2026
Rollup of 8 pull requests Successful merges: - #150474 (Tidy: detect ui tests subdirectory changes so `tests/ui/README.md` stays in sync) - #150572 (Improve move error diagnostic for `AsyncFn` closures) - #151596 (Fix -Zmir-enable-passes to detect unregistered enum names in declare_passes macro) - #151802 (Make `QueryDispatcher::Qcx` an associated type) - #151559 ([rustdoc] Add a marker to tell users that there are hidden (deprecated) items in the search results) - #151665 (Fix contrast ratio for `Since` element in rustodoc dark theme) - #151798 (Update `askama` to `0.15.3`) - #151800 (Subscribe myself to translation diagnostics)
rust-bors bot
pushed a commit
that referenced
this pull request
Jan 29, 2026
Rollup of 12 pull requests Successful merges: - #150474 (Tidy: detect ui tests subdirectory changes so `tests/ui/README.md` stays in sync) - #150572 (Improve move error diagnostic for `AsyncFn` closures) - #151596 (Fix -Zmir-enable-passes to detect unregistered enum names in declare_passes macro) - #151802 (Make `QueryDispatcher::Qcx` an associated type) - #149110 (Implement `cast_slice` for raw pointer types) - #151559 ([rustdoc] Add a marker to tell users that there are hidden (deprecated) items in the search results) - #151665 (Fix contrast ratio for `Since` element in rustdoc dark theme) - #151785 (Stabilize feature(push_mut)) - #151798 (Update `askama` to `0.15.3`) - #151800 (Subscribe myself to translation diagnostics) - #151804 (Document, sort, and tweak spellcheck entries in `typos.toml`) - #151805 (Fix grammar in `env::current_exe()#Security`)
rust-timer
added a commit
that referenced
this pull request
Jan 29, 2026
Rollup merge of #150572 - heathdutton:issue-150174-asyncfn-diagnostic-v2, r=lcnr Improve move error diagnostic for `AsyncFn` closures When an async closure captures a variable by move but is constrained to `AsyncFn` or `AsyncFnMut`, the error message now explains that the closure kind is the issue and points to the trait bound, similar to the existing diagnostic for `Fn`/`FnMut` closures. **Before:** ``` error[E0507]: cannot move out of `foos` which is behind a shared reference --> src/lib.rs:12:20 | 11 | async fn foo(foos: &mut [&mut Foo]) -> Result<(), ()> { | ---- move occurs because `foos` has type... 12 | in_transaction(async || -> Result<(), ()> { | ^^^^^^^^^^^^^^^^^^^^^^^^^^ `foos` is moved here 13 | for foo in foos { | ---- variable moved due to use in coroutine ``` **After:** ``` error[E0507]: cannot move out of `y`, a captured variable in an `AsyncFn` closure --> src/lib.rs:9:10 | LL | let y = vec![format!("World")]; | - captured outer variable LL | call(async || { | ^^^^^^^^ captured by this `AsyncFn` closure ... help: `AsyncFn` and `AsyncFnMut` closures require captured values to be able to be consumed multiple times, but `AsyncFnOnce` closures may consume them only once --> src/lib.rs:5:27 | LL | fn call<F>(_: F) where F: AsyncFn() {} | ^^^^^^^^^ ``` Fixes #150174
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When an async closure captures a variable by move but is constrained to
AsyncFnorAsyncFnMut, the error message now explains that the closure kind is the issue and points to the trait bound, similar to the existing diagnostic forFn/FnMutclosures.Before:
After:
Fixes #150174