Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f0adebc

Browse files
authoredJun 10, 2024
Rollup merge of #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 #125737
2 parents b595078 + 251d2d0 commit f0adebc

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed
 

‎compiler/rustc_infer/src/infer/error_reporting/note_and_explain.rs

+9
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
3232
diag.note("no two closures, even if identical, have the same type");
3333
diag.help("consider boxing your closure and/or using it as a trait object");
3434
}
35+
(ty::Coroutine(def_id1, ..), ty::Coroutine(def_id2, ..))
36+
if self.tcx.coroutine_is_async(def_id1)
37+
&& self.tcx.coroutine_is_async(def_id2) =>
38+
{
39+
diag.note("no two async blocks, even if identical, have the same type");
40+
diag.help(
41+
"consider pinning your async block and casting it to a trait object",
42+
);
43+
}
3544
(ty::Alias(ty::Opaque, ..), ty::Alias(ty::Opaque, ..)) => {
3645
// Issue #63167
3746
diag.note("distinct uses of `impl Trait` result in different opaque types");

‎tests/ui/async-await/coroutine-desc.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ LL | fun(async {}, async {});
1010
|
1111
= note: expected `async` block `{async block@$DIR/coroutine-desc.rs:10:9: 10:17}`
1212
found `async` block `{async block@$DIR/coroutine-desc.rs:10:19: 10:27}`
13+
= note: no two async blocks, even if identical, have the same type
14+
= help: consider pinning your async block and casting it to a trait object
1315
note: function defined here
1416
--> $DIR/coroutine-desc.rs:8:4
1517
|
@@ -51,6 +53,8 @@ LL | fun((async || {})(), (async || {})());
5153
|
5254
= note: expected `async` closure body `{async closure body@$DIR/coroutine-desc.rs:14:19: 14:21}`
5355
found `async` closure body `{async closure body@$DIR/coroutine-desc.rs:14:36: 14:38}`
56+
= note: no two async blocks, even if identical, have the same type
57+
= help: consider pinning your async block and casting it to a trait object
5458
note: function defined here
5559
--> $DIR/coroutine-desc.rs:8:4
5660
|

0 commit comments

Comments
 (0)
Failed to load comments.