Unfortunately, by the time the compiler sees something bad that it wants to warn about, it has probably transformed the code into something quite different from what you started with, and converting its diagnostic info back into something "sensible to humans" is sometimes difficult.hgm wrote:A bit confusing that it doesn't simply say the array bounds are violated, when it apparently sees that they are.
GCC actually did pretty well in this specific case, although it expects you to know a bit of compilerese. It told you that the undefined behavior was invoked on iteration 45 (and by implication, iterations 0 to 44 were not guilty of whatever it is warning you about on iteration 45). It also pointed out the specific spot in the source code that was accessing out of bounds, even if it didn't spell out exactly what the "undefined behavior" was. Since C and C++ contain over 200 kinds of undefined behavior, its not surprising that they just use a generic message. In at least some cases, the compiler will probably have lost track of precisely what kind of UB it discovered by the time it issues the diagnostic message. Anyway, at least it warned you about it at all!
I think modern compilers are getting a bit better at diagnosing "obvious UB bugs", where the code will be forced to execute the UB case. And better UB handling in general, especially clang with its sanitizer options. Clang has a stated goal of making sure that any kind of UB exploited by their optimizer, can also be tested for using their runtime sanitizer checks. The compiler's knowledge that UB cases "can't happen" leads to faster compiled programs for many well-defined source programs, and to strange behavior for some incorrect source programs, and the compiler usually can't tell which of those two types of program it was asked to compile. If they get to 100% coverage, then programmers will be able to test their programs in that mode to make sure their program is in the first category (i.e. to gain confidence that their optimized builds are not behaving in unexpected ways because of UB).