Rollup merge of #82165 - nellshamrell:nell/fix-80658-B, r=estebank

Reword labels on E0308 involving async fn return type

Fix for #80658.

When someone writes code like this:

```rust
fn foo() -> u8 {
    async fn async_fn() -> () {}

    async_fn()
}
```

And they try to compile it, they will see an error that looks like this:

```bash
error[E0308]: mismatched types
 --> test.rs:4:5
  |
1 | fn foo() -> u8 {
  |             -- expected `u8` because of return type
2 |     async fn async_fn() -> () {}
  |                            -- checked the `Output` of this `async fn`, found opaque type
3 |
4 |     async_fn()
  |     ^^^^^^^^^^ expected `u8`, found opaque type
  |
  = note: while checking the return type of this `async fn`
  = note:     expected type `u8`
          found opaque type `impl Future`
```
This commit is contained in:
Guillaume Gomez 2021-02-26 15:52:30 +01:00 committed by GitHub
commit a56bbb134f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 49 additions and 22 deletions

View File

@ -1484,13 +1484,16 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
for (key, values) in types.iter() {
let count = values.len();
let kind = key.descr();
let mut returned_async_output_error = false;
for sp in values {
err.span_label(
*sp,
format!(
"{}{}{} {}{}",
if sp.is_desugaring(DesugaringKind::Async) {
"the `Output` of this `async fn`'s "
if sp.is_desugaring(DesugaringKind::Async)
&& !returned_async_output_error
{
"checked the `Output` of this `async fn`, "
} else if count == 1 {
"the "
} else {
@ -1502,6 +1505,12 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
pluralize!(count),
),
);
if sp.is_desugaring(DesugaringKind::Async)
&& returned_async_output_error == false
{
err.note("while checking the return type of the `async fn`");
returned_async_output_error = true;
}
}
}
}

View File

@ -2,11 +2,12 @@ error[E0308]: mismatched types
--> $DIR/dont-suggest-missing-await.rs:14:18
|
LL | async fn make_u32() -> u32 {
| --- the `Output` of this `async fn`'s found opaque type
| --- checked the `Output` of this `async fn`, found opaque type
...
LL | take_u32(x)
| ^ expected `u32`, found opaque type
|
= note: while checking the return type of the `async fn`
= note: expected type `u32`
found opaque type `impl Future`
help: consider `await`ing on the `Future`

View File

@ -13,13 +13,15 @@ error[E0308]: mismatched types
--> $DIR/generator-desc.rs:12:16
|
LL | async fn one() {}
| - the `Output` of this `async fn`'s expected opaque type
| - checked the `Output` of this `async fn`, expected opaque type
LL | async fn two() {}
| - the `Output` of this `async fn`'s found opaque type
| - checked the `Output` of this `async fn`, found opaque type
...
LL | fun(one(), two());
| ^^^^^ expected opaque type, found a different opaque type
|
= note: while checking the return type of the `async fn`
= note: while checking the return type of the `async fn`
= note: expected opaque type `impl Future` (opaque type at <$DIR/generator-desc.rs:5:16>)
found opaque type `impl Future` (opaque type at <$DIR/generator-desc.rs:6:16>)
= help: consider `await`ing on both `Future`s

View File

@ -56,7 +56,7 @@ async fn struct_() -> Struct {
}
async fn tuple() -> Tuple {
//~^ NOTE the `Output` of this `async fn`'s expected opaque type
//~^ NOTE checked the `Output` of this `async fn`, expected opaque type
Tuple(1i32)
}
@ -92,6 +92,7 @@ async fn match_() {
Tuple(_) => {} //~ ERROR mismatched types
//~^ NOTE expected opaque type, found struct `Tuple`
//~| NOTE expected opaque type `impl Future`
//~| NOTE while checking the return type of the `async fn`
}
}

View File

@ -61,11 +61,12 @@ error[E0308]: mismatched types
--> $DIR/issue-61076.rs:92:9
|
LL | async fn tuple() -> Tuple {
| ----- the `Output` of this `async fn`'s expected opaque type
| ----- checked the `Output` of this `async fn`, expected opaque type
...
LL | Tuple(_) => {}
| ^^^^^^^^ expected opaque type, found struct `Tuple`
|
= note: while checking the return type of the `async fn`
= note: expected opaque type `impl Future`
found struct `Tuple`
help: consider `await`ing on the `Future`

View File

@ -2,11 +2,12 @@ error[E0308]: mismatched types
--> $DIR/suggest-missing-await-closure.rs:16:18
|
LL | async fn make_u32() -> u32 {
| --- the `Output` of this `async fn`'s found opaque type
| --- checked the `Output` of this `async fn`, found opaque type
...
LL | take_u32(x)
| ^ expected `u32`, found opaque type
|
= note: while checking the return type of the `async fn`
= note: expected type `u32`
found opaque type `impl Future`
help: consider `await`ing on the `Future`

View File

@ -2,11 +2,12 @@ error[E0308]: mismatched types
--> $DIR/suggest-missing-await.rs:12:14
|
LL | async fn make_u32() -> u32 {
| --- the `Output` of this `async fn`'s found opaque type
| --- checked the `Output` of this `async fn`, found opaque type
...
LL | take_u32(x)
| ^ expected `u32`, found opaque type
|
= note: while checking the return type of the `async fn`
= note: expected type `u32`
found opaque type `impl Future`
help: consider `await`ing on the `Future`
@ -18,11 +19,12 @@ error[E0308]: mismatched types
--> $DIR/suggest-missing-await.rs:22:5
|
LL | async fn dummy() {}
| - the `Output` of this `async fn`'s found opaque type
| - checked the `Output` of this `async fn`, found opaque type
...
LL | dummy()
| ^^^^^^^ expected `()`, found opaque type
|
= note: while checking the return type of the `async fn`
= note: expected unit type `()`
found opaque type `impl Future`
help: consider `await`ing on the `Future`

View File

@ -189,9 +189,10 @@ LL | async fn ft1();
LL | async fn ft1() {}
| ^
| |
| the `Output` of this `async fn`'s found opaque type
| checked the `Output` of this `async fn`, found opaque type
| expected `()`, found opaque type
|
= note: while checking the return type of the `async fn`
= note: expected fn pointer `fn()`
found fn pointer `fn() -> impl Future`
@ -204,9 +205,10 @@ LL | const async unsafe extern "C" fn ft5();
LL | const async unsafe extern "C" fn ft5() {}
| ^
| |
| the `Output` of this `async fn`'s found opaque type
| checked the `Output` of this `async fn`, found opaque type
| expected `()`, found opaque type
|
= note: while checking the return type of the `async fn`
= note: expected fn pointer `unsafe extern "C" fn()`
found fn pointer `unsafe extern "C" fn() -> impl Future`

View File

@ -53,9 +53,10 @@ LL | async fn associated();
LL | async fn associated();
| ^
| |
| the `Output` of this `async fn`'s found opaque type
| checked the `Output` of this `async fn`, found opaque type
| expected `()`, found opaque type
|
= note: while checking the return type of the `async fn`
= note: expected fn pointer `fn()`
found fn pointer `fn() -> impl Future`

View File

@ -17,8 +17,9 @@ LL | | }
::: $DIR/auxiliary/issue-81839.rs:6:49
|
LL | pub async fn answer_str(&self, _s: &str) -> Test {
| ---- the `Output` of this `async fn`'s found opaque type
| ---- checked the `Output` of this `async fn`, found opaque type
|
= note: while checking the return type of the `async fn`
= note: expected type `()`
found opaque type `impl Future`

View File

@ -13,9 +13,9 @@ fn extra_semicolon() {
};
}
async fn async_dummy() {} //~ NOTE the `Output` of this `async fn`'s found opaque type
async fn async_dummy2() {} //~ NOTE the `Output` of this `async fn`'s found opaque type
//~^ NOTE the `Output` of this `async fn`'s found opaque type
async fn async_dummy() {} //~ NOTE checked the `Output` of this `async fn`, found opaque type
async fn async_dummy2() {} //~ NOTE checked the `Output` of this `async fn`, found opaque type
//~| NOTE checked the `Output` of this `async fn`, found opaque type
async fn async_extra_semicolon_same() {
let _ = match true { //~ NOTE `match` arms have incompatible types
@ -26,6 +26,7 @@ async fn async_extra_semicolon_same() {
false => async_dummy(), //~ ERROR `match` arms have incompatible types
//~^ NOTE expected `()`, found opaque type
//~| NOTE expected type `()`
//~| NOTE while checking the return type of the `async fn`
//~| HELP consider `await`ing on the `Future`
};
}
@ -39,6 +40,7 @@ async fn async_extra_semicolon_different() {
false => async_dummy2(), //~ ERROR `match` arms have incompatible types
//~^ NOTE expected `()`, found opaque type
//~| NOTE expected type `()`
//~| NOTE while checking the return type of the `async fn`
//~| HELP consider `await`ing on the `Future`
};
}
@ -51,6 +53,7 @@ async fn async_different_futures() {
//~^ NOTE expected opaque type, found a different opaque type
//~| NOTE expected type `impl Future`
//~| NOTE distinct uses of `impl Trait` result in different opaque types
//~| NOTE while checking the return type of the `async fn`
};
}

View File

@ -2,7 +2,7 @@ error[E0308]: `match` arms have incompatible types
--> $DIR/match-prev-arm-needing-semi.rs:26:18
|
LL | async fn async_dummy() {}
| - the `Output` of this `async fn`'s found opaque type
| - checked the `Output` of this `async fn`, found opaque type
...
LL | let _ = match true {
| _____________-
@ -18,6 +18,7 @@ LL | |
LL | | };
| |_____- `match` arms have incompatible types
|
= note: while checking the return type of the `async fn`
= note: expected type `()`
found opaque type `impl Future`
help: consider `await`ing on the `Future`
@ -30,10 +31,10 @@ LL | async_dummy()
| --
error[E0308]: `match` arms have incompatible types
--> $DIR/match-prev-arm-needing-semi.rs:39:18
--> $DIR/match-prev-arm-needing-semi.rs:40:18
|
LL | async fn async_dummy2() {}
| - the `Output` of this `async fn`'s found opaque type
| - checked the `Output` of this `async fn`, found opaque type
...
LL | let _ = match true {
| _____________-
@ -49,6 +50,7 @@ LL | |
LL | | };
| |_____- `match` arms have incompatible types
|
= note: while checking the return type of the `async fn`
= note: expected type `()`
found opaque type `impl Future`
help: consider `await`ing on the `Future`
@ -64,10 +66,10 @@ LL | false => Box::new(async_dummy2()),
|
error[E0308]: `match` arms have incompatible types
--> $DIR/match-prev-arm-needing-semi.rs:50:18
--> $DIR/match-prev-arm-needing-semi.rs:52:18
|
LL | async fn async_dummy2() {}
| - the `Output` of this `async fn`'s found opaque type
| - checked the `Output` of this `async fn`, found opaque type
...
LL | let _ = match true {
| _____________-
@ -81,6 +83,7 @@ LL | |
LL | | };
| |_____- `match` arms have incompatible types
|
= note: while checking the return type of the `async fn`
= note: expected type `impl Future` (opaque type at <$DIR/match-prev-arm-needing-semi.rs:16:24>)
found opaque type `impl Future` (opaque type at <$DIR/match-prev-arm-needing-semi.rs:17:25>)
= note: distinct uses of `impl Trait` result in different opaque types