Skip to content

Inconsistent errors when passing functions as arguments. #139786

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

Open
sandmuel opened this issue Apr 14, 2025 · 0 comments
Open

Inconsistent errors when passing functions as arguments. #139786

sandmuel opened this issue Apr 14, 2025 · 0 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-inconsistent Diagnostics: Inconsistency in formatting, grammar or style between diagnostic messages. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@sandmuel
Copy link

I tried this code:

fn main() {
    foo(bad);
    bar(&bad);
}

fn foo(_: fn(i32)) {}

fn bar(_: &'static dyn Fn(i32)) {}

fn bad() {}

I expected to see this happen:

error[E0593]: function is expected to take 1 argument, but it takes 0 arguments
  --> bug.rs:2:9
   |
3  |     foo(bad);
   |         ^^^ expected function that takes 1 argument
...
10 | fn bad() {}
   | -------- takes 0 arguments
   |
   = note: required for the cast from `&fn() {bad}` to `&'static (dyn Fn(i32) + 'static)`

error[E0593]: function is expected to take 1 argument, but it takes 0 arguments
  --> bug.rs:3:9
   |
3  |     bar(&bad);
   |         ^^^^ expected function that takes 1 argument
...
10 | fn bad() {}
   | -------- takes 0 arguments
   |
   = note: required for the cast from `&fn() {bad}` to `&'static (dyn Fn(i32) + 'static)`

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0308, E0593.
For more information about an error, try `rustc --explain E0308`.

Instead, this happened:

error[E0308]: mismatched types
 --> bug.rs:2:9
  |
2 |     foo(bad);
  |     --- ^^^ incorrect number of function parameters
  |     |
  |     arguments to this function are incorrect
  |
  = note: expected fn pointer `fn(i32)`
                found fn item `fn() {bad}`
note: function defined here
 --> bug.rs:6:4
  |
6 | fn foo(_: fn(i32)) {}
  |    ^^^ ----------

error[E0593]: function is expected to take 1 argument, but it takes 0 arguments
  --> bug.rs:3:9
   |
3  |     bar(&bad);
   |         ^^^^ expected function that takes 1 argument
...
10 | fn bad() {}
   | -------- takes 0 arguments
   |
   = note: required for the cast from `&fn() {bad}` to `&'static (dyn Fn(i32) + 'static)`

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0308, E0593.
For more information about an error, try `rustc --explain E0308`.

The errors are inconsistent with the fn() getting less clear of an error than the dyn Fn.

Meta

rustc --version --verbose:

rustc 1.86.0 (05f9846f8 2025-03-31) (Homebrew)
binary: rustc
commit-hash: 05f9846f893b09a1be1fc8560e33fc3c815cfecb
commit-date: 2025-03-31
host: x86_64-unknown-linux-gnu
release: 1.86.0
LLVM version: 19.1.7
Backtrace

N/A

@sandmuel sandmuel added the C-bug Category: This is a bug. label Apr 14, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Apr 14, 2025
@lolbinarycat lolbinarycat added D-inconsistent Diagnostics: Inconsistency in formatting, grammar or style between diagnostic messages. A-diagnostics Area: Messages for errors, warnings, and lints labels Apr 14, 2025
@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Apr 14, 2025
@jieyouxu jieyouxu removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Apr 18, 2025
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. D-inconsistent Diagnostics: Inconsistency in formatting, grammar or style between diagnostic messages. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants