[clang] [Clang] Fixed a crash when instantiating an invalid out-of-line static data member definition in a local class (PR #196772)

Younan Zhang via cfe-commits cfe-commits at lists.llvm.org
Sun May 10 08:29:22 PDT 2026


zyn0217 wrote:

@TPPPP72 I took a stab at it and I think we could resolve it in this way:

```diff
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 9f8fc5a187b0..bd7d3130e7e8 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -4897,6 +4897,8 @@ void Sema::MergeVarDecl(VarDecl *New, LookupResult &Previous) {
       if (Def && checkVarDeclRedefinition(Def, New))
         return;
     }
+    if (Old->isInvalidDecl())
+      New->setInvalidDecl();
   } else {
     // C++ may not have a tentative definition rule, but it has a different
     // rule about what constitutes a definition in the first place. See
@@ -7950,7 +7952,7 @@ NamedDecl *Sema::ActOnVariableDeclarator(

     if (CurContext->isRecord()) {
       if (SC == SC_Static) {
        if (const 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;
@@ -7973,7 +7975,6 @@ NamedDecl *Sema::ActOnVariableDeclarator(
                  diag::err_static_data_member_not_allowed_in_local_class)
                 << Name << RD->getDeclName() << RD->getTagKind();
+             Invalid = true;
           } else if (AnonStruct) {
             // C++ [class.static.data]p4: Unnamed classes and classes contained
             // directly or indirectly within unnamed classes shall not contain
```

Can you test it? thanks!

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


More information about the cfe-commits mailing list