-
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
improving error message when returning an opaque type inside an if else block #107899
Comments
…ggestion, r=compiler-errors fix: improve the suggestion on future not awaited Considering the following code ```rust fn foo() -> u8 { async fn async_fn() -> u8 { 22 } async_fn() } fn main() {} ``` the error generated before this commit from the compiler is ``` ➜ rust git:(macros/async_fn_suggestion) ✗ rustc test.rs --edition 2021 error[E0308]: mismatched types --> test.rs:4:5 | 1 | fn foo() -> u8 { | -- expected `u8` because of return type ... 4 | async_fn() | ^^^^^^^^^^ expected `u8`, found opaque type | = note: expected type `u8` found opaque type `impl Future<Output = u8>` help: consider `await`ing on the `Future` | 4 | async_fn().await | ++++++ error: aborting due to previous error ``` In this case the error is nor perfect, and can confuse the user that do not know that the opaque type is the future. So this commit will propose (and conclude the work start in rust-lang#80658) to change the string `opaque type` to `future` when applicable and also remove the Expected vs Received note by adding a more specific one regarding the async function that return a future type. So the new error emitted by the compiler is ``` error[E0308]: mismatched types --> test.rs:4:5 | 1 | fn foo() -> u8 { | -- expected `u8` because of return type ... 4 | async_fn() | ^^^^^^^^^^ expected `u8`, found future | note: calling an async function returns a future --> test.rs:4:5 | 4 | async_fn() | ^^^^^^^^^^ help: consider `await`ing on the `Future` | 4 | async_fn().await | ++++++ error: aborting due to previous error ``` Fixes rust-lang#80658 It remains to rework the case described in the following issue rust-lang#107899 but I think this deserves its own PR after we discuss a little bit how to handle these kinds of cases. r? `@eholk` `@rustbot` label +I-async-nominated Signed-off-by: Vincenzo Palazzo <[email protected]>
@rustbot label +E-mentor |
@rustbot claim |
@vincenzopalazzo Where do I add the helpful error message, a good starting point would be helpful |
@vincenzopalazzo This is my first contribution to the compiler so I am still getting used to a bit of the codebase, I can see some functions that construct messages about future in PR #107902, but I am not quite sure under which one would this new message come ? Sorry if the question does not make much sense, but I am still trying to understand the codebase |
https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc_middle/src/ty/error.rs#L95-L96 I found out that the error is being built from here and the if else block is here https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc_infer/src/infer/error_reporting/mod.rs#L801 I think I can understand the flow, the IfExpression cause is generated here, from where the error is constructed self.cause( I am wondering if the async type checks would be done just before this line so that the message could be changed
|
@rustbot claim |
When I was trying to run this with master I found that my #107902 now fixed also this? So closing as resolved, this is the error message that we want
|
Code
Current output
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:
Future<Output = ()>
is equal toFuture<Output = ()>
Other cases
No response
Anything else?
A possible solution for this can be:
You can not return two different futures inside an if-else block. rustc --explain ...
opaque type
string intofuture
string when applicableThe text was updated successfully, but these errors were encountered: