rust-lang / rust-lang/rust

Poor errors from type-mismatched early return when the return type is `impl Future`

Open
#162,271 1 comment 0 reactions 1 assignee View on GitHub

@Hamdan-Khan is already working on this.

Since Sep 4, 2026.

A-diagnostics T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
pub fn sort_of_async_function() -> impl Future<Output = ()> {
    if false {
        return;
    }
    
    async {}
}
Current output
error[E0277]: `()` is not a future
 --> src/lib.rs:1:36
  |
1 | pub fn sort_of_async_function() -> impl Future<Output = ()> {
  |                                    ^^^^^^^^^^^^^^^^^^^^^^^^ `()` is not a future
  |
  = help: the trait `Future` is not implemented for `()`

error[E0308]: mismatched types
 --> src/lib.rs:6:5
  |
6 |     async {}
  |     ^^^^^^^^ expected `()`, found `async` block
  |
  = note:  expected unit type `()`
          found `async` block `{async block@src/lib.rs:6:5: 6:10}`
note: return type inferred to be `()` here
 --> src/lib.rs:3:9
  |
3 |         return;
  |         ^^^^^^
help: you might have meant to return this value
  |
6 |     return async {};
  |     ++++++         +
help: you might have meant to return this value
  |
6 |     return async {};
  |     ++++++         +

error[E0277]: `()` is not a future
 --> src/lib.rs:6:5
  |
6 |     async {}
  |     ^^^^^^^^ `()` is not a future
  |
  = help: the trait `Future` is not implemented for `()`
Desired output
error[E0277]: `()` is not a future
 --> src/lib.rs:1:36
  |
3 |         return;
  |         ^^^^^^ `()` is not a future
  |
  = help: the trait `Future` is not implemented for `()`

error[E0308]: mismatched types
 --> src/lib.rs:6:5
  |
6 |     async {}
  |     ^^^^^^^^ expected `()`, found `async` block
  |
  = note:  expected unit type `()`
          found `async` block `{async block@src/lib.rs:6:5: 6:10}`
note: return type inferred to be `()` here
 --> src/lib.rs:3:9
  |
3 |         return;
  |         ^^^^^^
Rationale and extra context

The problems with the current output are:

  • It prints “() is not a future” twice.
  • Neither of those diagnostics point to the source of the (), and one of them points to the return value that is not of type ().
  • The suggestion of adding a return to the block’s tail expression is not only unhelpful, but may mislead readers about how Rust works.
Other cases

Using a different trait than Future produces very different, and generally more sensible, results.

Rust Version
1.100.0-nightly (2026-09-02 2e2b193f8ada105f2760)

Also, stable 1.98.1 has a simpler but worse diagnostic output: it fails to mention the early return at all.

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.