public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "vincent-gcc at vinc17 dot net" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/112463] New: ternary operator / -Wsign-compare inconsistency
Date: Thu, 09 Nov 2023 16:38:32 +0000	[thread overview]
Message-ID: <bug-112463-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 112463
           Summary: ternary operator / -Wsign-compare inconsistency
           Product: gcc
           Version: 13.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vincent-gcc at vinc17 dot net
  Target Milestone: ---

-Wsign-compare is described in the man page as follows:

  -Wsign-compare
    Warn when a comparison between signed and unsigned  values  could
    produce an incorrect result when the signed value is converted to
    unsigned.   In C++, this warning is also enabled by -Wall.  In C,
    it is also enabled by -Wextra.

But it can emit a warning even in the absence of comparisons between signed and
unsigned values. For instance, it can appear due to the 2nd and 3rd operands of
the ternary operator (these operands are not compared, just selected from the
value of the first operand). This affects the warning output by -Wextra.

Consider the following C code:

#include <stdio.h>
int main (void)
{
  for (int c = -1; c <= 1; c++)
    {
      long long
        i = c == 0 ? 0LL : (c >= 0 ? 1U : -1),
        j = c >= 0 ? (c == 0 ? 0LL : 1U) : -1;
      printf ("i = %lld\nj = %lld\n", i, j);
    }
  return 0;
}

(which shows that the ternary operator is not associative due to type
conversions). With -Wextra, I get:

ternary-op.c: In function ‘main’:
ternary-op.c:7:43: warning: operand of ‘?:’ changes signedness from ‘int’ to
‘unsigned int’ due to unsignedness of other operand [-Wsign-compare]
    7 |         i = c == 0 ? 0LL : (c >= 0 ? 1U : -1),
      |                                           ^~

But the "-Wsign-compare" is incorrect as there are no comparisons between
signed and unsigned values. Only -Wsign-conversion should trigger a warning.

             reply	other threads:[~2023-11-09 16:38 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-09 16:38 vincent-gcc at vinc17 dot net [this message]
2023-11-10  3:01 ` [Bug c/112463] " egallager at gcc dot gnu.org
2023-11-10  3:22 ` pinskia at gcc dot gnu.org
2023-11-10  9:02 ` vincent-gcc at vinc17 dot net

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-112463-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).