Closed
Description
In this example:
#[derive(Copy, Clone, Eq, PartialEq)]
pub enum Variant {
Zero,
One,
Two,
}
#[inline]
fn unreachable() {
println!("Impossible");
}
extern {
fn exf1();
fn exf2();
}
#[no_mangle]
pub static mut GLOBAL: Variant = Variant::Zero;
pub unsafe fn test() {
let g = GLOBAL;
if g != Variant::Zero {
match g {
Variant::One => exf1(),
Variant::Two => exf2(),
Variant::Zero => unreachable(),
}
}
}
the unreachable()
branch is not removed. Adding any #[repr(n)]
but not #[repr(C)]
to Variant
or making reachable branches call only one of external functions allows the optimization.
There's no such issue with a similar C example using clang so it's likely something on Rust side.
Example links:
Rust https://rust.godbolt.org/z/9oh6sP
C https://godbolt.org/z/Tnbfx6