[clang] Avoid assert in substqualifier (PR #182707)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Feb 21 14:25:46 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Ayush Kumar Gaur (Ayush3941)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/182707.diff
2 Files Affected:
- (modified) clang/lib/Sema/SemaTemplateInstantiateDecl.cpp (+4-3)
- (added) clang/test/SemaTemplate/GH176152.cpp (+12)
``````````diff
diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
index e74c41517ecbf..77c4d732febac 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);
``````````
</details>
https://github.com/llvm/llvm-project/pull/182707
More information about the cfe-commits
mailing list