public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/97266] New: "enum constant in boolean context" warning seems incorrect
@ 2020-10-01 18:22 mfarazma.ext at gmail dot com
  2020-10-01 19:08 ` [Bug c++/97266] " redi at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: mfarazma.ext at gmail dot com @ 2020-10-01 18:22 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97266

            Bug ID: 97266
           Summary: "enum constant in boolean context" warning seems
                    incorrect
           Product: gcc
           Version: 8.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: mfarazma.ext at gmail dot com
  Target Milestone: ---

```
#include <iostream>

enum ValidateFlag : int8_t {
   a = 0, b , c
};

int main(){ 
  bool t = static_cast<bool>(c);
  return static_cast<int>(t);
}
```

Compiling the above code with `g++ -Wall test.cc` generates this warning:

warning: enum constant in boolean context [-Wint-in-bool-context]

The behaviour doesn't seem correct as `c` is just an `int8_t` value, and
casting an `int8_t` value to `bool` does not generate any warnings:

```
  int8_t c = 2;         
  bool t = static_cast<bool>(c);
  return static_cast<int>(t);
```

Having only 2 values in the enum also makes it compile fine:
```
enum ValidateFlag : int8_t {
   a = 0, c
};
```

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c++/97266] "enum constant in boolean context" warning seems incorrect
  2020-10-01 18:22 [Bug c++/97266] New: "enum constant in boolean context" warning seems incorrect mfarazma.ext at gmail dot com
@ 2020-10-01 19:08 ` redi at gcc dot gnu.org
  2020-10-01 19:16 ` mfarazma.ext at gmail dot com
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2020-10-01 19:08 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97266

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to m farazma from comment #0)
> ```
> #include <iostream>
> 
> enum ValidateFlag : int8_t {
>    a = 0, b , c
> };
> 
> int main(){ 
>   bool t = static_cast<bool>(c);
>   return static_cast<int>(t);
> }
> ```
> 
> Compiling the above code with `g++ -Wall test.cc` generates this warning:
> 
> warning: enum constant in boolean context [-Wint-in-bool-context]
> 
> The behaviour doesn't seem correct as `c` is just an `int8_t` value,

No it isn't. In C enumerators are just integers, but in C++ they have the same
type as the enumeration type they belong to.

> and
> casting an `int8_t` value to `bool` does not generate any warnings:
> 
> ```
>   int8_t c = 2;         
>   bool t = static_cast<bool>(c);
>   return static_cast<int>(t);
> ```

I don't know why this is different. I would expect them to be consistent.


> Having only 2 values in the enum also makes it compile fine:
> ```
> enum ValidateFlag : int8_t {
>    a = 0, c
> };

In this case c=1 so there's no change in value when casting to bool. The
warning is for uses of integers other than 0 and 1.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c++/97266] "enum constant in boolean context" warning seems incorrect
  2020-10-01 18:22 [Bug c++/97266] New: "enum constant in boolean context" warning seems incorrect mfarazma.ext at gmail dot com
  2020-10-01 19:08 ` [Bug c++/97266] " redi at gcc dot gnu.org
@ 2020-10-01 19:16 ` mfarazma.ext at gmail dot com
  2020-10-01 22:47 ` redi at gcc dot gnu.org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: mfarazma.ext at gmail dot com @ 2020-10-01 19:16 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97266

--- Comment #2 from m farazma <mfarazma.ext at gmail dot com> ---
(In reply to Jonathan Wakely from comment #1)
> (In reply to m farazma from comment #0)
> > ```
> > #include <iostream>
> > 
> > enum ValidateFlag : int8_t {
> >    a = 0, b , c
> > };
> > 
> > int main(){ 
> >   bool t = static_cast<bool>(c);
> >   return static_cast<int>(t);
> > }
> > ```
> > 
> > Compiling the above code with `g++ -Wall test.cc` generates this warning:
> > 
> > warning: enum constant in boolean context [-Wint-in-bool-context]
> > 
> > The behaviour doesn't seem correct as `c` is just an `int8_t` value,
> 
> No it isn't. In C enumerators are just integers, but in C++ they have the
> same type as the enumeration type they belong to.
> 
But isn't the type of the enum set to be `int8_t` in this example?
In which case casting from `int8_t` to `bool` should be straight forward and
without warnings.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c++/97266] "enum constant in boolean context" warning seems incorrect
  2020-10-01 18:22 [Bug c++/97266] New: "enum constant in boolean context" warning seems incorrect mfarazma.ext at gmail dot com
  2020-10-01 19:08 ` [Bug c++/97266] " redi at gcc dot gnu.org
  2020-10-01 19:16 ` mfarazma.ext at gmail dot com
@ 2020-10-01 22:47 ` redi at gcc dot gnu.org
  2020-10-01 23:10 ` mfarazma.ext at gmail dot com
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2020-10-01 22:47 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97266

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
No, the type is ValidateFlag. It has an underlying type of int8_t, but that
just means it has the same size and range of values as int8_t. It's not
actually that type.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c++/97266] "enum constant in boolean context" warning seems incorrect
  2020-10-01 18:22 [Bug c++/97266] New: "enum constant in boolean context" warning seems incorrect mfarazma.ext at gmail dot com
                   ` (2 preceding siblings ...)
  2020-10-01 22:47 ` redi at gcc dot gnu.org
@ 2020-10-01 23:10 ` mfarazma.ext at gmail dot com
  2020-10-02 13:31 ` redi at gcc dot gnu.org
  2020-10-02 13:53 ` mfarazma.ext at gmail dot com
  5 siblings, 0 replies; 7+ messages in thread
