From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 621BD3857BA8; Thu, 22 Sep 2022 16:12:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 621BD3857BA8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1663863139; bh=5raclKSVlVKSTRGC2yxvk67hv4lsgUHLhE2MgD7aCjU=; h=From:To:Subject:Date:In-Reply-To:References:From; b=SRlXQPvr5RY1e3QgPHiLXuZyy5M7OHNJqj6dJ5u/oMlAeV18x+lShBCW6l8FE7rRr Tu+IPz+ugtpMjovAzWC9Z23Y+WgwGPUS6nmd8060a2qpPYFkTDeLPT6IQFfLu1z5BM xVJ38l3JjUp+y7F58D74UcjFStuwWZSPchf7JE1E= From: "msebor at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/106988] subscripting a string literal is not an integer constant expression but __builtin_strlen is Date: Thu, 22 Sep 2022 16:12:19 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Version: 12.0 X-Bugzilla-Keywords: rejects-valid X-Bugzilla-Severity: normal X-Bugzilla-Who: msebor at gcc dot gnu.org 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: resolution bug_status keywords Message-ID: In-Reply-To: References: 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=3D106988 Martin Sebor changed: What |Removed |Added ---------------------------------------------------------------------------- Resolution|DUPLICATE |--- Status|RESOLVED |UNCONFIRMED Keywords|diagnostic |rejects-valid --- Comment #2 from Martin Sebor --- After experimenting with this some more I think the decision to reject this= and the other similar requests should be reconsidered. GCC accepts many equiva= lent nonconstant expressions in contexts where they are required (see below).=20 Rejecting the simplest of them seems arbitrary and is (as is evident from t= he duplicate requests) unhelpful to C programmers and surprising those used to= the C++ behavior. $ cat a.c && gcc -S -Wall -Wpedantic a.c #include _Static_assert (memcmp ("", "", 1) =3D=3D 0, ""); // okay in C mode only _Static_assert (memchr ("", 0, 1) !=3D 0, ""); // okay in C mode only _Static_assert (strlen ("") =3D=3D 0, ""); // okay in C and C++ _Static_assert (strcmp ("", "") =3D=3D 0, ""); // okay in C and C++ _Static_assert ("" =3D=3D "", ""); // okay in C and C++ _Static_assert (*"" =3D=3D 0, ""); // error in C only a.c:3:36: warning: expression in static assertion is not an integer constant expression [-Wpedantic] 3 | _Static_assert (memcmp ("", "", 1) =3D=3D 0, ""); // okay in C mod= e only | ~~~~~~~~~~~~~~~~~~~^~~~ a.c:4:35: warning: expression in static assertion is not an integer constant expression [-Wpedantic] 4 | _Static_assert (memchr ("", 0, 1) !=3D 0, ""); // okay in C mode = only | ~~~~~~~~~~~~~~~~~~^~~~ a.c:5:29: warning: expression in static assertion is not an integer constant expression [-Wpedantic] 5 | _Static_assert (strlen ("") =3D=3D 0, ""); // okay in C and= C++ | ~~~~~~~~~~~~^~~~ a.c:6:33: warning: expression in static assertion is not an integer constant expression [-Wpedantic] 6 | _Static_assert (strcmp ("", "") =3D=3D 0, ""); // okay in C and= C++ | ~~~~~~~~~~~~~~~~^~~~ a.c:7:20: warning: comparison with string literal results in unspecified behavior [-Waddress] 7 | _Static_assert ("" =3D=3D "", ""); // okay in C and= C++ | ^~ a.c:7:20: warning: expression in static assertion is not an integer constant expression [-Wpedantic] 7 | _Static_assert ("" =3D=3D "", ""); // okay in C and= C++ | ~~~^~~~~ a.c:9:21: error: expression in static assertion is not constant 9 | _Static_assert (*"" =3D=3D 0, ""); // error in C on= ly | ~~~~^~~~=