Hi. I tried this code in the godbolt:
pub enum Bool {
False,
True,
}
pub fn b(b: &mut bool) {
if *b == false {
*b = true;
}
}
pub fn B(b: &mut Bool) {
if let Bool::False = *b {
*b = Bool::True;
}
}
I was expecting something like this as the assembly output:
example::B:
mov byte ptr [rdi], 1
ret
But the compiler doesn't optimize it and doesn't outputs the above assembly. I don't really know about the internals of bool
type, but I know that the compiler is able to see possible variants, and therefore possible states of enums and act according to that. Since invalid discriminants for enums also UB, I was expecting the compiler to do the above optimization. Is there something I am not aware of that blocks this kind of optimization or is it just not implemented? If there is not such an obstacle, should we implement this to the compiler?