C++: Diagnose objects with anonymous union/struct types per CWG3130
GCC now diagnoses uses of anonymous unions and structs that violate CWG3130's intent, preventing certain declarations and derived types.
Following C++ standard clarification CWG3130, GCC now diagnoses declarations of objects with anonymous union or struct types. This prevents issues like ICEs and unexpected behavior that could arise from reusing anonymous types. The compiler will now error on variable declarations, function parameters, class members, and array types involving anonymous unions or structs. It also disallows anonymous structs from being used as base classes.
In Details
CWG3130 from the C++ committee intends to clarify that objects of anonymous aggregate types (unions and structs) are themselves considered anonymous. This commit implements diagnostic checks for constructs that would violate this principle, such as variable declarations of anonymous types, members of anonymous types, and derived types from anonymous structs. Previously, some such declarations could lead to compiler internal errors (ICEs) or misinterpretations of type identity during overload resolution or lookup. The implementation in typeck2.cc and semantics.cc checks for these cases at…
- anonymous union or struct
- A union or struct declared without a name. While these can be used within other structs or classes, their direct use as a type for standalone objects or members has historically been tricky and is now being further constrained by the C++ standard.
- CWG3130
- A defect report and resolution from the C++ Core Working Group that clarifies the rules regarding objects of anonymous union and struct types.
- ICE
- Internal Compiler Error. An error condition where the compiler encounters an unexpected state and terminates abnormally.
- derived types
- In C++, types that inherit from other types (base classes). This includes class inheritance and potentially types formed by arraying or punning other types.