Skip to content

[C23] Handle type compatibility of unnamed records #141783

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 29, 2025

Conversation

AaronBallman
Copy link
Collaborator

At the top-level, both types need to have a tag in order for them to be compatible within the same TU in C23. An unnamed structure has no tag, so it cannot be compatible with another type within the TU.

Fixes #141724

At the top-level, both types need to have a tag in order for them to be
compatible within the same TU in C23. An unnamed structure has no tag,
so it cannot be compatible with another type within the TU.

Fixes llvm#141724
@AaronBallman AaronBallman added clang Clang issues not falling into any other category c23 clang:frontend Language frontend issues, e.g. anything involving "Sema" accepts-invalid labels May 28, 2025
@llvmbot
Copy link
Member

llvmbot commented May 28, 2025

@llvm/pr-subscribers-clang

Author: Aaron Ballman (AaronBallman)

Changes

At the top-level, both types need to have a tag in order for them to be compatible within the same TU in C23. An unnamed structure has no tag, so it cannot be compatible with another type within the TU.

Fixes #141724


Full diff: https://github.com/llvm/llvm-project/pull/141783.diff

2 Files Affected:

  • (modified) clang/lib/AST/ASTStructuralEquivalence.cpp (+13-2)
  • (modified) clang/test/C/C23/n3037.c (+27-1)
diff --git a/clang/lib/AST/ASTStructuralEquivalence.cpp b/clang/lib/AST/ASTStructuralEquivalence.cpp
index c6df340cbdf0a..20dcaab7d90d3 100644
--- a/clang/lib/AST/ASTStructuralEquivalence.cpp
+++ b/clang/lib/AST/ASTStructuralEquivalence.cpp
@@ -1751,9 +1751,20 @@ static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
   // fulfill the preceding requirements. ... Otherwise, the structure, union,
   // or enumerated types are incompatible.
 
-  if (!NameIsStructurallyEquivalent(*D1, *D2)) {
+  // Note: "the same tag" refers to the identifier for the structure; two
+  // structures without names are not compatible within a TU. In C23, if either
+  // declaration has no name, they're not equivalent. However, the paragraph
+  // after the bulleted list goes on to talk about compatibility of anonymous
+  // structure and union members, so this prohibition only applies to top-level
+  // declarations, not members.
+  if (Context.LangOpts.C23 && (!D1->getIdentifier() || !D2->getIdentifier()) &&
+      (D1->getDeclContext()->isTranslationUnit() ||
+       D2->getDeclContext()->isTranslationUnit()))
+    return false;
+
+  // Otherwise, check the names for equivalence.
+  if (!NameIsStructurallyEquivalent(*D1, *D2))
     return false;
-  }
 
   if (D1->isUnion() != D2->isUnion()) {
     if (Context.Complain) {
diff --git a/clang/test/C/C23/n3037.c b/clang/test/C/C23/n3037.c
index 121b220323e83..03dc78d9c8633 100644
--- a/clang/test/C/C23/n3037.c
+++ b/clang/test/C/C23/n3037.c
@@ -140,7 +140,7 @@ struct quals_matter { // c17-note {{previous definition is here}}
 };
 
 struct quals_matter { // c17-error {{redefinition of 'quals_matter'}} \
-                         c23-error {{type 'struct quals_matter' has incompatible definitions}}                         
+                         c23-error {{type 'struct quals_matter' has incompatible definitions}}
   const int x;        // c23-note {{field 'x' has type 'const int' here}}
 };
 
@@ -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));
+
+
+// Test the same thing with enumerations (test for unions is omitted because
+// unions and structures are both RecordDecl objects, whereas EnumDecl is not).
+enum { E_Untagged1 } nontag_enum; // both-note {{previous definition is here}}
+_Static_assert(0 == _Generic(nontag_enum, enum { E_Untagged1 } : 1, default : 0)); // both-error {{redefinition of enumerator 'E_Untagged1'}}

* Switched the logic to !isRecord() instead of isTranslationUnit()
* Added a test for a case we got wrong
* Updated a comment for clarity
@AaronBallman AaronBallman merged commit 6769a83 into llvm:main May 29, 2025
10 of 11 checks passed
@AaronBallman AaronBallman deleted the aballman-gh141724 branch May 29, 2025 10:59
svkeerthy pushed a commit that referenced this pull request May 29, 2025
At the top-level, both types need to have a tag in order for them to be
compatible within the same TU in C23. An unnamed structure has no tag,
so it cannot be compatible with another type within the TU.

Fixes #141724
google-yfyang pushed a commit to google-yfyang/llvm-project that referenced this pull request May 29, 2025
At the top-level, both types need to have a tag in order for them to be
compatible within the same TU in C23. An unnamed structure has no tag,
so it cannot be compatible with another type within the TU.

Fixes llvm#141724
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepts-invalid c23 clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Clang] C23 struct compatiblity rule differs from GCC
4 participants