3 ms·
> If it wasn't true, we'd just make programming languages with no UB. It's not like compiler writers are just lazy. Well, we do make programming languages with
by pcwalton 10y ago
> If it wasn't true, we'd just make programming languages with no UB. It's not like compiler writers are just lazy.
Well, we do make programming languages with no undefined behavior. :) It's just that C and C++ need UB.
For instance, all your examples are things that Rust and Swift can statically prove without resorting to UB in the language semantics. Null pointer assumptions become precise with nullable types. Optimizations around opaque calls can be made sound with static knowledge of aliasing and type safety (most importantly, TBAA is a sound analysis if your language is memory safe). Signed overflow UB is less important to optimize around if programmers use proper iterator constructs so the compiler can soundly determine trip count. And so forth.
(I'm sure you know all of this, just stating for the record.)