[clang] [C23] Handle type compatibility of unnamed records (PR #141783)
Aaron Ballman via cfe-commits
cfe-commits at lists.llvm.org
Wed May 28 08:31:01 PDT 2025
================
@@ -359,3 +359,29 @@ struct alignment { // c17-error {{redefinition of 'alignment'}} \
c23-error {{type 'struct alignment' has a member with an attribute which currently causes the types to be treated as though they are incompatible}}
int x;
};
+
+// Both structures need to have a tag in order to be compatible within the same
+// translation unit.
+struct {int i;} nontag;
+struct tag {int i;} tagged; // c17-note 2 {{previous definition is here}}
+
+_Static_assert(1 == _Generic(tagged, struct tag {int i;}:1, default:0)); // c17-error {{redefinition of 'tag'}} \
+ c17-error {{static assertion failed}}
+_Static_assert(0 == _Generic(tagged, struct {int i;}:1, default:0));
+_Static_assert(0 == _Generic(nontag, struct tag {int i;}:1, default:0)); // c17-error {{redefinition of 'tag'}}
+// That means these two structures are not actually compatible; see GH141724.
+_Static_assert(0 == _Generic(nontag, struct {int i;}:1, default:0));
+
+struct InnerAnonStruct {
+ struct {
+ int i;
+ } untagged;
+} inner_anon_tagged;
+
+_Static_assert(0 == _Generic(inner_anon_tagged.untagged, struct { int i; } : 1, default : 0));
----------------
AaronBallman wrote:
Maybe I should reword the comment; you have to consider both types involved, so if either one is unnamed at the top-level, the two types cannot be compatible within the same TU. In this case, the type in the generic association is unnamed at the top level, so the assertion passes because _Generic results in 0.
https://github.com/llvm/llvm-project/pull/141783
More information about the cfe-commits
mailing list