Skip to content

Suggest awaiting impl Future value being printed #159484

Description

@estebank

Code

async fn number() -> i32 {
    42
}

async fn print_number() {
    let number = number();
    println!("{number}");
}

Current output

error[E0277]: `impl Future<Output = i32>` doesn't implement `std::fmt::Display`
 --> src/main.rs:7:15
  |
7 |     println!("{number}");
  |               ^^^^^^^^ `impl Future<Output = i32>` cannot be formatted with the default formatter
  |
  = help: the trait `std::fmt::Display` is not implemented for `impl Future<Output = i32>`
  = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead

Desired output

error[E0277]: `impl Future<Output = i32>` doesn't implement `std::fmt::Display`
 --> src/main.rs:7:15
  |
7 |     println!("{number}");
  |               ^^^^^^^^ `impl Future<Output = i32>` cannot be formatted with the default formatter
  |
  = help: the trait `std::fmt::Display` is not implemented for `impl Future<Output = i32>`
  = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
help: consider `await`ing on the `Future`
  |
7 +     let number = number.await;
  |

Rationale and extra context

For E0308, we already suggest awaiting:

error[E0308]: mismatched types
 --> src/main.rs:7:18
  |
7 |     let _: i32 = number;
  |            ---   ^^^^^^ expected `i32`, found future
  |            |
  |            expected due to this
  |
note: calling an async function returns a future
 --> src/main.rs:7:18
  |
7 |     let _: i32 = number;
  |                  ^^^^^^
help: consider `await`ing on the `Future`
  |
7 |     let _: i32 = number.await;
  |                        ++++++

We should do the same on E0277 when possible.

Other cases

Rust Version

1.97

Anything else?

No response

Metadata

Metadata

Assignees

Labels

A-async-awaitArea: Async & AwaitA-diagnosticsArea: Messages for errors, warnings, and lintsA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)D-lack-of-suggestionDiagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic.D-papercutDiagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions