Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suboptimal error reporting of invalid argument to generic function #36775

Closed
Seeker14491 opened this issue Sep 27, 2016 · 1 comment · Fixed by #64498
Closed

Suboptimal error reporting of invalid argument to generic function #36775

Seeker14491 opened this issue Sep 27, 2016 · 1 comment · Fixed by #64498
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Seeker14491
Copy link
Contributor

The following code produces two compilation errors.

use std::fmt::Display;

fn main() {
    let _ = Vec::with_capacity("0"); // error #1

    displayer(()); // error #2
}

fn displayer<T: Display>(t: T) {
    println!("{}", t);
}

Error 1

error[E0308]: mismatched types
 --> <anon>:4:32
  |
4 |     let _ = Vec::with_capacity("0"); // error #1
  |                                ^^^ expected usize, found reference
  |
  = note: expected type `usize`
  = note:    found type `&'static str`

The first error looks great. The compiler points at the erroneous argument.

Error 2

error[E0277]: the trait bound `(): std::fmt::Display` is not satisfied
 --> <anon>:6:5
  |
6 |     displayer(()); // error #2
  |     ^^^^^^^^^ trait `(): std::fmt::Display` not satisfied
  |
  = note: `()` cannot be formatted with the default formatter; try using `:?` instead if you are using a format string
  = note: required by `displayer`

In the second error, however, the compiler points at the function instead of the erroneous argument. This is no big problem in this case, as the function only takes one argument, and the argument has a very simple type. But in more complex functions, it might be hard to figure out which argument is problematic.

@TimNN TimNN added the A-diagnostics Area: Messages for errors, warnings, and lints label Sep 27, 2016
@steveklabnik steveklabnik added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 9, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-bug Category: This is a bug. label Jul 26, 2017
@estebank
Copy link
Contributor

Current output:

error[E0308]: mismatched types
 --> src/main.rs:4:32
  |
4 |     let _ = Vec::with_capacity("0"); // error #1
  |                                ^^^ expected usize, found reference
  |
  = note: expected type `usize`
             found type `&'static str`

error[E0277]: `()` doesn't implement `std::fmt::Display`
 --> src/main.rs:6:15
  |
6 |     displayer(()); // error #2
  |               ^^ `()` cannot be formatted with the default formatter
...
9 | fn displayer<T: Display>(t: T) {
  | ------------------------------ required by `displayer`
  |
  = 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

Fixed in #64498.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants