Open
Description
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