Skip to content

Confusing error on mismatched argument count of function parameters #42855

Closed
@phil-opp

Description

@phil-opp

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.WG-diagnosticsWorking group: Diagnostics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions