[Bug c/89408] No constant folding when dereferencing string literals

srk31 at srcf dot ucam.org gcc-bugzilla@gcc.gnu.org
Wed Feb 23 17:20:49 GMT 2022


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


More information about the Gcc-bugs mailing list