Development quote of the week
Development quote of the week
Posted Dec 5, 2022 12:17 UTC (Mon) by farnz (subscriber, #17727)In reply to: Development quote of the week by hummassa
Parent article: Development quote of the week
But the compiler does not "see the UB". It applies a set of rules that are valid assuming no UB, and comes to a result.
I bring you back round to the "proof" that 1 = 0. The outcome is clearly absurd - 1 = 0 is false - and yet I have a "proof" here that 1 = 0.
The reason it all goes to pieces is that I've applied a set of rules, all of which are valid. Every step in that "proof" is a valid application of the symbolic manipulation rules of algebra - none of the applied rules are wrong, nor was the manipulation incorrect. The reason I come up with an absurd result is that I didn't detect that, for the specific rule "divide both sides by (x-y)", it's invalid to apply that rule if x = y.
UB problems in compilation are similar. I've got a set of symbolic manipulations (often a very large set) that I can apply to your program. Some of those manipulations are only valid if the program does not contain UB (just as in my "proof", I used a manipulation rule that is only valid if x != y). At no point does the compiler "detect" UB - it's manipulating the program on the assumption that it doesn't 'contain UB, and the consequences of a chain of manipulations (that are all valid in the absence of UB) is a bad outcome if there is UB present.