Skip to content

Different error messages between async and regular functions #63277

Closed

Description

@Patryk27

Hi,

Let's consider following code:

fn foo() -> Result<bool, ()> {
    unimplemented!()
}

fn bar() -> Result<(), ()> {
    foo()?;
    ()
}

There's an error in the penultimate line - it should say Ok(()) and Rust correctly helps to spot the mistake:

  |
7 | fn bar() -> Result<(), ()> {
  |             -------------- expected `std::result::Result<(), ()>` because of return type
8 |     foo()?;
9 |     ()
  |     ^^ expected enum `std::result::Result`, found ()
  |
  = note: expected type `std::result::Result<(), ()>`
             found type `()`

The situation changes a bit when we switch to async functions:

#![feature(async_await)]

async fn foo() -> Result<bool, ()> {
    unimplemented!()
}

async fn bar() -> Result<(), ()> {
    foo().await?;
    ()
}

The error message now says the '?' operator can only be used in a function that returns 'Result' or 'Option', which is totally unhelpful, since the function is (or at least should be) returning a Result, according to its signature.

It seems that, when dispatching error messages, the actual return types have precedence over signatures in async functions, but it's exactly the other way around in regular functions, which leads to kinda unexpected and confusing error messages.

Activity

  1. Patryk27 commented on Aug 5, 2019

    @Patryk27
    ContributorAuthor

    For reference, it seems it's a duplicate of: #54326.

  2. cramertj commented on Aug 5, 2019

    @cramertj
    Member

    Closing as duplicate.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-async-awaitArea: Async & AwaitA-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