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] [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); _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
