rust/compiler/rustc_middle/src
Matthias Krüger 73b022b8e1
Rollup merge of #107902 - vincenzopalazzo:macros/async_fn_suggestion, 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 https://github.com/rust-lang/rust/issues/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 https://github.com/rust-lang/rust/issues/80658

It remains to rework the case described in the following issue https://github.com/rust-lang/rust/issues/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 <vincenzopalazzodev@gmail.com>
2023-02-13 23:25:11 +01:00
..
dep_graph Simplify tls::enter_context. 2023-02-09 15:25:45 +11:00
hir Improve pretty-printing of HirIdValidator errors 2023-02-01 20:09:22 +01:00
infer Make folding traits generic over the Interner 2023-02-13 10:24:49 +00:00
middle rustc: Remove needless lifetimes 2022-12-20 22:10:40 +01:00
mir Make atomic structural impls generic over Interner 2023-02-13 10:24:50 +00:00
query Create a single value cache for the () query key 2023-02-11 23:38:01 +01:00
thir address review 2023-01-27 22:13:55 +01:00
traits Rename folder traits' tcx method to interner 2023-02-13 10:24:51 +00:00
ty Rollup merge of #107902 - vincenzopalazzo:macros/async_fn_suggestion, r=compiler-errors 2023-02-13 23:25:11 +01:00
util Add some docs to bug, span_bug and delay_span_bug 2022-12-30 16:47:56 +01:00
arena.rs Resolve documentation links in rustc and store the results in metadata 2023-02-10 09:34:13 +04:00
error.rs Match crate and slug names 2022-11-21 15:24:50 +01:00
lib.rs Move folding and visiting traits into type library 2023-02-13 10:24:50 +00:00
lint.rs create helper function for rustc_lint_defs::Level and remove it's duplicated code r=ozkanonur 2023-01-10 10:56:17 +03:00
macros.rs Move folding and visiting traits into type library 2023-02-13 10:24:50 +00:00
metadata.rs rustc: Parameterize ty::Visibility over used ID 2022-09-07 13:35:41 +04:00
tests.rs mv compiler to compiler/ 2020-08-30 18:45:07 +03:00
thir.rs output tree representation for thir-tree 2023-01-26 11:31:55 +01:00
values.rs add EarlyBinder::subst_identity; impl ParameterizedOverTcx (needed for rustc_metadata) and Value for EarlyBinder 2023-01-14 00:13:06 -07:00