Skip to content

Weird and inconsistent function pointer comparison behaviour #33879

@simias

Description

@simias

Long story short:

fn main() {
    let f = foo;

    // error: binary operation `==` cannot be applied to type `fn(u32) {foo}` [E0369]
    let _ = f == foo;

    // error: binary operation `==` cannot be applied to type `fn(u32) {foo}` [E0369]
    let _ = foo == f;

    // error: binary operation `==` cannot be applied to type `fn(u32) {foo}` [E0369]
    let _ = foo as fn(&u32) == f;

    // Compiles, even though as far as I can tell it should be
    // equivalent to the previous one
    let _ = foo as fn(_) == f;

    // error: binary operation `==` cannot be applied to type `fn(u32) {foo}` [E0369]
    let _ = f == foo as fn(_);
}

fn foo(param: &u32) {
    println!("{}", param);
}

I don't understand why the compiler is so picky about comparing function pointers, in particular I don't understand why it accepts the cast as fn(_) but not as fn(u32). And even when it works it's not commutative, which for the == operator is rather unexpected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions