Skip to content

improving error message when returning an opaque type inside an if else block #107899

Closed
@vincenzopalazzo

Description

@vincenzopalazzo

Code

async fn async_dummy() {}

async fn async_dummy2() {}


fn main() {
    let _ = if true {
        async_dummy()
    } else {
        async_dummy2()
    };
}

Current output

Compiling playground v0.0.1 (/playground)
error[E0308]: `if` and `else` have incompatible types
  --> src/main.rs:10:9
   |
7  |       let _ = if true {
   |  _____________-
8  | |         async_dummy()
   | |         ------------- expected because of this
9  | |     } else {
10 | |         async_dummy2()
   | |         ^^^^^^^^^^^^^^ expected opaque type, found a different opaque type
11 | |     };
   | |_____- `if` and `else` have incompatible types

Desired output

No response

Rationale and extra context

We start a discussion on zulip regarding this that is available here where we noted that this suggestion is not helpful at all, but also we noted that it is difficult make a helpful suggestion in this case because there is the question that a user may ask to himself when reading the error message:

  1. What is it an opaque type?
  2. Future<Output = ()> is equal to Future<Output = ()>
  3. Why two futures are not equal?a

Other cases

No response

Anything else?

A possible solution for this can be:

  • Do not try to be too smart in some corner case like this, and in this case, I would like an error like You can not return two different futures inside an if-else block. rustc --explain ...
  • make a specific explanation on why we can not compare these two types
  • In case of the future generate a note that the user may want to call await on each branch
  • change the opaque type string into future string when applicable

Metadata

Metadata

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.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions