https://github.com/hokein created 
https://github.com/llvm/llvm-project/pull/140905

Fixes #140898 

>From d8a728d1e32c2bd317d11a0d8f46e6d66c0bb125 Mon Sep 17 00:00:00 2001
From: Haojian Wu <hokein...@gmail.com>
Date: Wed, 21 May 2025 16:07:54 +0200
Subject: [PATCH] [clang] Fixed an assertion failure triggered when
 instantiating a template with an expression that references an invalid
 declaration.

---
 clang/docs/ReleaseNotes.rst                |  2 ++
 clang/lib/Sema/SemaTemplateInstantiate.cpp |  3 +++
 clang/test/SemaCXX/gh140898.cpp            | 10 ++++++++++
 3 files changed, 15 insertions(+)
 create mode 100644 clang/test/SemaCXX/gh140898.cpp

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>();
+}

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to