[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:34:54 PDT 2025


================
@@ -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() ||
----------------
AaronBallman wrote:

> Does lexical decl context come into play here at all?

C doesn't have the notion of out-of-line declarations, so no.

> Also, and this might be my C++-brain firing here, but is this sufficient? I know in C++ we typically check `isFileContextDecl` but IIRC that is just namespaces...

`isFileContext()` checks for TU and namespaces, but C has no namespaces so only the TU is needed.

> Also-also-- what about structs who are defined 'inline' (or whatever y'all call it)... are those never equal too? Something like defined in a param list, or in an enum-initializer, or function scope?

I think existing tests already cover that, such as the one for `func1` and `func2` in this file. Keep in mind that C is a bit odd when you come from a C++ background:
```
struct S {
  struct T {
    int x;
  } t;
};

struct T wahoo = {}; // Perfectly fine in C, not fine in C++
```

> So I wonder if we want to invert this and check if decl-context is a RecordDecl?

I think the logic will work either way, but now I need to add more tests; what happens if the structures are within a function? :-D

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


More information about the cfe-commits mailing list