-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Unhelpful error on mismatched types for anonymous futures #125737
Comments
This seems like a good suggestion. I'd suggest tweaking the wording a bit, since I don't know that the implications of async blocks having unique types are immediately obvious. Maybe something like this?
|
@rustbot labels +AsyncAwait-Triaged We discussed this in our async call and agreed that some improvement along these lines would be good. We could wordsmith it in the PR of course. (It'd be interesting too to add such diagnostics for closures and other similar cases.) |
For reference, here's the error message for mismatched
|
…lcnr Add explanatory note to async block type mismatch error The async block type mismatch error might leave the user wondering as to why it occurred. The new note should give them the needed context. Changes this diagnostic: ``` error[E0308]: mismatched types --> src/main.rs:5:23 | 2 | let a = async { 1 }; | ----------- the expected `async` block 3 | let b = async { 2 }; | ----------- the found `async` block 4 | 5 | let bad = vec![a, b]; | ^ expected `async` block, found a different `async` block | = note: expected `async` block `{async block@src/main.rs:2:13: 2:24}` found `async` block `{async block@src/main.rs:3:13: 3:24}` ``` to this: ``` error[E0308]: mismatched types --> src/main.rs:5:23 | 2 | let a = async { 1 }; | ----------- the expected `async` block 3 | let b = async { 2 }; | ----------- the found `async` block 4 | 5 | let bad = vec![a, b]; | ^ expected `async` block, found a different `async` block | = note: expected `async` block `{async block@src/main.rs:2:13: 2:24}` found `async` block `{async block@src/main.rs:3:13: 3:24}` = note: no two async blocks, even if identical, have the same type = help: consider pinning your async block and and casting it to a trait object ``` Fixes rust-lang#125737
Rollup merge of rust-lang#126215 - gurry:125737-bad-err-anon-futs, r=lcnr Add explanatory note to async block type mismatch error The async block type mismatch error might leave the user wondering as to why it occurred. The new note should give them the needed context. Changes this diagnostic: ``` error[E0308]: mismatched types --> src/main.rs:5:23 | 2 | let a = async { 1 }; | ----------- the expected `async` block 3 | let b = async { 2 }; | ----------- the found `async` block 4 | 5 | let bad = vec![a, b]; | ^ expected `async` block, found a different `async` block | = note: expected `async` block `{async block@src/main.rs:2:13: 2:24}` found `async` block `{async block@src/main.rs:3:13: 3:24}` ``` to this: ``` error[E0308]: mismatched types --> src/main.rs:5:23 | 2 | let a = async { 1 }; | ----------- the expected `async` block 3 | let b = async { 2 }; | ----------- the found `async` block 4 | 5 | let bad = vec![a, b]; | ^ expected `async` block, found a different `async` block | = note: expected `async` block `{async block@src/main.rs:2:13: 2:24}` found `async` block `{async block@src/main.rs:3:13: 3:24}` = note: no two async blocks, even if identical, have the same type = help: consider pinning your async block and and casting it to a trait object ``` Fixes rust-lang#125737
Code
Current output
Desired output
Rationale and extra context
Especially for people new to async, this message is extremely opaque. There is not actually enough information to understand it unless you already know (a) that async blocks produce an anonymous future type and (b) that regardless of the
Output
type, those futures are always unique and incompatible types. The message sort of implies this (“found a differentasync
block”), but it is not actionable in any way.It is pretty easy to get here by reaching for (to pick just one easy example)
futures::future::join_all
. I ended up stumbling across it in the process of building up some sample code for the new async chapter in the book, and I had a 🤨 moment at first!I have the time to help fix this, so if someone can point me in the right direction and help get it landed, I would be delighted to do the work.
Other cases
No response
Rust Version
Anything else?
No response
The text was updated successfully, but these errors were encountered: