rust-lang / rust-lang/rust

`async_closure`: error: implementation of `AsyncFnOnce` is not general enough

Open
#126,350 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-async-await AsyncAwait-Triaged C-bug T-compiler T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I'm not sure if this is really a bug (or perhaps a duplicate issue of another bug). However, I can't see any reason why this shouldn't work.

I tried this code:

#![feature(async_closure)]

fn assert_send<T: Send>(_: T) {}

#[derive(Clone)]
struct Ctxt<'a>(&'a ());

async fn commit_if_ok<'a>(ctxt: &mut Ctxt<'a>, f: impl async FnOnce(&mut Ctxt<'a>)) {
    f(&mut ctxt.clone()).await;
}

fn operation(mut ctxt: Ctxt<'_>) {
    assert_send(async {
        commit_if_ok(&mut ctxt, async |_| todo!()).await;
    });
}

fn main() {}

I expected to see this happen: Code compiles successfully

Instead, this happened: Compile error:

error: implementation of `AsyncFnOnce` is not general enough
  --> src/lib.rs:13:5
   |
13 | /     assert_send(async {
14 | |         commit_if_ok(&mut ctxt, async |_| todo!()).await;
15 | |     });
   | |______^ implementation of `AsyncFnOnce` is not general enough
   |
   = note: `{async closure@src/lib.rs:14:33: 14:42}` must implement `AsyncFnOnce<(&mut Ctxt<'1>,)>`, for any two lifetimes `'0` and `'1`...
   = note: ...but it actually implements `AsyncFnOnce<(&mut Ctxt<'_>,)>`

error: could not compile `playground` (lib) due to 1 previous error

(see https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=536db823e50d32307ee54645dbec25c6)

Meta

rustc --version --verbose:

rustc 1.80.0-nightly (032af18af 2024-06-02)
binary: rustc
commit-hash: 032af18af578f4283a2927fb43b90df2bbb72b67
commit-date: 2024-06-02
host: x86_64-unknown-linux-gnu
release: 1.80.0-nightly
LLVM version: 18.1.6

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 supplied async_closure example in src/lib.rs on the reported nightly compiler, using the playground link as the starting point. Trace the compiler handling for the async closure and AsyncFnOnce lifetime error. Done means the example compiles successfully while assert_send remains valid.

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
Active
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.