public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/94558] New: Designated initializer inside _Generic is misinterpreted
@ 2020-04-11  7:49 elronnd at elronnd dot net
  2020-04-15 21:06 ` [Bug c/94558] " jsm28 at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: elronnd at elronnd dot net @ 2020-04-11  7:49 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 94558
           Summary: Designated initializer inside _Generic is
                    misinterpreted
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: elronnd at elronnd dot net
  Target Milestone: ---

POC:

typedef struct{int _;} A;
typedef struct{int _;} B;

typedef union {
        A a;
        B b;
} O;

#define NO(o) _Generic((o), \
        A: ((O){.a=o}), \
        B: ((O){.b=o}))

int main() {
        A a;
        O o = NO(a);
}


Generates the error message:

incompatible types when initializing type ‘int’ using type ‘A’ {aka ‘struct
<anonymous>’}

Because it's trying to initialize the '_' field in 'A' with 'a', instead of the
'a' field in 'O'.

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

* [Bug c/94558] Designated initializer inside _Generic is misinterpreted
  2020-04-11  7:49 [Bug c/94558] New: Designated initializer inside _Generic is misinterpreted elronnd at elronnd dot net
@ 2020-04-15 21:06 ` jsm28 at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: jsm28 at gcc dot gnu.org @ 2020-04-15 21:06 UTC (permalink / raw)
  To: gcc-bugs

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

Joseph S. Myers <jsm28 at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |INVALID
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #1 from Joseph S. Myers <jsm28 at gcc dot gnu.org> ---
This is not a bug.

First, note that all expressions inside _Generic must satisfy all syntactic and
compile-time constraints and must not involve any compile-time undefined
behavior, just like an expression inside "if (0)"; it's simply that only one of
the expressions ends up being evaluated at runtime when the _Generic expression
is executed. So one of the compound literals tries to use an object of type A
to initialize element b of the union, which results in the given error.

Next, the error refers to initializing the field _ (i.e. .b._) with an
incompatible type rather than to initializing .b with an incompatible type
because the rule for initializing subaggregates and contained unions, in the
absence of braces, is that an initializer with the correct structure or union
type initializes the whole of a contained subaggregate or union, but otherwise
the initializer goes down into nested subobjects.  So when the initializer
doesn't match the type of the field named by the designator, or the first field
of that field (where an aggregate or union), or the first field of that,
recursively, it ends up being interpreted as an initializer for the scalar at
the start of that object; that is, the int at the start of .b. So the error
message refers to the type int.

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

end of thread, other threads:[~2020-04-15 21:06 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-11  7:49 [Bug c/94558] New: Designated initializer inside _Generic is misinterpreted elronnd at elronnd dot net
2020-04-15 21:06 ` [Bug c/94558] " jsm28 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).