Skip to content

Slightly incorrect help message with mismatched typs in async code #66910

Closed

Description

@jfrimmel

The following works, since foo and bar return (), so the expression is simply returned:

fn foo() {}
fn bar() {
    foo()
}

The "equivalent" code in the async context yields a compiler error:

async fn foo() {}
async fn bar() {
    foo()
}
error[E0308]: mismatched types
  --> src/main.rs:62:5
   |
62 |     foo()
   |     ^^^^^- help: try adding a semicolon: `;`
   |     |
   |     expected (), found opaque type
   |
   = note: expected type `()`
              found type `impl core::future::future::Future`

The help message would be correct for non-async-code with mismatched types, but in the async case a .await should be suggested.

Technically the current help message is correct, but leads to another warning (that can be even overlooked):

warning: unused implementer of `core::future::future::Future` that must be used
  --> src/main.rs:62:5
   |
62 |     foo();
   |     ^^^^^^
   |
   = note: `#[warn(unused_must_use)]` on by default
   = note: futures do nothing unless you `.await` or poll them

So you have to do a second compile iteration.

I'd suggest to spawn a help message like that:

error[E0308]: mismatched types
  --> src/main.rs:62:5
   |
62 |     foo()
   |     ^^^^^- help: try to await the future: `.await`
   |     |
   |     expected (), found opaque type
   |
   = note: expected type `()`
              found type `impl core::future::future::Future`

Activity

  1. jfrimmel commented on Nov 30, 2019

    @jfrimmel
    ContributorAuthor

    Note: this is the message with the current nightly (rustc 1.41.0-nightly (25d8a94 2019-11-29)).
    Rust 1.39 does not even spawn the help message

  2. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    C-enhancementCategory: An issue proposing an enhancement or a PR with one.
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Nov 30, 2019
  3. nikomatsakis commented on Dec 3, 2019

    @nikomatsakis
    Contributor

    Async await traige: marking as on-deck, as suggesting the use of .await seems pretty useful and general

  4. Aaron1011 commented on Dec 3, 2019

    @Aaron1011
    Contributor

    I started working on this, then realized that it already exists: #62067

    However, it looks like we don't currently attempt to emit suggestions for certain coercions (including the return type coercion), which is why it doesn't show up here. I'm working on emitting coercion suggestions in more places.

  5. Aaron1011 commented on Dec 3, 2019

    @Aaron1011
    Contributor

    @rustbot claim

  6. added a commit that references this issue on Dec 4, 2019
    462f06d
  7. added a commit that references this issue on Dec 6, 2019
    99fee78
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

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.C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-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