[clang] [Clang] Fix crash when type-name is combined with class specifier in template argument (PR #191689)

via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 20 00:59:19 PDT 2026


https://github.com/TPPPP72 updated https://github.com/llvm/llvm-project/pull/191689

>From e3230a7cc854e21eff03850b1c6cfb823c5a50fa Mon Sep 17 00:00:00 2001
From: TPPPP72 <906483498 at qq.com>
Date: Mon, 20 Apr 2026 15:58:38 +0800
Subject: [PATCH] draft

---
 clang/docs/ReleaseNotes.rst          |  1 +
 clang/lib/Sema/SemaType.cpp          |  6 +++++-
 clang/test/SemaTemplate/gh187664.cpp | 28 ++++++++++++++++++++++++++++
 3 files changed, 34 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/SemaTemplate/gh187664.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 638a813ca105b..42fe63b45128a 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -500,6 +500,7 @@ Bug Fixes to AST Handling
 - Fixed a bug where explicit nullability property attributes were not stored in AST nodes in Objective-C. (#GH179703)
 - Fixed a crash when parsing Doxygen ``@param`` commands attached to invalid declarations or non-function entities. (#GH182737)
 - Fixed the SourceLocation and SourceRange of reversed rewritten CXXOperatorCallExpr. (#GH192467)
+- Fixed a crash when a type-name is incorrectly combined with a class specifier within a template default argument. (#GH187664)
 
 Miscellaneous Bug Fixes
 ^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index 3dc8ceb70f3cd..3f3a6d43c4066 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -6171,7 +6171,11 @@ namespace {
                                          ElaboratedTypeKeyword::None
                                      ? DS.getTypeSpecTypeLoc()
                                      : SourceLocation());
-      TL.setQualifierLoc(DS.getTypeSpecScope().getWithLocInContext(Context));
+                                     
+      if (TL.getTypePtr()->getQualifier() &&
+          DS.getTypeSpecScope().getRange().isValid())
+        TL.setQualifierLoc(DS.getTypeSpecScope().getWithLocInContext(Context));
+
       TL.setNameLoc(DS.getTypeSpecTypeNameLoc());
     }
     void VisitAtomicTypeLoc(AtomicTypeLoc TL) {
diff --git a/clang/test/SemaTemplate/gh187664.cpp b/clang/test/SemaTemplate/gh187664.cpp
new file mode 100644
index 0000000000000..9e9bc0fd166f2
--- /dev/null
+++ b/clang/test/SemaTemplate/gh187664.cpp
@@ -0,0 +1,28 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+struct A {
+  struct B{};
+};
+
+A struct A::B i; // expected-error {{cannot combine with previous 'type-name' declaration specifier}}
+// expected-note at -1 + {{previous definition is here}}
+
+struct S{};
+S struct A::B i;
+// expected-error at -1 {{cannot combine with previous 'type-name' declaration specifier}}
+// expected-error at -2 {{redefinition of 'i' with a different type: 'S' vs 'A'}}
+
+// using T = int;
+// T struct A::B i;
+
+namespace NS {
+  struct Scoped{};
+};
+
+S struct NS::Scoped i;
+// expected-error at -1 {{cannot combine with previous 'type-name' declaration specifier}}
+// expected-error at -2 {{redefinition of 'i' with a different type: 'S' vs 'A'}}
+
+S struct ::S i;
+// expected-error at -1 {{cannot combine with previous 'type-name' declaration specifier}}
+// expected-error at -2 {{redefinition of 'i' with a different type: 'S' vs 'A'}}



More information about the cfe-commits mailing list