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 &lt;typename&gt; struct A {
  template &lt;typename U&gt; static B x; // unknown type name 'B'
  template &lt;typename U&gt; static int x&lt;U*&gt;;
};
A&lt;int&gt; a;
```
During the instantiation of `A&lt;int&gt;`, 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&lt;U*&gt;` 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
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to