Closed
Description
Playground link: https://play.rust-lang.org/?gist=62d362e2bf72001bd3f3c4a3ed0c842c&version=stable&mode=release&edition=2015
Switching to debug causes it to print both addresses twice (expected behavior), but in release both functions are optimized into one, so x
and y
are given the same address. @CryZe took a look at the assembly, and LLVM removed the first comparison (and evaluated it to a constant false), but not the second, which then evaluates to true. This is very reminiscent of undefined behavior in C and C++.
Thanks to @memoryruins for sending the original link which allowed me to discover this.
fn foo(i: i32) -> i32 {
1
}
fn bar(i: i32) -> i32 {
1
}
fn main() {
let x: fn(i32) -> i32 = foo;
let y: fn(i32) -> i32 = bar;
if x == y {
println!("true")
} else {
println!("{:?}, {:?}", x, y);
}
if x == y {
println!("true")
} else {
println!("{:?}, {:?}", x, y);
}
}
Debug output:
0x562a7b399600, 0x562a7b399620
0x562a7b399600, 0x562a7b399620
Release output:
0x560d1e3a1310, 0x560d1e3a1310
true
stable rustc 1.29.1 (b801ae6 2018-09-20)