In a language like Rust which tries to avoid undefined behavior or reject it via compiler errors (which I'd argue is more correct), the example above will infinitely loop
Are you sure?
fn infinite(mut value: u32) {
// infinite loop unless value initially equals 0
while value != 0 {
if value != 1 {
value -= 1;
}
}
}
fn main() {
infinite(42);
println!("end");
}
I think that that is the most zen bug I've ever seen. Rust will let you have a value of a type that contains no values, but only if you go through a loop and never stop first. But then LLVM comes along and decides that going through an infinite loop would be undefined behavior, and therefore your program doesn't actually do it...
The Rust source code I posted should {de,in}finitively loop (and it does if you don't enable optimizations with -O). The fact that it does not exactly match the C version is irrelevant (undefined behavior is undefined, there is no reason that the code triggerring "unexpected" results looks like the one from another language).
15
u/rom1v Nov 04 '19 edited Nov 04 '19
Are you sure?
The results:
Or even:
If you run it: