3 ms·
It doesn't 'invoke' undefined behavior in the sense that it calls some function 'doRandomShit()'. What happens is: Compilers sees unreachable and assumes that
by Xeamek 2y ago
It doesn't 'invoke' undefined behavior in the sense that it calls some function 'doRandomShit()'.
What happens is:
Compilers sees unreachable and assumes that this codepath will never run, so it just yeet it out of existence.
For example, maybe it will remove the preceding if-statement and rather then check its condition, just insert true or false. Afterall, why waste computing on checking condition if we know it will never resolve to the unreachable path.
In Rust, the same if-statement won't be optimized away, and if the code happens to go for the unreachable path, it will call panic.
Now, the example is a simplification, but it's just to demonstrate that UB comes from compilers (verry aggressively) assuming UB will never happen, and not because someone decided to "add it".