Skip to content

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

Description

@kpreid

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions