Skip to content

Rust fails to coerce to a fn pointer when passing an array as an argument to a generic function. #136420

@theemathas

Description

@theemathas

I tried this code:

fn foo() {} fn bar() {} fn main() { let _a = if true { foo } else { bar }; let _b = vec![foo, bar]; let _c = [foo, bar]; d(if true { foo } else { bar }); e(vec![foo, bar]); f([foo, bar]); } fn d<T>(_: T) {} fn e<T>(_: Vec<T>) {} fn f<T>(_: [T; 2]) {}

I expected the six lines to either all compile or all error. Instead, only the f([foo, bar]) line fails, with the following error:

error[E0308]: mismatched types --> src/main.rs:10:7 | 10 | f([foo, bar]); | - ^^^^^^^^^^ expected `[fn() {foo}; 2]`, found `[fn(); 2]` | | | arguments to this function are incorrect | = note: expected array `[fn() {foo}; 2]` found array `[fn(); 2]` note: function defined here --> src/main.rs:15:4 | 15 | fn f<T>(_: [T; 2]) {} | ^ --------- For more information about this error, try `rustc --explain E0308`. 

For some reason, it's failing to coerce the function items into function pointers, but only when an array is being passed to a generic function.

Discovered by @xero-lib

Meta

Issue reproducible on the playground with stable rust 1.84.1, and nightly rust 1.86.0-nightly (2025-02-01 8239a37f9c0951a037cf)

@rustbot labels +A-inference +A-coercions

Metadata

Metadata

Assignees

Labels

A-coercionsArea: implicit and explicit `expr as Type` coercionsA-inferenceArea: Type inferenceC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types 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