Skip to content

[clang] Fixed an assertion failure triggered when instantiating a template with an expr that references an invalid decl #140905

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

hokein
Copy link
Collaborator

@hokein hokein commented May 21, 2025

Fixes #140898

…plate with an

expression that references an invalid declaration.
@hokein hokein requested review from cor3ntin and AaronBallman May 21, 2025 14:12
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels May 21, 2025
@llvmbot
Copy link
Member

llvmbot commented May 21, 2025

@llvm/pr-subscribers-clang

Author: Haojian Wu (hokein)

Changes

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

@@ -4552,6 +4552,9 @@ LocalInstantiationScope::findInstantiationOf(const Decl *D) {
isa<CXXDeductionGuideDecl>(D->getDeclContext()))
return nullptr;

if (D->isInvalidDecl())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't we just check after D = getCanonicalParmVarDecl(D);?

@zyn0217
Copy link
Contributor

zyn0217 commented May 22, 2025

I feel an invalid Decl shouldn't make its way down so deeper into instantiation. Could you check?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Assertion failure "Assertion `isa<LabelDecl>(D) && "declaration not instantiated in this scope"' failed"
4 participants