Skip to content

async closure does not implement FnMut because it captures state from its environment for async move closures capturing copyable values, while closures returning async move blocks work fine #140403

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ProbstDJakob opened this issue Apr 28, 2025 · 0 comments
Labels
A-async-closures `async || {}` C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ProbstDJakob
Copy link

async move closures do not implement FnMut (and therefore also not Fn) when capturing a copyable value, but closures returning a async move block do.

I tried this code:

fn main() {}

fn needs_fn_mut<T, F: FnMut() -> T>(mut f: F) -> T {
    (f)()
}

fn hello() -> impl Future<Output = u32> {
    let copyable = 0;

    needs_fn_mut(async move || { copyable })
}
Same with Fn

fn main() {}

fn needs_fn<T, F: Fn() -> T>(f: F) -> T {
    (f)()
}

fn hello() -> impl Future<Output = u32> {
    let copyable = 0;

    needs_fn(async move || { copyable })
}

Working code with async move blocks:

fn main() {}

fn needs_fn_mut<T, F: FnMut() -> T>(mut f: F) -> T {
    (f)()
}

fn hello() -> impl Future<Output = u32> {
    let copyable = 0;

    needs_fn_mut(|| async move { copyable })
}
Same with Fn

fn main() {}

fn needs_fn<T, F: Fn() -> T>(f: F) -> T {
    (f)()
}

fn hello() -> impl Future<Output = u32> {
    let copyable = 0;

    needs_fn(|| async move { copyable })
}

Meta

rustc --version --verbose:

rustc 1.86.0 (05f9846f8 2025-03-31)
binary: rustc
commit-hash: 05f9846f893b09a1be1fc8560e33fc3c815cfecb
commit-date: 2025-03-31
host: x86_64-unknown-linux-gnu
release: 1.86.0
LLVM version: 19.1.7
Backtrace

error: async closure does not implement `FnMut` because it captures state from its environment
  --> <anon>:10:18
   |
10 |     needs_fn_mut(async move || { copyable })
   |     ------------ ^^^^^^^^^^^^^
   |     |
   |     required by a bound introduced by this call
   |
note: required by a bound in `needs_fn_mut`
  --> <anon>:3:23
   |
3  | fn needs_fn_mut<T, F: FnMut() -> T>(mut f: F) -> T {
   |                       ^^^^^^^^^^^^ required by this bound in `needs_fn_mut`

error: aborting due to 1 previous error

This issue may be linked to #125247.

@ProbstDJakob ProbstDJakob added the C-bug Category: This is a bug. label Apr 28, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Apr 28, 2025
@jieyouxu jieyouxu added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-async-closures `async || {}` labels Apr 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-async-closures `async || {}` C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants