rust-lang / rust-lang/rust

Extremely un-informative error when a future that capture the environment is used as Send

Open
#126,550 2 comments 0 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

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

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

fn trouble() -> impl Send {
    foo()
}
Current output
error[E0308]: mismatched types
  --> src/main.rs:12:5
   |
5  |       let updates = [async {}.then(|()| async move {
   |  _______________________________________-
6  | |         &THING
7  | |     })];
   | |     -
   | |     |
   | |_____the expected `async` block
   |       the found `async` block
...
12 |       foo()
   |       ^^^^^ one type is more general than the other
   |
   = note: expected `async` block `{async block@src/main.rs:5:39: 7:6}`
              found `async` block `{async block@src/main.rs:5:39: 7:6}`

For more information about this error, try `rustc --explain E0308`.
error: could not compile `test_project` (bin "test_project") due to 1 previous error
Desired output

No response

Rationale and extra context

No response

Other cases

No response

Rust Version
rustc 1.79.0 (129f3b996 2024-06-10)
binary: rustc
commit-hash: 129f3b9964af4d4a709d1383930ade12dfe7c081
commit-date: 2024-06-10
host: x86_64-unknown-linux-gnu
release: 1.79.0
LLVM version: 18.1.7

(Happens on nightly too)
Anything else?

The code compiles without the reference, so I'm guessing it's to do with the Future being made "unique" by that even though it doesn't actually contain any "real" captures that should make the closure different.

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 example from src/main.rs with the reported Rust 1.79.0 toolchain, then inspect the compiler diagnostic for the async closure and Send requirement. The issue provides no desired output, so first determine what actionable explanation the compiler can provide; done means replacing the opaque mismatched-types message with an informative diagnostic and covering the case with a regression test.

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
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.