-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Closed
Labels
A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-bugCategory: This is a bug.Category: This is a bug.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.Call for participation: An issue has been fixed and does not reproduce, but no test has been added.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
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)
Kixunil, Humberd and sevabakutovfbstj, matklad, kdy1, YaLTeR, jitthunk and 29 moremre, queer, seandewar and Duskhorna-little-devil, seandewar and Duskhornvladislav-tkach
Metadata
Metadata
Assignees
Labels
A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-bugCategory: This is a bug.Category: This is a bug.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.Call for participation: An issue has been fixed and does not reproduce, but no test has been added.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.