Skip to content

Unhelpful error on mismatched types for anonymous futures #125737

Closed
@chriskrycho

Description

@chriskrycho

Code

fn main() {
    let a = async { 1 };
    let b = async { 2 };
    
    let bad = vec![a, b];
}

Current output

error[E0308]: mismatched types
 --> src/main.rs:5:23
  |
2 |     let a = async { 1 };
  |             ----------- the expected `async` block
3 |     let b = async { 2 };
  |             ----------- the found `async` block
4 |     
5 |     let bad = vec![a, b];
  |                       ^ expected `async` block, found a different `async` block
  |
  = note: expected `async` block `{async block@src/main.rs:2:13: 2:24}`
             found `async` block `{async block@src/main.rs:3:13: 3:24}`

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

Desired output

error[E0308]: mismatched types
 --> src/main.rs:5:23
  |
2 |     let a = async { 1 };
  |             ----------- the expected `async` block
3 |     let b = async { 2 };
  |             ----------- the found `async` block
4 |     
5 |     let bad = vec![a, b];
  |                       ^ <something better here!>
  |
  = note: expected `async` block `{async block@src/main.rs:2:13: 2:24}`
             found `async` block `{async block@src/main.rs:3:13: 3:30}`

  = note: the futures produced by async blocks always have unique types, even if
          they have the same output type.

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

Rationale and extra context

Especially for people new to async, this message is extremely opaque. There is not actually enough information to understand it unless you already know (a) that async blocks produce an anonymous future type and (b) that regardless of the Output type, those futures are always unique and incompatible types. The message sort of implies this (“found a different async block”), but it is not actionable in any way.

It is pretty easy to get here by reaching for (to pick just one easy example) futures::future::join_all. I ended up stumbling across it in the process of building up some sample code for the new async chapter in the book, and I had a 🤨 moment at first!

I have the time to help fix this, so if someone can point me in the right direction and help get it landed, I would be delighted to do the work.

Other cases

No response

Rust Version

rustc 1.77.1 (7cf61ebde 2024-03-27)
binary: rustc
commit-hash: 7cf61ebde7b22796c69757901dd346d0fe70bd97
commit-date: 2024-03-27
host: aarch64-apple-darwin
release: 1.77.1
LLVM version: 17.0.6

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-async-awaitArea: Async & AwaitA-diagnosticsArea: Messages for errors, warnings, and lintsAsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.D-papercutDiagnostics: An error or lint that needs small tweaks.E-help-wantedCall for participation: Help is requested to fix this issue.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.WG-asyncWorking group: Async & await

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions