Skip to content

Inconsistent errors when passing functions as arguments. #139786

Open
@sandmuel

Description

@sandmuel

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.D-inconsistentDiagnostics: Inconsistency in formatting, grammar or style between diagnostic messages.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions