Inconsistent shift-overflow warnings in C++20 · Issue #52873 · llvm/llvm-project (original) (raw)
Consider the following code:
int foo4s() { return (0x7000'0000) << 4; }
unsigned int foo4u() { return (0x7000'0000u) << 4; }
int foo1s() { return (0x7000'0000) << 1; }
unsigned int foo1u() { return (0x7000'0000u) << 1; }
which when compiled with -std=c++20 -Wall -Wextra -Wpedantic
results in:
<source>:2:26: warning: signed shift result (0x700000000) requires 36 bits to represent, but 'int' only has 32 bits [-Wshift-overflow]
return (0x7000'0000) << 4;
~~~~~~~~~~~~~ ^ ~
1 warning generated.
In C++20, the semantics are just as well defined for the signed case as they are for the unsigned case, thus generating a warning when shifting bits outside of a signed target type and not when shifting outside of a unsigned target type seems inconsistent to me.
GCC does not warn in this case in C++20 mode. See https://godbolt.org/z/9rbafz3h3.
This came up when discussing a similar (but not identical) issue in GCC. See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103826