rust-lang / rust-lang/rust

Point at binding declaration on type and trait errors

Open
#124,933 1 comment 0 reactions 1 assignee View on GitHub

@cardigan1008 is already working on this.

Since May 9, 2024.

A-diagnostics T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
fn main() {
    let x = if true {
        "";
    } else {
        "";
    };
    println!("{x}");
    foo(x);
}
fn foo(_: &str) {
}
Current output
error[E0277]: `()` doesn't implement `std::fmt::Display`
 --> src/main.rs:7:15
  |
7 |     println!("{x}");
  |               ^^^ `()` cannot be formatted with the default formatter
  |
  = help: the trait `std::fmt::Display` is not implemented for `()`
  = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
  = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0308]: mismatched types
  --> src/main.rs:8:9
   |
8  |     foo(x);
   |     --- ^ expected `&str`, found `()`
   |     |
   |     arguments to this function are incorrect
   |
note: function defined here
  --> src/main.rs:10:4
   |
10 | fn foo(_: &str) {
   |    ^^^ -------
Desired output
error[E0277]: `()` doesn't implement `std::fmt::Display`
 --> src/main.rs:7:15
  |
2 |     let x = if true {
  |         - this binding has type `()`
...
7 |     println!("{x}");
  |               ^^^ `()` cannot be formatted with the default formatter
  |
  = help: the trait `std::fmt::Display` is not implemented for `()`
  = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
  = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0308]: mismatched types
  --> src/main.rs:8:9
   |
2  |     let x = if true {
   |         - this binding has type `()`
...
   |
8  |     foo(x);
   |     --- ^ expected `&str`, found `()`
   |     |
   |     arguments to this function are incorrect
   |
note: function defined here
  --> src/main.rs:10:4
   |
10 | fn foo(_: &str) {
   |    ^^^ -------
Rationale and extra context

This is specially useful for cases where a binding has the unit type (), because that signals some kind of mistake on the declaration, but it can be useful in general. It might be better to make it a note, so it isn't the first thing people see, but we can iterate on the output after seeing how it works in practice.

Other cases

No response

Rust Version
1.78
Anything else?

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.