-
Notifications
You must be signed in to change notification settings - Fork 13k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Confusing error message with type unification of impl Traits #63167
Comments
How about?:
In the second case I think we should suggest that the cc @estebank |
This is perfect! It's the exact hint I needed to understand the problem :)
Agreed, this is definitely a bit trickier. I'll defer to you on the best way to message this error. |
Doesn't it already does so?
|
@Centril what is the correct nomenclature for a regular, non-opaque type that we could use here? With that we could say something like "consider materializing to an explicit type in both branches from the opaque type" and "for |
@estebank The antonym of opaque is transparent but I'm not sure that is situationally good to use here. ISTM you'd want to say something like "use an enum with variants for both cases" (at least that's the advice I'd give in person). I'd go for a non-actionable description as a first pass at this problem and consider an improvement later. |
Are opaque types the same as anonymous types in C#/Java? If so I think 'anonymous' is clearer than opaque (pardon the pun!). |
This is a real gotcha of rust and needs a great error message. We could show the definition sites of the opaque types so it's clear which type is expected in this position: Something like:
|
It doesn't matter where the As for "anonymous", the closest equivalent Java's anonymous classes, which conjure new types, in Rust are closures. Also, with Java's anonymous classes you can recover the type identity at runtime using |
Tweak E0308 on opaque types ``` error[E0308]: if and else have incompatible types --> file.rs:21:9 | 18 | / if true { 19 | | thing_one() | | ----------- expected because of this 20 | | } else { 21 | | thing_two() | | ^^^^^^^^^^^ expected opaque type, found a different opaque type 22 | | }.await | |_____- if and else have incompatible types | = note: expected type `impl std::future::Future` (opaque type) found type `impl std::future::Future` (opaque type) = note: distinct uses of `impl Trait` result in different opaque types = help: if both futures resolve to the same type, consider `await`ing on both of them ``` r? @Centril CC rust-lang#63167
I'll get used to
You're refering here to But if they weren't trying to bind both impl Traits to be the same type, then given both types textually look the same it would be great if the error message could point out the difference between those two types. Currently the error says:
As a nubie I need to see something different between the two types that I can get my teeth into.
Somehow it would be great to distinguish these two opaque types (or does the compiler at this point has no knowledge of where these two opaque types came from - only that they are not the same?). |
Fwiw, we desire to keep jargon to minimum. |
Seems like a nice improvement. |
Thanks Centril - hadn't seen that switch. If people are ok with adding line numbers to pinpoint the different impl Traits I'll have a go at rolling a PR? |
…ramertj Opaque type locations in error message for clarity. Attempts to fix rust-lang#63167
…ramertj,Centril Opaque type locations in error message for clarity. Attempts to fix rust-lang#63167
With all the various improvements that we've done, is there more we could do? or shall we close this issue for now? |
It took a colleague explaining to me that the return type of a function returning an impl Trait is unique across function definitions for me to understand the following error: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=4ceda076c82b01ea279e8864518ab5d9
This becomes more important for async/await because of the automatic translation of
async fn foo() -> i64
tofn foo() -> impl Future<Output = i64>
: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=b109446a49d22a632e395aacaa2cb8ffIn the first case, can the error maybe call out that the opaque types are different due to how the compiler treats them? And for the second error, can this be called out in async-specific language. That error happens when you forget to call
.await
.The text was updated successfully, but these errors were encountered: