Stephen Sprunk said:
No specific examples come to mind, but generally speaking, GCC is
obviously capable of generating such warnings since it does so when
optimization is enabled, yet at some point someone chose not to do the
same analysis when optimization is disabled even though the warnings
could obviously be given without affecting code generation.
I think your understanding is reversed (or, conceivably, mine is).
When you requesti optimization (via "-O3" or whatever),
gcc performs additional analysis if your code so that it can
determine what optimizations can be performed without breaking the
required behavior. A side effect of that analysis is that it can
detect things that might cause undefined behavior. For example
tracking the value that an object holds during execution both (a)
enables optimization (such as replacing a reference to the object
with a constant if the compiler can prove that it must hold some
value in particular), and (b) enables some warnings (such as an
overflow because it's been able to figure out that the value you're
incrementing at run time happens to be INT_MAX).
It refrains from performing that analysis at -O0 simply because
it takes additional time and memory, and "gcc -O0" means roughly
"Compiler: Please generate correct code quickly".
It might have made some sense not to tie these two things together, so
that the compiler could perform the analysis needed to diagnose (some
instances of) undefined behavior without generating optimized code, but
there probably isn't enough demand for that.
When I was first learning C, I had no trouble making my programs compile
quietly and work as expected with -O0. However, with -O3, I would get
dozens of new warnings--and my code no longer worked as expected. So,
correctly or not, I learned that GCC only warns me when it thinks it's
doing something that I don't expect.
It's likely that your code has undefined behavior regardless of the
optimization level (in fact, UB is always independent of the
optimization level), and it just happened to "work" at "-O0" and not at
"-O3". Which is why compiling with "-O3", even if you don't intend to
run the optimized code, can be a good way to flush out bugs.
For the specific case where the problem is a disallowed implicit
conversion, sure, but useful undefined behavior is larger than that.
Hence the problem with a proposal to _require_ warnings.
I don't recall any such proposal.