https://github.com/Ayush3941 updated 
https://github.com/llvm/llvm-project/pull/182707

>From a36f1ac769ac7a770a1eafd160f3f40a1bdf75fd Mon Sep 17 00:00:00 2001
From: Ayush3941 <[email protected]>
Date: Mon, 16 Mar 2026 11:01:26 -0400
Subject: [PATCH 1/3] [Clang][Sema] Propagate invalidity for local static
 member definitions

---
 clang/lib/Sema/SemaDecl.cpp          |  7 ++++++-
 clang/test/SemaTemplate/GH176152.cpp | 12 ++++++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/SemaTemplate/GH176152.cpp

diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 3db91b00f9d80..4fb74827ee01c 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -4702,6 +4702,9 @@ void Sema::MergeVarDecl(VarDecl *New, LookupResult 
&Previous) {
     return New->setInvalidDecl();
   }
 
+  if (Old->isInvalidDecl())
+    return New->setInvalidDecl();
+
   // If the old declaration was found in an inline namespace and the new
   // declaration was qualified, update the DeclContext to match.
   adjustDeclContextForDeclaratorDecl(New, Old);
@@ -7929,7 +7932,7 @@ NamedDecl *Sema::ActOnVariableDeclarator(
 
     if (CurContext->isRecord()) {
       if (SC == SC_Static) {
-        if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC)) {
+        if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC)) {
           // Walk up the enclosing DeclContexts to check for any that are
           // incompatible with static data members.
           const DeclContext *FunctionOrMethod = nullptr;
@@ -7951,6 +7954,8 @@ NamedDecl *Sema::ActOnVariableDeclarator(
             Diag(D.getIdentifierLoc(),
                  diag::err_static_data_member_not_allowed_in_local_class)
                 << Name << RD->getDeclName() << RD->getTagKind();
+            Invalid = true;
+            RD->setInvalidDecl();
           } else if (AnonStruct) {
             // C++ [class.static.data]p4: Unnamed classes and classes contained
             // directly or indirectly within unnamed classes shall not contain
diff --git a/clang/test/SemaTemplate/GH176152.cpp 
b/clang/test/SemaTemplate/GH176152.cpp
new file mode 100644
index 0000000000000..7d61aa292982d
--- /dev/null
+++ b/clang/test/SemaTemplate/GH176152.cpp
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -std=c++23 -fsyntax-only -verify %s
+
+template <class T> int f(T) {
+  struct MyClass {
+    static int staticField;
+    // expected-error@-1 {{static data member 'staticField' not allowed in 
local struct 'MyClass'}}
+  };
+  int MyClass::staticField = 42;
+  return 0;
+}
+
+int x = f(0);

>From 4d10eddfd0ac0f5ca6197524083cf7a56de9e167 Mon Sep 17 00:00:00 2001
From: Ayush3941 <[email protected]>
Date: Mon, 16 Mar 2026 11:08:05 -0400
Subject: [PATCH 2/3] [Clang][Sema] added a release note

---
 clang/docs/ReleaseNotes.rst | 1 +
 1 file changed, 1 insertion(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index b1d7c77e65958..0193283075045 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -296,6 +296,7 @@ Bug Fixes to C++ Support
 - Fixed a crash when instantiating ``requires`` expressions involving 
substitution failures in C++ concepts. (#GH176402)
 - Fixed a crash when a default argument is passed to an explicit object 
parameter. (#GH176639)
 - Fixed a crash when diagnosing an invalid static member function with an 
explicit object parameter (#GH177741)
+- Fixed a crash when instantiating an invalid out-of-line static data member 
definition in a local class. (#GH176152)
 - Fixed a bug where captured variables in non-mutable lambdas were incorrectly 
treated as mutable 
   when used inside decltype in the return type. (#GH180460)
 - Fixed a crash when evaluating uninitialized GCC vector/ext_vector_type 
vectors in ``constexpr``. (#GH180044)

>From 98d720b25836e3c3cae5d32e98c4357eaf5222bc Mon Sep 17 00:00:00 2001
From: Ayush Kumar Gaur <[email protected]>
Date: Mon, 16 Mar 2026 11:41:48 -0400
Subject: [PATCH 3/3] removing broad MergeVarDecl change

---
 clang/lib/Sema/SemaDecl.cpp | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 0ff635d19e9cf..c5e8046c2b5e5 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -4715,9 +4715,6 @@ void Sema::MergeVarDecl(VarDecl *New, LookupResult 
&Previous) {
     return New->setInvalidDecl();
   }
 
-  if (Old->isInvalidDecl())
-    return New->setInvalidDecl();
-
   // If the old declaration was found in an inline namespace and the new
   // declaration was qualified, update the DeclContext to match.
   adjustDeclContextForDeclaratorDecl(New, Old);

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to