rust-lang / rust-lang/rust

Cannot combine `async fn`-in-traits with `AsyncFn{,Mut}` closures

Open
#145,127 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-async-await A-async-closures A-auto-traits A-higher-ranked C-bug fixed-by-higher-ranked-assumptions
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

This program:

#![allow(dead_code)]

trait Foo {
    fn foo(&self) -> impl Future<Output = ()> + Send + '_;
}

struct MyType;

impl Foo for MyType {
    async fn foo(&self) {
        self.run_async_fn(async || self.noop()).await;
    }
}

impl MyType {
    fn noop(&self) {}

    async fn run_async_fn<T>(&self, mut f: impl AsyncFnMut() -> T) -> T {
        f().await
    }
}

fails to compile with:

error[E0477]: the type `{async closure@src/lib.rs:11:27: 11:35}` does not fulfill the required lifetime
  --> src/lib.rs:10:5
   |
10 |     async fn foo(&self) {
   |     ^^^^^^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0477`.

A similar version of this program with a dyn-compatible trait also does not compile:

#![allow(dead_code)]

trait Foo {
    fn foo<'a>(&'a self) -> Box<dyn Future<Output = ()> + Send + 'a>;
}

struct MyType;

impl Foo for MyType {
    fn foo<'a>(&'a self) -> Box<dyn Future<Output = ()> + Send + 'a> {
        Box::new(async move {
            self.run_async_fn(async || self.noop()).await;
        })
    }
}

impl MyType {
    fn noop(&self) {}

    async fn run_async_fn<T>(&self, mut f: impl AsyncFnMut() -> T) -> T {
        f().await
    }
}

and this fails with a different error:

error: implementation of `Send` is not general enough
  --> src/lib.rs:11:9
   |
11 | /         Box::new(async move {
12 | |             self.run_async_fn(async || self.noop()).await;
13 | |         })
   | |__________^ implementation of `Send` is not general enough
   |
   = note: `Send` would have to be implemented for the type `&'0 MyType`, for any lifetime `'0`...
   = note: ...but `Send` is actually implemented for the type `&'1 MyType`, for some specific lifetime `'1`

My instinct is that both of these programs should compile. Replacing AsyncFnMut with AsyncFn yields similar errors, but replacing the bound with AsyncFnOnce makes these errors go away. This feels like a bug in the compiler so I wanted to be sure to document this, but if I'm also misunderstanding things it might be good to also improve the error message here as a diagnostics issue.

Meta

rustc --version --verbose:

rustc 1.91.0-nightly (7d82b83ed 2025-08-06)
binary: rustc
commit-hash: 7d82b83ed57d188ab3f2441a765a6419685a88a3
commit-date: 2025-08-06
host: x86_64-unknown-linux-gnu
release: 1.91.0-nightly
LLVM version: 21.1.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the two playground examples from the issue with the reported rustc 1.91.0-nightly version and Rust 2024 edition. Compare the AsyncFnMut and AsyncFn cases with the AsyncFnOnce variant, then determine whether the expected result is successful compilation or a clearer diagnostic; done means the behavior is explained and the appropriate compiler outcome is covered.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.