3 ms·
> Overflow is defined by the language as an illegal operation Is it? What do you mean by illegal here? Overflow can't cause UB madness in Rust like it does in
by Manishearth 11y ago
> Overflow is defined by the language as an illegal operation
Is it? What do you mean by illegal here? Overflow can't cause UB madness in Rust like it does in C; the compiler does not optimize loops assuming no overflow.
- vardump 11y agoUB can be confusing, but please remember it also means a lot of very reasonable optimization opportunities. Say in data flow analysis, when the compiler can assume "(value + 1) > value" to be always true, a whole expensive branch can be removed. If the overflow is never going to happen anyways, the savings are considerable (expensive branch eliminated) and the result is always correct.
- Manishearth 11y agoI know that UB can be useful for optimizations. Where did I assert otherwise? I'm saying that overflow is not undefined in Rust (the very first section of the blog post says this). I made this same mistake a few days ago and thought it was undefined (and implementation-defined to panic on debug), but I was wrong. Overflow is well defined in Rust.