public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/89408] No constant folding when dereferencing string literals
       [not found] <bug-89408-4@http.gcc.gnu.org/bugzilla/>
@ 2022-02-23 17:20 ` srk31 at srcf dot ucam.org
  2022-02-23 19:18 ` joseph at codesourcery dot com
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 4+ messages in thread
From: srk31 at srcf dot ucam.org @ 2022-02-23 17:20 UTC (permalink / raw)
  To: gcc-bugs

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

Stephen Kell <srk31 at srcf dot ucam.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |srk31 at srcf dot ucam.org

--- Comment #5 from Stephen Kell <srk31 at srcf dot ucam.org> ---
I just ran into this issue myself. Even though GCC is within the C language
spec in not supporting this case, there are at least the following reasons to
consider the reported behaviour a bug.

- GCC doesn't need to reject this code. C11 6.6 pt 10 says "An implementation
may accept other forms of constant expressions."

- The error message suggests that the omission is not intentional. If it really
was about language-lawyering, it should explicitly the C-language concept of
"integer constant expressions". Currently, what it says is actually false and
confusing, because the expression clearly *does* reduce to an integer constant.
It just isn't an "integer constant expression". To me it looks like the
compiler just happens not to be doing the necessary folding in that particular
case (no pun intended).

- GCC accepts these kinds of expressions in initializers, even though the C
language also does not require this to work (because they're not in the classes
of expression listed in C11 6.6 pt 7). Test case: int x = "C"[0];

- It makes the compiler more useful, has no apparent downside, Clang supports
it, etc etc. :-)

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

* [Bug c/89408] No constant folding when dereferencing string literals
       [not found] <bug-89408-4@http.gcc.gnu.org/bugzilla/>
  2022-02-23 17:20 ` [Bug c/89408] No constant folding when dereferencing string literals srk31 at srcf dot ucam.org
@ 2022-02-23 19:18 ` joseph at codesourcery dot com
  2022-02-24 12:43 ` srk31 at srcf dot ucam.org
  2022-09-21  0:57 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 4+ messages in thread
From: joseph at codesourcery dot com @ 2022-02-23 19:18 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from joseph at codesourcery dot com <joseph at codesourcery dot com> ---
Case labels are required to be integer constant expressions.  In most 
cases where an integer constant expression is required, something that 
isn't an integer constant expression but folds to an integer constant is 
accepted with a pedwarn-if-pedantic (see c-typeck.cc:do_case for this in 
the case of case labels, where the diagnostic is "case label is not an 
integer constant expression").  *But* note that this is mainly for 
compatibility with pre-GCC-4.5 code depending on folding that occurred 
before GCC 4.5; as I said in 
<https://gcc.gnu.org/pipermail/gcc/2021-July/236945.html>, it's not any 
sort of designed, documented extension with stable semantics that it would 
be a good idea to use.

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

* [Bug c/89408] No constant folding when dereferencing string literals
       [not found] <bug-89408-4@http.gcc.gnu.org/bugzilla/>
  2022-02-23 17:20 ` [Bug c/89408] No constant folding when dereferencing string literals srk31 at srcf dot ucam.org
  2022-02-23 19:18 ` joseph at codesourcery dot com
@ 2022-02-24 12:43 ` srk31 at srcf dot ucam.org
  2022-09-21  0:57 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 4+ messages in thread
From: srk31 at srcf dot ucam.org @ 2022-02-24 12:43 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Stephen Kell <srk31 at srcf dot ucam.org> ---
Thanks for the backstory. I understand that 'accept as much as possible' isn't
the goal, nor would it be wise.

The pedwarn message you quoted, which explicitly makes reference to integer
constant expressions, is a good one. What's not good is that the issued
diagnostic is not that. It is not coming from c/c-typeck.cc. It seems to be
coming from c-family/c-common.cc.

I now understand why this behaviour has arisen -- the pre-4.5 compatibility
hack, combined with whatever foldings the compiler happens to do. I would still
say it's not a desirable or desired behaviour.

Arguably more desirable would be a behaviour consistent with the analogous
extension that is allowed for initializers. Of course keeping the pedwarn --
and adding one for the initializer case, which currently lacks one. Being
neither stable nor documented is still fine for many purposes (including mine).

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

* [Bug c/89408] No constant folding when dereferencing string literals
       [not found] <bug-89408-4@http.gcc.gnu.org/bugzilla/>
                   ` (2 preceding siblings ...)
  2022-02-24 12:43 ` srk31 at srcf dot ucam.org
@ 2022-09-21  0:57 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-09-21  0:57 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |msebor at gcc dot gnu.org

--- Comment #8 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
*** Bug 106988 has been marked as a duplicate of this bug. ***

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

end of thread, other threads:[~2022-09-21  0:57 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <bug-89408-4@http.gcc.gnu.org/bugzilla/>
2022-02-23 17:20 ` [Bug c/89408] No constant folding when dereferencing string literals srk31 at srcf dot ucam.org
2022-02-23 19:18 ` joseph at codesourcery dot com
2022-02-24 12:43 ` srk31 at srcf dot ucam.org
2022-09-21  0:57 ` 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).