Bug 72751 - anonymous union within an anonymous union accepted without diagnostic (i.e. add -Wnested-anon-types)
Summary: anonymous union within an anonymous union accepted without diagnostic (i.e. a...
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 7.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
Keywords: accepts-invalid, diagnostic
: 113919 (view as bug list)
Depends on:
Blocks: new-warning, new_warning
  Show dependency treegraph
Reported: 2016-07-29 21:17 UTC by Martin Sebor
Modified: 2024-02-14 16:29 UTC (History)
5 users (show)

See Also:
Known to work:
Known to fail:
Last reconfirmed: 2021-07-27 00:00:00


Note You need to log in before you can comment on or make changes to this bug.
Description Martin Sebor 2016-07-29 21:17:45 UTC
Quoting from C++14, [class.union.anon], p1:

Each member-declaration in the member-specification of an anonymous union shall either define a non-static data member or be a static_assert-declaration. [Note: Nested types, anonymous unions, and functions cannot be declared within
an anonymous union. -- end note]

That makes the following program ill-formed.  In C mode (where the program is also ill-formed) GCC issues a warning for the nested union and an error for the use of a.  However in C++ mode, G++ fails to issue a diagnostic for it, even in pedantic mode.  Clang issues:

  warning: anonymous types declared in an anonymous union are an extension [-Wnested-anon-types]

and EDG eccp 4.11:

   error: types cannot be declared in anonymous unions

$ cat t.C && gcc -S -Wall -Wextra -Wpedantic t.C
void f ()
  union { union { int a, b; }; };
  a = 0;
Comment 1 Eric Gallager 2017-08-28 15:24:19 UTC
Comment 2 Eric Gallager 2018-10-10 05:06:31 UTC
Since implementing the warning that clang gives would count as a new warning, making this block the meta-bug for new warnings
Comment 3 Marek Polacek 2024-02-14 16:26:45 UTC
*** Bug 113919 has been marked as a duplicate of this bug. ***
Comment 4 Marek Polacek 2024-02-14 16:27:56 UTC
Test from Bug 113919:

struct _ {
        union {
            int __;

int main(){return 0;}
Comment 5 Marek Polacek 2024-02-14 16:29:11 UTC
We should also make sure that __extension__ suppresses the -Wnested-anon-types diagnostic.