-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[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
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
@llvm/pr-subscribers-clang Author: Aaron Ballman (AaronBallman) ChangesAt 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:
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'}}
|
erichkeane
reviewed
May 28, 2025
Fznamznon
reviewed
May 28, 2025
* Switched the logic to !isRecord() instead of isTranslationUnit() * Added a test for a case we got wrong * Updated a comment for clarity
Fznamznon
approved these changes
May 28, 2025
erichkeane
approved these changes
May 28, 2025
erichkeane
reviewed
May 28, 2025
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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