[Bug c++/57170] No diagnostic for a negative case when switching over unsigned

2017-08-23 Thread rs2740 at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57170

TC  changed:

   What|Removed |Added

 CC||rs2740 at gmail dot com

--- Comment #2 from TC  ---
As comment #0 alluded to - perhaps a bit opaquely - this code is ill-formed in
C++11 and later. [stmt.switch] requires the case expression to be "a converted
constant expression ([expr.const]) of the promoted type of the switch
condition", and a narrowing conversion cannot be used in a converted constant
expression. The standard requires a diagnostic, which GCC fails to provide even
with -pedantic.

(Meanwhile, using unsigned char or unsigned short there is actually well-formed
if they get promoted to int as they usually do.)

So, unless you are saying that GCC's full conformance mode requires
-Wsign-conversion, I don't see how this bug is invalid.

[Bug c++/57170] No diagnostic for a negative case when switching over unsigned

2017-08-21 Thread egallager at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57170

Eric Gallager  changed:

   What|Removed |Added

   Keywords||diagnostic
 Status|UNCONFIRMED |RESOLVED
 CC||egallager at gcc dot gnu.org
 Resolution|--- |INVALID

--- Comment #1 from Eric Gallager  ---
Use -Wsign-conversion to get a warning:

$ /usr/local/bin/gcc -c -Wall -Wextra -Wconversion -Wnarrowing -pedantic
-Wswitch -Wsign-promo -Wtype-limits -Wsign-conversion -std=c++98 57170.cc
57170.cc: In function ‘int main()’:
57170.cc:4:3: warning: unsigned conversion from ‘int’ to ‘unsigned int’ changes
value from ‘-1’ to ‘4294967295’ [-Wsign-conversion]
   case -1:
   ^~~~
$

-Wsign-conversion has to be specified explicitly in c++, but in plain c, it's
implied by -Wconversion:

$ /usr/local/bin/gcc -c -x c -Wall -Wextra -Wconversion -pedantic -Wswitch
-Wtype-limits 57170.cc
57170.cc: In function ‘main’:
57170.cc:4:3: warning: unsigned conversion from ‘int’ to ‘unsigned int’ changes
value from ‘-1’ to ‘4294967295’ [-Wsign-conversion]
   case -1:
   ^~~~
$