[clang] [Clang] Fixed an assertion failure in constant evaluation/AST parsing when encountering malformed struct and enum declarations with missing closing braces (PR #200214)

Shengxin Pei via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 1 22:55:38 PST 2026


https://github.com/TPPPP72 updated https://github.com/llvm/llvm-project/pull/200214

>From 971a22c5969ad8172dd3c68df543cdde9b8046e1 Mon Sep 17 00:00:00 2001
From: Shengxin Pei <TPPPP72 at outlook.com>
Date: Wed, 2 Dec 2026 14:55:19 +0800
Subject: [PATCH] [Clang] Fixed an assertion failure in constant evaluation/AST
 parsing when encountering malformed struct and enum declarations with missing
 closing braces

---
 clang/docs/ReleaseNotes.md   |  1 +
 clang/lib/Sema/SemaDecl.cpp  |  5 ++++
 clang/test/Sema/gh118061.cpp | 48 ++++++++++++++++++++++++++++++++++++
 3 files changed, 54 insertions(+)
 create mode 100644 clang/test/Sema/gh118061.cpp

diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 676d2b2761c2fa..cbf21d9f5271af 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1011,6 +1011,7 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
 - Fixed conversion to a C23 enumeration with an underlying type of `bool`, which truncated the value instead of converting it like `bool` (e.g. `(enum E : bool)2` was 0 instead of 1). This also affected compound assignment and increment. (#GH241289)
 - Fixed `#embed` of a missing file not being diagnosed if `-MD` is used. (#GH247359)
 - Fixed `__has_embed` return value with `limit` greater than the file size. (#GH247344)
+- Fixed an assertion failure in constant evaluation/AST parsing when encountering malformed struct and enum declarations with missing closing braces. (#GH118061)
 
 #### Bug Fixes to Compiler Builtins
 
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 26f71ed64fd972..2719baf0c4ec75 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -19173,6 +19173,11 @@ Sema::ActOnTag(Scope *S, unsigned TagSpec, TagUseKind TUK, SourceLocation KWLoc,
     Invalid = true;
   }
 
+  // Impossible to declare struct/union/class in enum.
+  if (!Invalid && getLangOpts().CPlusPlus && TUK == TagUseKind::Declaration &&
+      DC->getDeclKind() == Decl::Enum)
+    Invalid = true;
+
   // Maybe add qualifier info.
   if (SS.isNotEmpty()) {
     if (SS.isSet()) {
diff --git a/clang/test/Sema/gh118061.cpp b/clang/test/Sema/gh118061.cpp
new file mode 100644
index 00000000000000..7a1ff403d0f85a
--- /dev/null
+++ b/clang/test/Sema/gh118061.cpp
@@ -0,0 +1,48 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+// expected-warning at +3 {{declaration does not declare anything}}
+// expected-note at +2 {{to match this '{'}}
+// expected-error at +1 {{anonymous structs and classes must be class members}}
+struct {
+    // expected-error at +1 {{types cannot be declared in an anonymous struct}}
+    enum a1 {
+        b1 = (struct c1
+    // expected-error at -1 {{expected ';' after struct}}
+    // expected-note at -2 {{to match this '('}}
+};
+// expected-error at -1 {{expected ')'}}
+
+// expected-error at +1 {{types cannot be declared in an anonymous struct}}
+struct c1 {
+};
+
+// expected-note at +1 {{to match this '{'}}
+union {
+    enum a2 {
+        b2 = (union c2
+    // expected-error at -1 {{expected ';' after union}}
+    // expected-note at -2 {{to match this '('}}
+};
+// expected-error at -1 {{expected ')'}}
+
+union c2 {
+};
+
+// expected-note at +2 {{to match this '{'}}
+// expected-warning at +1 {{declaration does not declare anything}}
+class {
+    // expected-error at +1 {{types cannot be declared in an anonymous struct}}
+    enum a3 {
+        b3 = (class c3
+    // expected-error at -1 {{expected ';' after class}}
+    // expected-note at -2 {{to match this '('}}
+    // expected-note at -3 {{previously declared 'public' here}}
+};
+// expected-error at -1 {{expected ')'}}
+
+// expected-error at +2 {{types cannot be declared in an anonymous struct}}
+// expected-error at +1 {{'c3' redeclared with 'private' access}}
+class c3 {
+};
+// expected-error at -1 {{expected ';' after class}}
+// expected-error {{expected ';' after struct}} \ expected-error {{expected ';' after union}} \ expected-error 3 {{expected '}'}}



More information about the cfe-commits mailing list