[clang] [clang] Fix a crash issue that caused by handling of fields with initializers in nested anonymous unions (PR #113049)

Eli Friedman via cfe-commits cfe-commits at lists.llvm.org
Sun Oct 20 23:05:49 PDT 2024


================
@@ -115,3 +115,14 @@ namespace nested_union {
   // of Test3, or we should exclude f(Test3) as a candidate.
   static_assert(f({1}) == 2, ""); // expected-error {{call to 'f' is ambiguous}}
 }
+
+// Fix crash issue https://github.com/llvm/llvm-project/issues/112560.
+// Make sure clang compiles the following code without crashing:
+namespace GH112560 {
+union U {
+  int f = ; // expected-error {{expected expression}}
----------------
efriedma-quic wrote:

As far as I can tell, the reason the code in SemaInit is getting confused is that the union is marked "hasInClassInitializer" because of the initializer... but then there isn't actually an initializer anywhere because we failed to parse it.

I think hasInClassInitializer() on RecordDecl needs to be consistent with hasInClassInitializer() on FieldDecl.  If  hasInClassInitializer is true, there should be an initializer somewhere.  Either we should pretend the user didn't write the "=", or we should construct a RecoveryExpr to represent the missing initializer.  Adding extra handling to every bit of code that checks hasInClassInitializer spreads out error handling to places it shouldn't exist.

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


More information about the cfe-commits mailing list