public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "jakub at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/105510] error: initializer element is not constant
Date: Mon, 09 May 2022 13:00:35 +0000	[thread overview]
Message-ID: <bug-105510-4-B9ozjTvRXQ@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-105510-4@http.gcc.gnu.org/bugzilla/>

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

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

--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
(struct Test2){3, 4}
is a compound literal, see
https://en.cppreference.com/w/c/language/compound_literal
for further details, so it isn't a valid constant expression, it is like
doing
struct Test2
{
    long int x;
    long int y;
};

struct Test
{
    long int x;
    struct Test2 t;
};

struct Test2 tmp2={3, 4};
struct Test tmp1={1, tmp2};
struct Test t=tmp1;
except that the compound literals introduce unnamed, not named objects.
The above is also rejected, by both GCC and Clang.
Note, implementations may accept as constant expressions even expressions the
standard doesn't require to be constant expressions, so probably that is the
reason why Clang chooses to accept it.  Though, at least without using const
struct Test{,2} in the compound literals it is actually an unnamed object that
can be modified, so it is weird it is accepted.
The above testcase with tmp2/tmp1 is as an extension accepted by GCC when one
uses const struct Test2 or const struct Test (but rejected by Clang), though
if you use it in the compound literals, we don't accept that.

  parent reply	other threads:[~2022-05-09 13:00 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-06 18:55 [Bug c/105510] New: [12] " 570070308 at qq dot com
2022-05-09  8:04 ` [Bug c/105510] " rguenth at gcc dot gnu.org
2022-05-09 12:21 ` 570070308 at qq dot com
2022-05-09 13:00 ` jakub at gcc dot gnu.org [this message]
2022-05-09 20:39 ` joseph at codesourcery dot com
2023-12-04 13:46 ` andy.shevchenko at gmail dot com

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-105510-4-B9ozjTvRXQ@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).