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

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

Closed
mqudsi opened this issue Nov 5, 2017 · 4 comments
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

@mqudsi
Copy link
Contributor

mqudsi commented Nov 5, 2017

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
@mqudsi mqudsi changed the title "The function takes X arguments but N arguments is required" typo/clarification Wrong error message when Fn parameter doesn't match expected signature in call to function Nov 5, 2017
@mqudsi
Copy link
Contributor Author

mqudsi commented Nov 5, 2017

I updated the bug description to the real bug; the issue at hand was more subtle than I had realized when I first filed

@TimNN TimNN added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Nov 7, 2017
@estebank estebank added E-needs-mentor WG-diagnostics Working group: Diagnostics labels Dec 9, 2017
@estebank
Copy link
Contributor

estebank commented Dec 9, 2017

Proposed output:

error[E0593]: function is expected to take 1 argument, but it takes 2 arguments
  --> src/main.rs:12:5
   |
1  | fn takes_callback<F,T,R>(callback: F, t: T)
   |                          -------- expected function that takes 1 argument here...
2  |     where F: FnOnce(T) -> R
   |           ----------------- ...because of this binding
...
12 |     takes_callback(callback, "rust!");
   |     ^^^^^^^^^^^^^^ -------- this function takes 2 arguments
   |     |
   |     expected function that takes 1 argument
   |
   = note: required by `takes_callback`

@shepmaster
Copy link
Member

Duplicate of #42855 ?

@estebank
Copy link
Contributor

The current output doesn't point directly at F: FnOnce(T) -> R, but it does point at the entire method:

error[E0593]: function is expected to take 1 argument, but it takes 2 arguments
  --> src/main.rs:12:5
   |
7  | fn callback(s1: &str, s2: &str) {
   | ------------------------------- takes 2 arguments
...
12 |     takes_callback(callback, "rust!");
   |     ^^^^^^^^^^^^^^ expected function that takes 1 argument
   |
note: required by `takes_callback`
  --> src/main.rs:1:1
   |
1  | / fn takes_callback<F,T,R>(callback: F, t: T)
2  | |     where F: FnOnce(T) -> R
3  | | {
4  | |     callback(t);
5  | | }
   | |_^

Closing as duplicate as per @shepmaster's comment.

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

No branches or pull requests

4 participants