[clang] [clang] Fixed an assertion failure triggered when instantiating a template with an expr that references an invalid decl (PR #140905)
via cfe-commits
cfe-commits at lists.llvm.org
Wed May 21 07:13:41 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Haojian Wu (hokein)
<details>
<summary>Changes</summary>
Fixes #<!-- -->140898
---
Full diff: https://github.com/llvm/llvm-project/pull/140905.diff
3 Files Affected:
- (modified) clang/docs/ReleaseNotes.rst (+2)
- (modified) clang/lib/Sema/SemaTemplateInstantiate.cpp (+3)
- (added) clang/test/SemaCXX/gh140898.cpp (+10)
``````````diff
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 257c4696de403..0fcaec6690a00 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -579,6 +579,8 @@ Bug Fixes in This Version
``#include`` directive. (#GH138094)
- Fixed a crash during constant evaluation involving invalid lambda captures
(#GH138832)
+- Fixed an assertion failure triggered when instantiating a template with an
+ expression that references an invalid declaration (#GH140898)
Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaTemplateInstantiate.cpp b/clang/lib/Sema/SemaTemplateInstantiate.cpp
index d028eea4f8f3e..e5078547c8cc3 100644
--- a/clang/lib/Sema/SemaTemplateInstantiate.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiate.cpp
@@ -4552,6 +4552,9 @@ LocalInstantiationScope::findInstantiationOf(const Decl *D) {
isa<CXXDeductionGuideDecl>(D->getDeclContext()))
return nullptr;
+ if (D->isInvalidDecl())
+ return nullptr;
+
// If we didn't find the decl, then we either have a sema bug, or we have a
// forward reference to a label declaration. Return null to indicate that
// we have an uninstantiated label.
diff --git a/clang/test/SemaCXX/gh140898.cpp b/clang/test/SemaCXX/gh140898.cpp
new file mode 100644
index 0000000000000..23151a73cf98b
--- /dev/null
+++ b/clang/test/SemaCXX/gh140898.cpp
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify %s
+template <typename T>
+void s() {
+ switch (Unknown tr = 1) // expected-error {{unknown type name 'Unknown'}}
+ tr;
+}
+
+void abc() {
+ s<int>();
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/140905
More information about the cfe-commits
mailing list