From: mfarazma.ext at gmail dot com @ 2020-10-01 23:10 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97266

--- Comment #4 from m farazma <mfarazma.ext at gmail dot com> ---
(In reply to Jonathan Wakely from comment #3)
> No, the type is ValidateFlag. It has an underlying type of int8_t, but that
> just means it has the same size and range of values as int8_t. It's not
> actually that type.

Sorry for the confusion, I've tried creating a new variable with the following
2 types:

```
  ValidateFlag v = c;
  bool t = static_cast<bool>(v);
```

and

```
  int8_t i = c;
  bool t = static_cast<bool>(i);
```

and they both compile whiteout a warning, but using `c` itself causes the
issue.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c++/97266] "enum constant in boolean context" warning seems incorrect
  2020-10-01 18:22 [Bug c++/97266] New: "enum constant in boolean context" warning seems incorrect mfarazma.ext at gmail dot com
                   ` (3 preceding siblings ...)
  2020-10-01 23:10 ` mfarazma.ext at gmail dot com
@ 2020-10-02 13:31 ` redi at gcc dot gnu.org
  2020-10-02 13:53 ` mfarazma.ext at gmail dot com
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2020-10-02 13:31 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97266

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2020-10-02

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Neither of those cases are constants, and the warning documentation (and the
actual diagnostic itself) talk about constants.

But there's still no warning for:

  constexpr ValidateFlag v = c;
  bool t = static_cast<bool>(v);

which definitely seems inconsistent. I don't know what the intended behaviour
is, but it should be consistent.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c++/97266] "enum constant in boolean context" warning seems incorrect
  2020-10-01 18:22 [Bug c++/97266] New: "enum constant in boolean context" warning seems incorrect mfarazma.ext at gmail dot com
                   ` (4 preceding siblings ...)
  2020-10-02 13:31 ` redi at gcc dot gnu.org
@ 2020-10-02 13:53 ` mfarazma.ext at gmail dot com
  5 siblings, 0 replies; 7+ messages in thread
From: mfarazma.ext at gmail dot com @ 2020-10-02 13:53 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97266

--- Comment #6 from m farazma <mfarazma.ext at gmail dot com> ---
(In reply to Jonathan Wakely from comment #5)
> Neither of those cases are constants, and the warning documentation (and the
> actual diagnostic itself) talk about constants.
> 
> But there's still no warning for:
> 
>   constexpr ValidateFlag v = c;
>   bool t = static_cast<bool>(v);
> 
> which definitely seems inconsistent. I don't know what the intended
> behaviour is, but it should be consistent.

When using clang 12 there are no warning messages. We had to 
use "-Wno-int-in-bool-context" to disable the warning on gcc.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2020-10-02 13:53 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-01 18:22 [Bug c++/97266] New: "enum constant in boolean context" warning seems incorrect mfarazma.ext at gmail dot com
2020-10-01 19:08 ` [Bug c++/97266] " redi at gcc dot gnu.org
2020-10-01 19:16 ` mfarazma.ext at gmail dot com
2020-10-01 22:47 ` redi at gcc dot gnu.org
2020-10-01 23:10 ` mfarazma.ext at gmail dot com
2020-10-02 13:31 ` redi at gcc dot gnu.org
2020-10-02 13:53 ` mfarazma.ext at gmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).