rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
96.38k stars 12.45k forks source link

Opaque error message for opaque type #93519

Open Geobert opened 2 years ago

Geobert commented 2 years ago

Given the following code: For this code

use anyhow;

fn main() {}

trait MyTrait {}

async fn endpoint() -> anyhow::Result<impl MyTrait> {
    if true {
        call_a().await
    } else {
        call_b().await
    }
}

async fn call_a() -> anyhow::Result<impl MyTrait> {
    todo!()
}

async fn call_b() -> anyhow::Result<impl MyTrait> {
    todo!()
}

The current output is:

error[E0308]: mismatched types
  --> src/main.rs:11:9
   |
11 |         call_b().await
   |         ^^^^^^^^^^^^^^ expected opaque type, found a different opaque type
...
15 | async fn call_a() -> anyhow::Result<impl MyTrait> {
   |                                     ------------ the expected opaque type
...
19 | async fn call_b() -> anyhow::Result<impl MyTrait> {
   |                                     ------------ the found opaque type
   |
   = note: expected enum `Result<impl MyTrait, _>` (opaque type at <src/main.rs:15:37>)
              found enum `Result<impl MyTrait, _>` (opaque type at <src/main.rs:19:37>)
   = note: distinct uses of `impl Trait` result in different opaque types

error[E0720]: cannot resolve opaque type
  --> src/main.rs:15:37
   |
15 | async fn call_a() -> anyhow::Result<impl MyTrait> {
   |                                     ^^^^^^^^^^^^ cannot resolve opaque type

error[E0720]: cannot resolve opaque type
  --> src/main.rs:19:37
   |
19 | async fn call_b() -> anyhow::Result<impl MyTrait> {
   |                                     ^^^^^^^^^^^^ cannot resolve opaque type

Ideally the output should look like: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=d937792ca9217c9556025f96e5574df9

I was asked to create a ticket to improve the compiler when the Result type of a Future is an impl Trait after asking the question on the forum: https://users.rust-lang.org/t/another-distinct-uses-of-impl-trait-result-in-different-opaque-types/71136

compiler-errors commented 2 years ago

I'm not sure how this error message can be improved. @Geobert, what exactly do you want to see different about this error message? You mentioned that other playground as what "ideally the output should look like", but I'm not sure what can be taken from that suggestion.

Geobert commented 2 years ago

I think the point was to have a suggestion to help solving the issue? As I didn't quite understood the issue in the first place, I just followed the suggestion to create a ticket here, but if it's silly, you can close it :)