Skip to content

Wrong error message when Fn parameter doesn't match expected signature in call to function #45788

Closed
@mqudsi

Description

@mqudsi

A minimal complete test case:

fn takes_callback<F,T,R>(callback: F, t: T)
    where F: FnOnce(T) -> R
{
    callback(t);
}

fn callback(s1: &str, s2: &str) {
    println!("Hello {} {}", s1, s2);
}

fn main() {
    takes_callback(callback, "rust!");
}

Attempting to compile this under nightly gives the following:

mqudsi@ZBook /m/c/U/M/g/iMessageCore> rustc +nightly ./test.rs
error[E0593]: function is expected to take 1 argument, but it takes 2 arguments
  --> ./test.rs:12:5
   |
12 |     takes_callback(callback, "rust!");
   |     ^^^^^^^^^^^^^^ expected function that takes 1 argument
   |
   = note: required by `takes_callback`

error: aborting due to previous error

The error E0593 is correct, a function is expected to take 1 argument but it takes 2 arguments. But the "function" that is failing this expectation/test is not takes_callback but rather callback.

The error message "expected function that takes 1 argument" in the diagram below the error points to takes_callback which definitely was not expected to take only one argument (since it takes two arguments, FnOnce and T).

As best as I can tell, this error should be

mqudsi@ZBook /m/c/U/M/g/iMessageCore> rustc +nightly ./test.rs
error[E0593]: function is expected to take 1 argument, but it takes 2 arguments
  --> ./test.rs:12:5
   |
12 |     takes_callback(callback, "rust!");
   |                       ^^^^^ expected closure that takes 1 argument
   |
   = note: required by `takes_callback`

error: aborting due to previous error

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