[clang] [clang][sema] Fix crash on class template instantiation when a member variable partial specialization has an invalid primary template (PR #202006)
Harlen Batagelo via cfe-commits
cfe-commits at lists.llvm.org
Sun Jun 14 20:29:45 PDT 2026
https://github.com/hbatagelo updated https://github.com/llvm/llvm-project/pull/202006
>From 36cdf84c20828a550e0334bb31834fe86f8f08aa Mon Sep 17 00:00:00 2001
From: Harlen Batagelo <hbatagelo at gmail.com>
Date: Sat, 6 Jun 2026 02:05:54 -0300
Subject: [PATCH] Bail out when the lookup finds nothing
---
clang/docs/ReleaseNotes.rst | 1 +
clang/lib/Sema/SemaTemplateInstantiateDecl.cpp | 3 ++-
clang/test/SemaTemplate/GH195988.cpp | 8 ++++++++
3 files changed, 11 insertions(+), 1 deletion(-)
create mode 100644 clang/test/SemaTemplate/GH195988.cpp
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index cf4826f50e5a5..774a650c2c517 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -731,6 +731,7 @@ Bug Fixes to C++ Support
- Fixed a use-after-free bug when parsing default arguments containing lambdas in declarations with template-id declarators. (#GH196725)
- Fixed a crash in constant evaluation using placement new on an array which was later initialized. (#GH196450)
- Fixed an issue where Clang incorrectly accepted invalid unqualified uses of local nested class names outside their declaring scope. (#GH184622)
+- Fixed a crash when instantiating a class template whose member variable partial specialization has an invalid primary template. (#GH195988)
Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
index 324d6bf3857c7..d6e0b63f71e54 100644
--- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -2535,7 +2535,8 @@ Decl *TemplateDeclInstantiator::VisitVarTemplatePartialSpecializationDecl(
// Lookup the already-instantiated declaration and return that.
DeclContext::lookup_result Found = Owner->lookup(VarTemplate->getDeclName());
- assert(!Found.empty() && "Instantiation found nothing?");
+ if (Found.empty())
+ return nullptr;
VarTemplateDecl *InstVarTemplate = dyn_cast<VarTemplateDecl>(Found.front());
assert(InstVarTemplate && "Instantiation did not find a variable template?");
diff --git a/clang/test/SemaTemplate/GH195988.cpp b/clang/test/SemaTemplate/GH195988.cpp
new file mode 100644
index 0000000000000..1924a8c98c262
--- /dev/null
+++ b/clang/test/SemaTemplate/GH195988.cpp
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+template <typename> struct A {
+ template <typename T> static B x; // expected-error {{unknown type name 'B'}}
+ template <typename T> static int x<T*>;
+};
+
+A<int> a;
More information about the cfe-commits
mailing list