[clang] Avoid assert in substqualifier (PR #182707)

Ayush Kumar Gaur via cfe-commits cfe-commits at lists.llvm.org
Sat Feb 21 14:25:17 PST 2026


https://github.com/Ayush3941 created https://github.com/llvm/llvm-project/pull/182707

Replace an assertion in SubstQualifier with a guarded early return when
encountering non-friend qualified declarations inside dependent contexts.

Invalid code during template instantiation may reach this path during
error recovery  causing an ICE  Instead of asserting treat this as a
substitution failure and abort qualifier substitution gracefully

Fixes #176152.

>From f1b8a99b13886b8e3e90fd8916461cde3c91d573 Mon Sep 17 00:00:00 2001
From: Ayush3941 <ayushkgaur1 at gmail.com>
Date: Sat, 21 Feb 2026 17:19:50 -0500
Subject: [PATCH 1/2] [Clang][Sema] Avoid assertion in SubstQualifier for
 non-friend qualified decls in dependent contexts

---
 clang/lib/Sema/SemaTemplateInstantiateDecl.cpp |  7 ++++---
 clang/test/SemaTemplate/GH176152.cpp           | 12 ++++++++++++
 2 files changed, 16 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/SemaTemplate/GH176152.cpp

diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
index e74c41517ecbf..d226c0bc01779 100644
--- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -55,9 +55,10 @@ static bool SubstQualifier(Sema &SemaRef, const DeclT *OldDecl, DeclT *NewDecl,
   if (!OldDecl->getQualifierLoc())
     return false;
 
-  assert((NewDecl->getFriendObjectKind() ||
-          !OldDecl->getLexicalDeclContext()->isDependentContext()) &&
-         "non-friend with qualified name defined in dependent context");
+  if (!NewDecl->getFriendObjectKind() &&
+    OldDecl->getLexicalDeclContext()->isDependentContext()) {
+    return true;
+  }
   Sema::ContextRAII SavedContext(
       SemaRef,
       const_cast<DeclContext *>(NewDecl->getFriendObjectKind()
diff --git a/clang/test/SemaTemplate/GH176152.cpp b/clang/test/SemaTemplate/GH176152.cpp
new file mode 100644
index 0000000000000..7d61aa292982d
--- /dev/null
+++ b/clang/test/SemaTemplate/GH176152.cpp
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -std=c++23 -fsyntax-only -verify %s
+
+template <class T> int f(T) {
+  struct MyClass {
+    static int staticField;
+    // expected-error at -1 {{static data member 'staticField' not allowed in local struct 'MyClass'}}
+  };
+  int MyClass::staticField = 42;
+  return 0;
+}
+
+int x = f(0);

>From 92124a001c319d0b22310b9b7bd3735d79b2ae9c Mon Sep 17 00:00:00 2001
From: Ayush3941 <ayushkgaur1 at gmail.com>
Date: Sat, 21 Feb 2026 17:23:35 -0500
Subject: [PATCH 2/2] [Clang][Sema] Avoid assertion in SubstQualifier for
 non-friend qualified decls in dependent contexts fixed format

---
 clang/lib/Sema/SemaTemplateInstantiateDecl.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
index d226c0bc01779..77c4d732febac 100644
--- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -56,7 +56,7 @@ static bool SubstQualifier(Sema &SemaRef, const DeclT *OldDecl, DeclT *NewDecl,
     return false;
 
   if (!NewDecl->getFriendObjectKind() &&
-    OldDecl->getLexicalDeclContext()->isDependentContext()) {
+      OldDecl->getLexicalDeclContext()->isDependentContext()) {
     return true;
   }
   Sema::ContextRAII SavedContext(



More information about the cfe-commits mailing list