[Bug c++/57170] Diagnostic for a negative case when switching over unsigned comes from -Wsign-conversion instead of -Wpedantic or -Wnarrowing

2019-05-29 Thread egallager at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57170

--- Comment #7 from Eric Gallager  ---
(In reply to Jonathan Wakely from comment #6)
> It should be an error, not a warning. For other narrowing conversions
> involving constants we do actually use -Wnarrowing (but still defaulting to
> an error) so I guess my last comment was wrong.
> 
> Or it could just be an unconditional error. Or a permerror, so -fpermissive
> would allow it.

I'd prefer the permerror so -fpermissive would allow it, but...

> 
> The code that handles this is in gcc/c-family/c-common.c so isn't
> C++-specific. I'm not sure if that code has enough context to know if the
> conversion happens in a constant expression where narrowing conversions are
> disallowed.

...if it's in c-common.c... are permerrors allowed to come from there even
though -fpermissive is only for C++?

> 
> 
> #0  unsafe_conversion_p (loc=, loc@entry=267203,
> type=type@entry=0x7013c690, expr=expr@entry=0x701401f8,
> result=result@entry=0x70140048, produce_warns=produce_warns@entry=true)
> at /home/jwakely/src/gcc/gcc/gcc/c-family/c-common.c:1261
> #1  0x008cdb27 in conversion_warning (loc=267203,
> type=type@entry=0x7013c690, expr=expr@entry=0x701401f8,
> result=result@entry=0x70140048) at
> /home/jwakely/src/gcc/gcc/gcc/c-family/c-warn.c:1127
> #2  0x008d11d1 in warnings_for_convert_and_check (loc= out>, loc@entry=267203, type=type@entry=0x7013c690,
> expr=expr@entry=0x701401f8, result=result@entry=0x70140048) at
> /home/jwakely/src/gcc/gcc/gcc/c-family/c-warn.c:1278
> #3  0x0085df41 in convert_and_check (loc=loc@entry=267203,
> type=type@entry=0x7013c690, expr=0x701401f8) at
> /home/jwakely/src/gcc/gcc/gcc/c-family/c-common.c:1548
> #4  0x00886a55 in c_add_case_label (loc=loc@entry=267203,
> cases=0x24ad5e0, cond=cond@entry=0x77ff6cf0,
> orig_type=orig_type@entry=0x7013c690, low_value=,
> low_value@entry=0x701401f8, high_value=,
> high_value@entry=0x0, outside_range_p=0x24abff0)
> at /home/jwakely/src/gcc/gcc/gcc/c-family/c-common.c:4780
> #5  0x0066b1fa in finish_case_label (loc=267203,
> low_value=, low_value@entry=0x701401f8,
> high_value=) at /home/jwakely/src/gcc/gcc/gcc/cp/decl.c:3669
> #6  0x007309e9 in cp_parser_label_for_labeled_statement
> (parser=parser@entry=0x77ff6ab0, attributes=attributes@entry=0x0) at
> /home/jwakely/src/gcc/gcc/gcc/cp/parser.c:11072

[Bug c++/57170] Diagnostic for a negative case when switching over unsigned comes from -Wsign-conversion instead of -Wpedantic or -Wnarrowing

2018-08-29 Thread redi at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57170

--- Comment #6 from Jonathan Wakely  ---
It should be an error, not a warning. For other narrowing conversions involving
constants we do actually use -Wnarrowing (but still defaulting to an error) so
I guess my last comment was wrong.

Or it could just be an unconditional error. Or a permerror, so -fpermissive
would allow it.

The code that handles this is in gcc/c-family/c-common.c so isn't C++-specific.
I'm not sure if that code has enough context to know if the conversion happens
in a constant expression where narrowing conversions are disallowed.


#0  unsafe_conversion_p (loc=, loc@entry=267203,
type=type@entry=0x7013c690, expr=expr@entry=0x701401f8,
result=result@entry=0x70140048, produce_warns=produce_warns@entry=true) at
/home/jwakely/src/gcc/gcc/gcc/c-family/c-common.c:1261
#1  0x008cdb27 in conversion_warning (loc=267203,
type=type@entry=0x7013c690, expr=expr@entry=0x701401f8,
result=result@entry=0x70140048) at
/home/jwakely/src/gcc/gcc/gcc/c-family/c-warn.c:1127
#2  0x008d11d1 in warnings_for_convert_and_check (loc=,
loc@entry=267203, type=type@entry=0x7013c690,
expr=expr@entry=0x701401f8, result=result@entry=0x70140048) at
/home/jwakely/src/gcc/gcc/gcc/c-family/c-warn.c:1278
#3  0x0085df41 in convert_and_check (loc=loc@entry=267203,
type=type@entry=0x7013c690, expr=0x701401f8) at
/home/jwakely/src/gcc/gcc/gcc/c-family/c-common.c:1548
#4  0x00886a55 in c_add_case_label (loc=loc@entry=267203,
cases=0x24ad5e0, cond=cond@entry=0x77ff6cf0,
orig_type=orig_type@entry=0x7013c690, low_value=,
low_value@entry=0x701401f8, high_value=,
high_value@entry=0x0, outside_range_p=0x24abff0)
at /home/jwakely/src/gcc/gcc/gcc/c-family/c-common.c:4780
#5  0x0066b1fa in finish_case_label (loc=267203, low_value=, low_value@entry=0x701401f8, high_value=) at
/home/jwakely/src/gcc/gcc/gcc/cp/decl.c:3669
#6  0x007309e9 in cp_parser_label_for_labeled_statement
(parser=parser@entry=0x77ff6ab0, attributes=attributes@entry=0x0) at
/home/jwakely/src/gcc/gcc/gcc/cp/parser.c:11072

[Bug c++/57170] Diagnostic for a negative case when switching over unsigned comes from -Wsign-conversion instead of -Wpedantic or -Wnarrowing

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

--- Comment #5 from Eric Gallager  ---
(In reply to Jonathan Wakely from comment #4)
> Diagnostics for narrowing conversions involving non-constants are controlled
> by -Wnarrowing (allowing them to be either warnings or errors). But
> narrowing conversions involving constants are always errors. Case labels
> must be constants, so making this depend on -Wnarrowing wouldn't seem
> appropriate to me.

So -Wpedantic instead? Or -fpermissive?

[Bug c++/57170] Diagnostic for a negative case when switching over unsigned comes from -Wsign-conversion instead of -Wpedantic or -Wnarrowing

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

--- Comment #4 from Jonathan Wakely  ---
Diagnostics for narrowing conversions involving non-constants are controlled by
-Wnarrowing (allowing them to be either warnings or errors). But narrowing
conversions involving constants are always errors. Case labels must be
constants, so making this depend on -Wnarrowing wouldn't seem appropriate to
me.

[Bug c++/57170] Diagnostic for a negative case when switching over unsigned comes from -Wsign-conversion instead of -Wpedantic or -Wnarrowing

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

Eric Gallager  changed:

   What|Removed |Added

   Keywords||accepts-invalid
 Status|RESOLVED|NEW
   Last reconfirmed||2017-08-24
 Resolution|INVALID |---
Summary|No diagnostic for a |Diagnostic for a negative
   |negative case when  |case when switching over
   |switching over unsigned |unsigned comes from
   ||-Wsign-conversion instead
   ||of -Wpedantic or
   ||-Wnarrowing
 Ever confirmed|0   |1

--- Comment #3 from Eric Gallager  ---
(In reply to TC from comment #2)
> 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.

OK, reopening and retitling to make it clearer.