Skip to content

Conversation

@heathdutton
Copy link
Contributor

@heathdutton heathdutton commented Jan 1, 2026

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

@rustbot rustbot added 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 Jan 1, 2026
@rustbot
Copy link
Collaborator

rustbot commented Jan 1, 2026

r? @mati865

rustbot has assigned @mati865.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot

This comment has been minimized.

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.
@heathdutton heathdutton force-pushed the issue-150174-asyncfn-diagnostic-v2 branch from d65f71a to 9ef09ef Compare January 2, 2026 03:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

E0507: AsyncFn vs AsyncFnOnce not given as cause of error, unlike Fn vs FnOnce

3 participants