public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/111056] New: Missing -Wsign-compare warning with enum values
@ 2023-08-17 22:16 alexhenrie24 at gmail dot com
  2023-08-17 22:42 ` [Bug c/111056] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: alexhenrie24 at gmail dot com @ 2023-08-17 22:16 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 111056
           Summary: Missing -Wsign-compare warning with enum values
           Product: gcc
           Version: 13.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: alexhenrie24 at gmail dot com
  Target Milestone: ---

#include <stdio.h>

int main()
{
    signed int a = 1;
    enum : signed int { b = 1 };
    unsigned int c = -1;

    if (a < c) // condition is true, -Wsign-compare warns about it
        puts("a < c");

    if (b < c) // condition is true, but no warning
        puts("b < c");

    return 0;
}

The compiler should warn about both if statements.

$ gcc --version
gcc (GCC) 13.2.1 20230801

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

* [Bug c/111056] Missing -Wsign-compare warning with enum values
  2023-08-17 22:16 [Bug c/111056] New: Missing -Wsign-compare warning with enum values alexhenrie24 at gmail dot com
@ 2023-08-17 22:42 ` pinskia at gcc dot gnu.org
  2023-08-17 23:11 ` alexhenrie24 at gmail dot com
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-08-17 22:42 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
We don't warn for the same reason as we don't warn for `constexpr` because the
value is known at compile time.
That is:
```
#include <stdio.h>

int main()
{
    signed int a = 1;
    enum : signed int { b = 1 }d = b;
    constexpr int t = 1;

    unsigned int c = -1;

    if (a < c) // condition is true, -Wsign-compare warns about it
        puts("a < c");

    if (t < c) // condition is true, but no warning
        puts("t < c");

    if (b < c) // condition is true, but no warning
        puts("b < c");

    if (d < c) // condition is true, -Wsign-compare warns about it
        puts("d < c");

    return 0;
}
```
`t` and `b` here are replaced with 1. But with a variable that is declared as
the enum type we do warn.

I think this is correct behavior really.
clang has the same behavior (well they don't yet support constexpr though).

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

* [Bug c/111056] Missing -Wsign-compare warning with enum values
  2023-08-17 22:16 [Bug c/111056] New: Missing -Wsign-compare warning with enum values alexhenrie24 at gmail dot com
  2023-08-17 22:42 ` [Bug c/111056] " pinskia at gcc dot gnu.org
@ 2023-08-17 23:11 ` alexhenrie24 at gmail dot com
  2023-08-18  0:50 ` alexhenrie24 at gmail dot com
  2023-08-18  0:55 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: alexhenrie24 at gmail dot com @ 2023-08-17 23:11 UTC (permalink / raw)
  To: gcc-bugs

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

Alex Henrie <alexhenrie24 at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |INVALID

--- Comment #2 from Alex Henrie <alexhenrie24 at gmail dot com> ---
You're right, the second warning is unnecessary because the behavior there is
not counterintuitive (although it would have been more clear if I had written
`c = UINT32_MAX` instead of `c = -1`). It's a bad example, sorry for the noise.
GCC does warn about legitimately counterintuitive comparisons involving enums.

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

* [Bug c/111056] Missing -Wsign-compare warning with enum values
  2023-08-17 22:16 [Bug c/111056] New: Missing -Wsign-compare warning with enum values alexhenrie24 at gmail dot com
  2023-08-17 22:42 ` [Bug c/111056] " pinskia at gcc dot gnu.org
  2023-08-17 23:11 ` alexhenrie24 at gmail dot com
@ 2023-08-18  0:50 ` alexhenrie24 at gmail dot com
  2023-08-18  0:55 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: alexhenrie24 at gmail dot com @ 2023-08-18  0:50 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Alex Henrie <alexhenrie24 at gmail dot com> ---
By the way, thanks for pointing out that using constexpr suppresses the warning
on GCC. Although Clang does not support constexpr in C yet, it is interesting
that Clang is smart enough to not warn about a plain const:

    const int a = 1;
    unsigned int b = 2;

    if (a < b) // warning here on GCC, but not on Clang
        puts("a < b");

I imagine that it would be difficult to change GCC's behavior to match Clang's
in this case.

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

* [Bug c/111056] Missing -Wsign-compare warning with enum values
  2023-08-17 22:16 [Bug c/111056] New: Missing -Wsign-compare warning with enum values alexhenrie24 at gmail dot com
                   ` (2 preceding siblings ...)
  2023-08-18  0:50 ` alexhenrie24 at gmail dot com
@ 2023-08-18  0:55 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-08-18  0:55 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Alex Henrie from comment #3) 
> I imagine that it would be difficult to change GCC's behavior to match
> Clang's in this case.

Except GCC does not warn at -O1 and above (there are a few other bug reports
about the difference at -O0 and -O1 already, e.g. PR 92507)

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

end of thread, other threads:[~2023-08-18  0:55 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-17 22:16 [Bug c/111056] New: Missing -Wsign-compare warning with enum values alexhenrie24 at gmail dot com
2023-08-17 22:42 ` [Bug c/111056] " pinskia at gcc dot gnu.org
2023-08-17 23:11 ` alexhenrie24 at gmail dot com
2023-08-18  0:50 ` alexhenrie24 at gmail dot com
2023-08-18  0:55 ` pinskia at gcc dot gnu.org

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).