public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/105510] New: [12] error: initializer element is not constant
@ 2022-05-06 18:55 570070308 at qq dot com
  2022-05-09  8:04 ` [Bug c/105510] " rguenth at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: 570070308 at qq dot com @ 2022-05-06 18:55 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 105510
           Summary: [12] error: initializer element is not constant
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: 570070308 at qq dot com
  Target Milestone: ---

clang can success compile it but gcc can't. MSVC can't too, I'm not sure this
is a bug.
test.c
```
struct Test2
{
    long int x;
    long int y;
};

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

struct Test t=(struct Test){1, (struct Test2){3, 4}};
```
gcc version: gcc-11.3 or gcc-12.0.1

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

* [Bug c/105510] error: initializer element is not constant
  2022-05-06 18:55 [Bug c/105510] New: [12] error: initializer element is not constant 570070308 at qq dot com
@ 2022-05-09  8:04 ` rguenth at gcc dot gnu.org
  2022-05-09 12:21 ` 570070308 at qq dot com
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-05-09  8:04 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|[12] error: initializer     |error: initializer element
                   |element is not constant     |is not constant
                 CC|                            |jsm28 at gcc dot gnu.org
      Known to fail|                            |4.3.5, 7.5.0

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
As a workaround it works with

struct Test t=(struct Test){1, {3, 4}};

I don't think it your way of writing is actually valid though.

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

* [Bug c/105510] error: initializer element is not constant
  2022-05-06 18:55 [Bug c/105510] New: [12] error: initializer element is not constant 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
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: 570070308 at qq dot com @ 2022-05-09 12:21 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from 。 <570070308 at qq dot com> ---
(In reply to Richard Biener from comment #1)
> As a workaround it works with
> 
> struct Test t=(struct Test){1, {3, 4}};
> 
> I don't think it your way of writing is actually valid though.

Yes, I'm not sure that this is a Bug. I meet this error when I'm using macro to
init a struct. For example:
```test.c
struct Test2
{
    long int x;
    long int y;
};

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

#define TEST2_INIT(x, y) ((struct Test2){x, y})

#define TEST_INIT(x) ((struct Test){x, TEST2_INIT(1, 2)})

struct Test test=TEST_INIT(0);  // error
```

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

* [Bug c/105510] error: initializer element is not constant
  2022-05-06 18:55 [Bug c/105510] New: [12] error: initializer element is not constant 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
  2022-05-09 20:39 ` joseph at codesourcery dot com
  2023-12-04 13:46 ` andy.shevchenko at gmail dot com
  4 siblings, 0 replies; 6+ messages in thread
From: jakub at gcc dot gnu.org @ 2022-05-09 13:00 UTC (permalink / raw)
  To: gcc-bugs

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.

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

* [Bug c/105510] error: initializer element is not constant
  2022-05-06 18:55 [Bug c/105510] New: [12] error: initializer element is not constant 570070308 at qq dot com
                   ` (2 preceding siblings ...)
  2022-05-09 13:00 ` jakub at gcc dot gnu.org
@ 2022-05-09 20:39 ` joseph at codesourcery dot com
  2023-12-04 13:46 ` andy.shevchenko at gmail dot com
  4 siblings, 0 replies; 6+ messages in thread
From: joseph at codesourcery dot com @ 2022-05-09 20:39 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from joseph at codesourcery dot com <joseph at codesourcery dot com> ---
We have a documented extension:

    As a GNU extension, GCC allows initialization of objects with static
storage
    duration by compound literals (which is not possible in ISO C99 because
    the initializer is not a constant).
    It is handled as if the object were initialized only with the
brace-enclosed
    list if the types of the compound literal and the object match.
    The elements of the compound literal must be constant.
    If the object being initialized has array type of unknown size, the size is
    determined by the size of the compound literal.

So the question is whether this extension should also allow the case where 
a compound literal is used to initialize a sub-object.

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

* [Bug c/105510] error: initializer element is not constant
  2022-05-06 18:55 [Bug c/105510] New: [12] error: initializer element is not constant 570070308 at qq dot com
                   ` (3 preceding siblings ...)
  2022-05-09 20:39 ` joseph at codesourcery dot com
@ 2023-12-04 13:46 ` andy.shevchenko at gmail dot com
  4 siblings, 0 replies; 6+ messages in thread
From: andy.shevchenko at gmail dot com @ 2023-12-04 13:46 UTC (permalink / raw)
  To: gcc-bugs

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

Andy Shevchenko <andy.shevchenko at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andy.shevchenko at gmail dot com

--- Comment #5 from Andy Shevchenko <andy.shevchenko at gmail dot com> ---
Isn't this a dup of bug #71713?

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

end of thread, other threads:[~2023-12-04 13:46 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-06 18:55 [Bug c/105510] New: [12] error: initializer element is not constant 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
2022-05-09 20:39 ` joseph at codesourcery dot com
2023-12-04 13:46 ` andy.shevchenko at gmail dot com

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