3 ms·
The problem, as I've heard it, is that all but the most trivial instances of statically detectable UB are only noticeable once optimizations have been performed
by twoodfin 11y ago
The problem, as I've heard it, is that all but the most trivial instances of statically detectable UB are only noticeable once optimizations have been performed (or simulated by an analyzer), and at that point they're indistinguishable from potential UB introduced by the optimizations themselves.
The compiler does a bunch of inlining, constant propagation, other optimizing transformations... and it ends up with some code paths that statically represent UB. If the original code couldn't display UB (up to the user to get this right), and the transformations preserve the original code's defined behavior (as of course they should), then it's safe to exploit the fact that these paths are impossible for further optimization or dead code elimination.
Reversing layers of optimization to decide "Is this UB something introduced by me or by the user?" is not something compilers are traditionally architected to do, nor can they typically perform the analysis necessary to determine if a UB code path from either source is guarded by some high level invariants.
In such cases, warnings would be actively harmful, as they'd suggest unneeded corrections.