rust-lang / rust-lang/rust

higher-ranked lifetime error when using `AsyncFnMut`

Open
#151,398 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-async-closures A-higher-ranked C-bug T-compiler WG-async
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

use std::future::Future;

async fn spawn<F: Future + Send + 'static>(_: F)
where
    <F as Future>::Output: Send + 'static,
{
}

async fn a(mut input: String) {
    b(async |val| {
        input.pop();
        val + 1
    })
    .await;
}

async fn b(mut f: impl AsyncFnMut(i32) -> i32) {
    let out = f(6).await;
    dbg!(out);
}

async fn c() {
    spawn(a("xx".into())).await;
}

fn main() {}

I expected to see this happen: It compiles.

Instead, this happened: It fails to compile:

error: higher-ranked lifetime error
  --> src/main.rs:23:5
   |
23 |     spawn(a("xx".into())).await;
   |     ^^^^^^^^^^^^^^^^^^^^^
   |
   = note: could not prove `impl Future<Output = ()>: Send`

error: higher-ranked lifetime error
  --> src/main.rs:23:5
   |
23 |     spawn(a("xx".into())).await;
   |     ^^^^^^^^^^^^^^^^^^^^^
   |
   = note: could not prove `impl Future<Output = ()> well-formed`

error: higher-ranked lifetime error
  --> src/main.rs:23:27
   |
23 |     spawn(a("xx".into())).await;
   |                           ^^^^^
   |
   = note: could not prove `impl Future<Output = ()> well-formed`

error: higher-ranked lifetime error
  --> src/main.rs:23:27
   |
23 |     spawn(a("xx".into())).await;
   |                           ^^^^^
   |
   = note: could not prove `<impl Future<Output = ()> as IntoFuture>::IntoFuture well-formed`

error: higher-ranked lifetime error
  --> src/main.rs:23:27
   |
23 |     spawn(a("xx".into())).await;
   |                           ^^^^^
   |
   = note: could not prove `&mut impl Future<Output = ()> well-formed`

error: higher-ranked lifetime error
  --> src/main.rs:23:27
   |
23 |     spawn(a("xx".into())).await;
   |                           ^^^^^
   |
   = note: could not prove `Pin<&mut impl Future<Output = ()>> well-formed`

error: higher-ranked lifetime error
  --> src/main.rs:23:27
   |
23 |     spawn(a("xx".into())).await;
   |                           ^^^^^
   |
   = note: could not prove `Poll<<impl Future<Output = ()> as Future>::Output> well-formed`

error: could not compile `playground` (bin "playground") due to 7 previous errors
Meta

rustc --version --verbose:

rustc 1.92.0 (ded5c06cf 2025-12-08)
rustc 1.94.0-nightly (31cd367b9 2026-01-08)

I looked for prior art but didn't find it. Curiously I encountered this while working on a larger project and the code was more complex, the error I got was also in the invocation of spawn but it said "std::marker::Send would have to be implemented for the type &'0 bool, for any lifetime '0". So I'm not sure I fully reproduced the issue.

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

Start by compiling the minimal reproducer with rustc 1.92.0 and nightly 1.94.0, focusing on the spawn(a("xx".into())).await call and its AsyncFnMut bound. Investigate the higher-ranked lifetime diagnostics and Send well-formedness notes; done means the reproducer compiles as expected or produces a correct, actionable diagnostic.

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
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.