https://github.com/hbatagelo created https://github.com/llvm/llvm-project/pull/202006
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. >From 36cdf84c20828a550e0334bb31834fe86f8f08aa Mon Sep 17 00:00:00 2001 From: Harlen Batagelo <[email protected]> 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; _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
