[clang] [Clang] Don't start a definition for an invalid member specialization after instantiation (PR #241659)

via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 11 20:23:56 PST 2026


https://github.com/Himadhith created https://github.com/llvm/llvm-project/pull/241659

This patch fixes a crash on invalid code when a member class of a class template is explicitly specialized after it was implicitly instantiated.

In `Sema::ActOnTag`, `CheckMemberSpecialization` reports "explicit specialization of 'Y' after instantiation" and the new declaration is marked invalid. We still called `New->startDefinition()` on it, even though `PrevDecl` already had a definition. The invalid declaration then became the definition that lookup and layout used, and `getASTRecordLayout()` hit the `!D->isInvalidDecl() && "Cannot get layout of invalid decl!"` assertion.

In this version we record when `CheckMemberSpecialization` fails and skip `startDefinition()`. 

>From 044b21e5e7d220166de85ee2331106f82de862fe Mon Sep 17 00:00:00 2001
From: himadhith <himadhith.v at ibm.com>
Date: Wed, 11 Nov 2026 23:18:23 -0500
Subject: [PATCH] [Clang] Don't start a definition for an invalid member
 specialization after instantiation

---
 clang/lib/Sema/SemaDecl.cpp                   |  9 ++++--
 ...ialization-after-instantiation-invalid.cpp | 31 +++++++++++++++++++
 2 files changed, 38 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/SemaTemplate/explicit-specialization-after-instantiation-invalid.cpp

diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index d4fbf04ee284af..625b70fe270961 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -19198,8 +19198,9 @@ Sema::ActOnTag(Scope *S, unsigned TagSpec, TagUseKind TUK, SourceLocation KWLoc,
 
   // If this is a specialization of a member class (of a class template),
   // check the specialization.
+  bool InvalidMemberSpecialization = false;
   if (isMemberSpecialization && CheckMemberSpecialization(New, Previous))
-    Invalid = true;
+    Invalid = InvalidMemberSpecialization = true;
 
   // If we're declaring or defining a tag in function prototype scope in C,
   // note that this type can only be used within the function and add it to
@@ -19256,7 +19257,11 @@ Sema::ActOnTag(Scope *S, unsigned TagSpec, TagUseKind TUK, SourceLocation KWLoc,
 
   if (TUK == TagUseKind::Definition) {
     if (!SkipBody || !SkipBody->ShouldSkip) {
-      New->startDefinition();
+      // An invalid member specialization of a class that already has a
+      // definition must not start a second one.
+      if (!InvalidMemberSpecialization || !PrevDecl ||
+          !PrevDecl->getDefinition())
+        New->startDefinition();
     } else {
       New->setCompleteDefinition();
       New->demoteThisDefinitionToDeclaration();
diff --git a/clang/test/SemaTemplate/explicit-specialization-after-instantiation-invalid.cpp b/clang/test/SemaTemplate/explicit-specialization-after-instantiation-invalid.cpp
new file mode 100644
index 00000000000000..21d2175175b390
--- /dev/null
+++ b/clang/test/SemaTemplate/explicit-specialization-after-instantiation-invalid.cpp
@@ -0,0 +1,31 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++17 %s
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++20 %s
+
+// Used to hit "Cannot get layout of invalid decl!" because the invalid
+// specialization started a second definition. See GH207953.
+
+template <typename T>
+struct X {
+  struct Y {
+    Y() : v(0) {}
+    int v;
+    int getValue();
+  } y; // expected-note {{implicit instantiation first required here}}
+};
+
+template <typename T>
+int X<T>::Y::getValue() {
+  return ++v;
+}
+
+template <> struct X<int>::Y { int getValue() { return 55; } };
+// expected-error at -1 {{explicit specialization of 'Y' after instantiation}}
+
+extern template class X<int>::Y;
+
+int main() {
+  X<int> x;
+  return x.y.getValue();
+}



More information about the cfe-commits mailing list