Skip to content

Wrong codegen for overload set with aggregate designated initializers and anonymous union #59868

@ecatmur

Description

@ecatmur
struct C {
  union {
    int a;
    const char* p;
  };
};
struct D {
  int a = 1;
  const char* p = "bad";
};
int f(C);
int f(D);
int i = f({.a = 2, .p = "ok"});

Clang should either reject as ambiguous, or remove f(C) from the overload set under https://wg21.link/dcl.init.aggr#4.1.sentence-2 and call f(D) with a D{.a = 2, .p = "ok"} (the standard is not particularly clear on this; this came up discussing CWG 2169). It should not call f(D) with a D{} (i.e. D{.a = 1, .p = "bad"}), but that's what it does.

Also, it comes up with a comedy warning:

warning: ISO C++ requires field designators to be specified in declaration order; field 'p' will be initialized after field '' [-Wreorder-init-list]
<source>:13:20: warning: initializer overrides prior initialization of this subobject [-Winitializer-overrides]
int i = f({.a = 2, .p = "ok"});
                   ^~~~~~~~~
<source>:13:17: note: previous initialization is here
int i = f({.a = 2, .p = "ok"});
                ^

If you remove the declaration f(C);, it DTRT.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerclang:frontendLanguage frontend issues, e.g. anything involving "Sema"waiting-for-wg21Blocked on C++ Standards Committee

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions