[clang] [WIP][clang] fix: allow invalid case for template member specialization in assertion (PR #201506)
Fan Mo via cfe-commits
cfe-commits at lists.llvm.org
Wed Jun 3 21:46:40 PDT 2026
https://github.com/w007878 created https://github.com/llvm/llvm-project/pull/201506
fixes #201490
In the code here https://github.com/llvm/llvm-project/blob/b29352f7ea1d46fa7d90900d7a279851b6de9f74/clang/lib/Sema/SemaTemplate.cpp#L2196-L2206
```c++
...
if (SS.isSet()) {
// If the name of the template was qualified, we must be defining the
// template out-of-line.
if (!SS.isInvalid() && !Invalid && !PrevClassTemplate) {
Diag(NameLoc, TUK == TagUseKind::Friend
? diag::err_friend_decl_does_not_match
: diag::err_member_decl_does_not_match)
<< Name << SemanticContext << /*IsDefinition*/ true << SS.getRange();
Invalid = true;
}
}
...
```
It would be possible to have `PrevClassTemplate == false` when `SS` was invalid.
>From 62c448c34c3cb06a54050228ab822b99f888fc67 Mon Sep 17 00:00:00 2001
From: Fan Mo <w007878 at hotmail.com>
Date: Wed, 3 Jun 2026 23:33:18 -0500
Subject: [PATCH] fix: allow invalid case in assertion
---
clang/lib/Sema/SemaTemplate.cpp | 5 +++--
clang/test/SemaTemplate/GH201490.cpp | 10 ++++++++++
2 files changed, 13 insertions(+), 2 deletions(-)
create mode 100644 clang/test/SemaTemplate/GH201490.cpp
diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index 8c94a1ad39208..23d47249dfd88 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -2247,9 +2247,10 @@ DeclResult Sema::CheckClassTemplate(
NewTemplate->setModulePrivate();
if (IsMemberSpecialization) {
- assert(PrevClassTemplate &&
+ assert((PrevClassTemplate || Invalid) &&
"Member specialization without a primary template?");
- NewTemplate->setMemberSpecialization();
+ if (PrevClassTemplate)
+ NewTemplate->setMemberSpecialization();
}
// Set the access specifier.
diff --git a/clang/test/SemaTemplate/GH201490.cpp b/clang/test/SemaTemplate/GH201490.cpp
new file mode 100644
index 0000000000000..13f05a56f41ab
--- /dev/null
+++ b/clang/test/SemaTemplate/GH201490.cpp
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+// regression test for https://github.com/llvm/llvm-project/issues/201490
+template<class T> struct A {};
+template<class T> struct B : A<T> {};
+template<> template<class T> class A<int>::B {}; // expected-error{{out-of-line definition of 'B' does not match any declaration in 'A<int>'}}
+
+// A legitimate member class template explicit specialization
+template<class T> struct C { template<class U> struct D; };
+ template<> template<class U> struct C<int>::D {};
More information about the cfe-commits
mailing list