rust-lang / rust-lang/rust

Compiler can't consistently auto-coerce fn pointers

Open
#149,731 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-coercions A-inference C-enhancement T-compiler T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I noticed that when matching and returning (const) fn items directly, the compiler was able to coerce them into the same fn type fn(u64, u64) -> u64 automatically, but when included as part of a tuple, the compiler seemingly lost this ability.

I'd probably call this more "surprising" than a "bug", since the diagnostics do hint at the right way to remediate this. Still - I wanted to report since it was suprising to me that it could handle it in the simple case and not in the next more complex type.

I tried this code:

playground link

fn main() {
    let op = "*";
    
    // Single item: totally fine!
    let func = match op {
        "+" => u64::wrapping_add,
        "*" => u64::wrapping_mul,
        _ => unimplemented!(),
    };
    println!("{}", func(2, 20));
    
    // NOT FINE
    //
    //   = note: expected tuple `({integer}, fn(_, _) -> _ {core::num::<impl u64>::wrapping_add})`
    //               found tuple `({integer}, fn(_, _) -> _ {core::num::<impl u64>::wrapping_mul})`
    //   = note: different fn items have unique types, even if their signatures are the same
    //   = help: consider casting both fn items to fn pointers using `as fn(u64, u64) -> u64`
    //
    // let (base, func) = match op {
    //     "+" => (0, u64::wrapping_add),
    //     "*" => (1, u64::wrapping_mul),
    //     _ => unimplemented!(),
    // };
    // println!("{}", func(base, 20));
    
    // Fine if we specify the type (or do manual casting)
    let (base, func): (u64, fn(u64, u64) -> u64) = match op {
        "+" => (0, u64::wrapping_add),
        "*" => (1, u64::wrapping_mul),
        _ => unimplemented!(),
    };
    println!("{}", func(base, 20));
}

I expected to see this happen: Compiler is consistent whether the fn items are directly returned or as part of an aggregate/tuple item.

Instead, this happened: Compiler only performed auto-coercion of the fn item when it was not part of an aggregate/tuple item

Meta

Repros on current stable/nightlies: 1.91.1, 2025-12-06.

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 reproducing the playground example, comparing the direct function-item match with the tuple-valued match and the explicitly typed version. Trace the compiler's function-item coercion behavior for match arms containing tuples, then verify that the tuple case behaves consistently with the direct case without requiring the annotation.

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
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.