[clang] [clang][sema] Fix crash on class template instantiation when a member variable partial specialization has an invalid primary template (PR #202006)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Jun 5 22:24:15 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Harlen Batagelo (hbatagelo)
<details>
<summary>Changes</summary>
Fixes #<!-- -->195988.
For the root cause, consider this reproducer:
https://godbolt.org/z/6jTnvocs4
```cpp
template <typename> struct A {
template <typename U> static B x; // unknown type name 'B'
template <typename U> static int x<U*>;
};
A<int> a;
```
During the instantiation of `A<int>`, the variable template `x` is skipped because it was marked as an invalid declaration due to the unknown type `B`. When the partial specialization `x<U*>` is visited next, the lookup for `x` finds nothing and triggers the assertion:
https://github.com/llvm/llvm-project/blob/2433b06e6dbe3ef015a226620d207a45f7b98c7c/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp#L2538
Fix by replacing the assertion with a bail-out when the lookup fails. This is consistent with the error recovery as the enclosing class instantiation is already marked invalid when `x` was skipped.
---
Full diff: https://github.com/llvm/llvm-project/pull/202006.diff
3 Files Affected:
- (modified) clang/docs/ReleaseNotes.rst (+1)
- (modified) clang/lib/Sema/SemaTemplateInstantiateDecl.cpp (+2-1)
- (added) clang/test/SemaTemplate/GH195988.cpp (+8)
``````````diff
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;
``````````
</details>
https://github.com/llvm/llvm-project/pull/202006
More information about the cfe-commits
mailing list