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
This commit is contained in:
Matthias Krüger 2024-06-10 21:12:27 +02:00 committed by GitHub
commit f0adebc39d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 13 additions and 0 deletions

View file

@ -32,6 +32,15 @@ pub fn note_and_explain_type_err(
diag.note("no two closures, even if identical, have the same type");
diag.help("consider boxing your closure and/or using it as a trait object");
}
(ty::Coroutine(def_id1, ..), ty::Coroutine(def_id2, ..))
if self.tcx.coroutine_is_async(def_id1)
&& self.tcx.coroutine_is_async(def_id2) =>
{
diag.note("no two async blocks, even if identical, have the same type");
diag.help(
"consider pinning your async block and casting it to a trait object",
);
}
(ty::Alias(ty::Opaque, ..), ty::Alias(ty::Opaque, ..)) => {
// Issue #63167
diag.note("distinct uses of `impl Trait` result in different opaque types");

View file

@ -10,6 +10,8 @@ LL | fun(async {}, async {});
|
= note: expected `async` block `{async block@$DIR/coroutine-desc.rs:10:9: 10:17}`
found `async` block `{async block@$DIR/coroutine-desc.rs:10:19: 10:27}`
= note: no two async blocks, even if identical, have the same type
= help: consider pinning your async block and casting it to a trait object
note: function defined here
--> $DIR/coroutine-desc.rs:8:4
|
@ -51,6 +53,8 @@ LL | fun((async || {})(), (async || {})());
|
= note: expected `async` closure body `{async closure body@$DIR/coroutine-desc.rs:14:19: 14:21}`
found `async` closure body `{async closure body@$DIR/coroutine-desc.rs:14:36: 14:38}`
= note: no two async blocks, even if identical, have the same type
= help: consider pinning your async block and casting it to a trait object
note: function defined here
--> $DIR/coroutine-desc.rs:8:4
|