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

Confusing error on mismatched argument count of function parameters #42855

Closed
phil-opp opened this issue Jun 23, 2017 · 1 comment · Fixed by #64498
Closed

Confusing error on mismatched argument count of function parameters #42855

phil-opp opened this issue Jun 23, 2017 · 1 comment · Fixed by #64498
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. WG-diagnostics Working group: Diagnostics

Comments

@phil-opp
Copy link
Contributor

Example:

fn main() {
    bar(1, foo);
}

fn foo(a: u16) {}

fn bar<F>(a: u16, mut f: F) where F: FnMut(u16, u8) {
    f(a, 0);
}

The problem is the second argument in the bar call (foo) takes only one argument instead of the expected 2 arguments. However, the error looks like there is something wrong with the argument count of the bar call:

Error on nightly & beta:

error[E0593]: function takes 1 argument but 2 arguments are required
 --> <anon>:2:5
  |
2 |     bar(1, foo);
  |     ^^^ expected function that takes 2 arguments
  |
  = note: required by `bar`

Error on stable:

error[E0281]: type mismatch: the type `fn(u16) {foo}` implements the trait `std::ops::FnMut<(u16,)>`, but the trait `std::ops::FnMut<(u16, u8)>` is required (expected a tuple with 2 elements, found one with 1 elements)
 --> <anon>:2:5
  |
2 |     bar(1, foo);
  |     ^^^
  |
  = note: required by `bar`

Instead, the ^^^ should point to the faulty argument:

error[E0593]: function takes 1 argument but 2 arguments are required
 --> <anon>:2:5
  |
2 |     bar(1, foo);
  |            ^^^ expected function that takes 2 arguments
  |
  = note: required by `bar`

cc @oli-obk

@Mark-Simulacrum Mark-Simulacrum added the A-diagnostics Area: Messages for errors, warnings, and lints label Jun 23, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 26, 2017
@estebank estebank added the WG-diagnostics Working group: Diagnostics label Dec 10, 2017
bors added a commit that referenced this issue Dec 14, 2017
Point at whole method call instead of args

To avoid confusion in cases where the code is

```rust
fn foo() {}
/ foo(
|     bar()
|     ^^^ current diagnostics point here for arg count mismatch
| );
|_^ new diagnostic span points here
```

as this leads to confusion making people think that the diagnostic is
talking about `bar`'s arg count, not `foo`'s.

Point at `fn`s definition on arg mismatch, just like we do for closures.

Re #42855, Fix #45633.
@estebank
Copy link
Contributor

estebank commented Jan 26, 2018

Current output:

error[E0593]: function is expected to take 2 arguments, but it takes 1 argument
 --> src/main.rs:2:5
  |
2 |     bar(1, foo);
  |     ^^^ expected function that takes 2 arguments
...
5 | fn foo(a: u16) {}
  | -------------- takes 1 argument
  |
note: required by `bar`
 --> src/main.rs:7:1
  |
7 | fn bar<F>(a: u16, mut f: F) where F: FnMut(u16, u8) {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Ideally it should also point at the foo being passed as an argument and at the F: FnMut(u16, u8) restriction.

Centril added a commit to Centril/rust that referenced this issue Sep 16, 2019
On obligation errors point at the unfulfilled binding when possible

CC rust-lang#42855, rust-lang#64130, rust-lang#64135.
Centril added a commit to Centril/rust that referenced this issue Sep 19, 2019
On obligation errors point at the unfulfilled binding when possible

CC rust-lang#42855, rust-lang#64130, rust-lang#64135.
Centril added a commit to Centril/rust that referenced this issue Sep 20, 2019
When possible point at argument causing item obligation failure

Fix rust-lang#41781, fix rust-lang#42855, fix rust-lang#46658, fix rust-lang#48099, fix rust-lang#63143.
@bors bors closed this as completed in 7225264 Sep 20, 2019
bors added a commit that referenced this issue Sep 22, 2019
On obligation errors point at the unfulfilled binding when possible

CC #42855, #64130, #64135. Fix #61860.
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-enhancement Category: An issue proposing an enhancement or a PR with one. WG-diagnostics Working group: Diagnostics
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants