rust-lang / rust-lang/rust

Async code causes error "implementation of `FnOnce` is not general enough" when demanding an `impl Send`

Open
#126,551 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-async-await A-diagnostics AsyncAwait-Triaged D-confusing T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Credit to @Ciel-MC for discovering this.

I tried this code:

use futures::prelude::future::{join_all, FutureExt};

fn foo_is_send() -> impl Send {
    foo()
}

static THING: () = ();

async fn foo() {
    let updates = [async { &THING }.then(|_| async {})];
    join_all(updates).await;
}

I expected the code to compile without errors. Instead, I got the following compile error:

   Compiling playground v0.0.1 (/playground)
error: implementation of `FnOnce` is not general enough
 --> src/lib.rs:4:5
  |
4 |     foo()
  |     ^^^^^ implementation of `FnOnce` is not general enough
  |
  = note: closure with signature `fn(&'0 ()) -> {async block@src/lib.rs:10:46: 10:54}` must implement `FnOnce<(&'1 (),)>`, for any two lifetimes `'0` and `'1`...
  = note: ...but it actually implements `FnOnce<(&(),)>`

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

Possibly related to #126044

See also #126550, which has similar reproduction code.

Meta

Tested on the playground with stable (1.79.0) and nightly (1.81.0-nightly (2024-06-15 3cf924b934322fd7b514))

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 failure from src/lib.rs, starting with foo_is_send and the async foo example, and compare the reported behavior with related issues #126044 and #126550. Done means the reproduction compiles without the “FnOnce is not general enough” error when foo is required to return impl Send.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.