Skip to content

Weird behavior when comparing function pointers #54695

@VictorKoenders

Description

@VictorKoenders

As per the discussion on twitter

This piece of code in release mode has weird behavior when comparing 2 pointers twice. This behavior happens only in release mode and does appear on stable.

It looks like a LLVM optimization to me, @eddyb thinks it "might be an accidental optimization"

fn foo(_: i32) -> i32 {
    1
}
fn bar(_: i32) -> i32 {
    1
}

fn main() {
    let x: fn(i32) -> i32 = foo;
    let y: fn(i32) -> i32 = bar;
    
    if x == y {
        println!("true")
    } else {
        println!("false {:?}", x);
    }

    if x == y {
        println!("true")
    } else {
        println!("false {:?}", x);
    }
}
false 0x55eabbd342f0
true

play.rust-lang.org link

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