[Bug c++/92158] Enum warning when -1 enum converted to unsigned int or int

2019-10-25 Thread redi at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92158

Jonathan Wakely  changed:

   What|Removed |Added

 Status|WAITING |NEW

--- Comment #5 from Jonathan Wakely  ---
Thank you for the corrected testcase, although compiling with -Werror when
asking for warnings is not very helpful (it obscures what's a warning and
what's an error) and posting code that doesn't even compile is not very helpful
either.

Confirming as a diagnostic enhancement for the following testcase:

enum E { a = -1 } ;
unsigned i = a;

Clang warns with -Wsign-conversion (not enabled by -Wall or -Wextra):

e.cc:2:14: warning: implicit conversion changes signedness: 'E' to 'unsigned
int' [-Wsign-conversion]
unsigned i = a;
 ~   ^
1 warning generated.

[Bug c++/92158] Enum warning when -1 enum converted to unsigned int or int

2019-10-22 Thread jg at jguk dot org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92158

--- Comment #4 from Jonny Grant  ---
Hello

Implicit conversion can introduce bugs. I would like to detect implicit enum
conversions to other types in C and C++. How about just adding the C++ warnings
first to match clang in example below?

The following example does produce warnings in C++ with Clang trunk, but not C.
Likewise with MSVC, C++ warning, but no C warning.

G++ only warns on line (19), clang has the additional line (15) and line (17)
warnings.

Could G++ also add the additions warnings?




-O2 -Wall -Wextra -Wconversion -Werror

#include 

typedef enum
{
a = -1
} a_t;

a_t f()
{
return a;
}

int main()
{
unsigned int b = f();

a_t test = b;
printf("%u %u", test, b);
return b;
}


#1 with x86-64 clang (trunk)
:15:22: error: implicit conversion changes signedness: 'a_t' to
'unsigned int' [-Werror,-Wsign-conversion]
unsigned int b = f();
 ~   ^~~
:17:9: error: cannot initialize a variable of type 'a_t' with an lvalue
of type 'unsigned int'
a_t test = b;
^  ~
:19:12: error: implicit conversion changes signedness: 'unsigned int'
to 'int' [-Werror,-Wsign-conversion]
return b;
~~ ^
3 errors generated.
Compiler returned: 1




#1 with x64 msvc v19.22
example.cpp
(18): error C2440: 'initializing': cannot convert from 'unsigned int'
to 'a_t'
(18): note: Conversion to enumeration type requires an explicit cast
(static_cast, C-style cast or function-style cast)
Compiler returned: 2



x86-64 gcc (trunk) - 484ms
#1 with x86-64 gcc (trunk)
: In function 'int main()':
:17:16: error: invalid conversion from 'unsigned int' to 'a_t'
[-fpermissive]
   17 | a_t test = b;
  |^
  ||
  |unsigned int
Compiler returned: 1

[Bug c++/92158] Enum warning when -1 enum converted to unsigned int or int

2019-10-21 Thread redi at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92158

Jonathan Wakely  changed:

   What|Removed |Added

 Status|UNCONFIRMED |WAITING
   Last reconfirmed||2019-10-21
 Ever confirmed|0   |1

--- Comment #3 from Jonathan Wakely  ---
Your example doesn't warn with Clang or Clang++ either.

[Bug c++/92158] Enum warning when -1 enum converted to unsigned int or int

2019-10-21 Thread redi at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92158

--- Comment #2 from Jonathan Wakely  ---
At least when the sign doesn't change, i.e. for conversion to int.

Please clarify what exactly you want a warning to do.

[Bug c++/92158] Enum warning when -1 enum converted to unsigned int or int

2019-10-21 Thread redi at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92158

--- Comment #1 from Jonathan Wakely  ---
Converting to integral types is a feature of enums, I see no reason to warn.

[Bug c++/92158] Enum warning when -1 enum converted to unsigned int or int

2019-10-21 Thread rguenth at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92158

Richard Biener  changed:

   What|Removed |Added

   Keywords||diagnostic
Version|unknown |10.0
   Severity|normal  |enhancement