From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 9638C3858CD1; Thu, 9 Nov 2023 16:38:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9638C3858CD1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1699547913; bh=fe8J27SnLHLjh/igeMo9V6ibaoPEBD6IoeO8t4pTa9g=; h=From:To:Subject:Date:From; b=cLh1xaYJ/Ugfq0I4fRgn5Tw/Px3t35MkCy0wWBadUJjkXn07tANgEUpvAG8DbFcU9 +2vwulZnlq1iKjjKQxmWxLSqYIYzELetm08tKbIZU8kUuymZEtJ+Fsa4y74KDS2fLj 2tsIA8o8zSkt2jaWFn/0kvgquqBGz2RwA/Mu4EPo= From: "vincent-gcc at vinc17 dot net" 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 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Version: 13.2.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: vincent-gcc at vinc17 dot net X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D112463 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 operand= s of the ternary operator (these operands are not compared, just selected from t= he value of the first operand). This affects the warning output by -Wextra. Consider the following C code: #include int main (void) { for (int c =3D -1; c <=3D 1; c++) { long long i =3D c =3D=3D 0 ? 0LL : (c >=3D 0 ? 1U : -1), j =3D c >=3D 0 ? (c =3D=3D 0 ? 0LL : 1U) : -1; printf ("i =3D %lld\nj =3D %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 =E2=80=98main=E2=80=99: ternary-op.c:7:43: warning: operand of =E2=80=98?:=E2=80=99 changes signedn= ess from =E2=80=98int=E2=80=99 to =E2=80=98unsigned int=E2=80=99 due to unsignedness of other operand [-Wsign= -compare] 7 | i =3D c =3D=3D 0 ? 0LL : (c >=3D 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= .=