rust-lang / rust-lang/rust

Inconsistent errors when passing functions as arguments.

Open
#139,786 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics C-bug D-inconsistent T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by compiling the reported bug.rs reproducer with rustc and compare the diagnostics for foo(bad) and bar(&bad). Trace the compiler diagnostic paths for function-pointer and dyn Fn argument mismatches, then add coverage showing that both cases produce consistent, clear errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.