[clang] [clang-tools-extra] [clang] Implement CWG2413 (implicit `typename` in conversion operators) (PR #195207)
Victor Chernyakin via cfe-commits
cfe-commits at lists.llvm.org
Fri May 1 09:53:10 PDT 2026
https://github.com/localspook updated https://github.com/llvm/llvm-project/pull/195207
>From 18d80624cb81994ecc8e3091e80a85aa559c7be0 Mon Sep 17 00:00:00 2001
From: Victor Chernyakin <chernyakin.victor.j at outlook.com>
Date: Thu, 30 Apr 2026 18:47:05 -0700
Subject: [PATCH 1/3] [clang] Implement CWG2413 (implicit `typename` in
conversion-function-ids)
---
.../clang-tidy/readability/RedundantTypenameCheck.cpp | 3 ++-
clang/docs/ReleaseNotes.rst | 3 +++
clang/include/clang/Parse/Parser.h | 2 +-
clang/lib/Parse/ParseDecl.cpp | 5 +----
clang/test/CXX/drs/cwg24xx.cpp | 10 ++++++++++
clang/test/CXX/temp/temp.res/p4.cpp | 4 ++--
clang/www/cxx_dr_status.html | 2 +-
7 files changed, 20 insertions(+), 9 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/readability/RedundantTypenameCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantTypenameCheck.cpp
index 77ef2b8622c93..33359fdc3f4bc 100644
--- a/clang-tools-extra/clang-tidy/readability/RedundantTypenameCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/RedundantTypenameCheck.cpp
@@ -34,7 +34,8 @@ void RedundantTypenameCheck::registerMatchers(MatchFinder *Finder) {
cxxMethodDecl(), hasParent(friendDecl()),
functionDecl(has(nestedNameSpecifier())),
cxxDeductionGuideDecl(hasDeclContext(recordDecl())))))))),
- // Match return types.
+ // Match return types. FIXME: CWG2413 made conversion operators
+ // an implicit typename context.
functionDecl(unless(cxxConversionDecl()))))),
hasParent(expr(anyOf(cxxNamedCastExpr(), cxxNewExpr()))));
Finder->addMatcher(
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index c5c8c1fa12e7a..44750a1485a49 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -155,6 +155,9 @@ C++17 Feature Support
Resolutions to C++ Defect Reports
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+- Clang now allows omitting ``typename`` before a template name in a
+ conversion operator, implementing `CWG2413 <https://wg21.link/cwg2413>`_.
+
C Language Changes
------------------
diff --git a/clang/include/clang/Parse/Parser.h b/clang/include/clang/Parse/Parser.h
index 1d07d8dbcfa01..7c89a88dd37ff 100644
--- a/clang/include/clang/Parse/Parser.h
+++ b/clang/include/clang/Parse/Parser.h
@@ -1685,13 +1685,13 @@ class Parser : public CodeCompletionHandler {
case DeclSpecContext::DSC_alias_declaration:
case DeclSpecContext::DSC_template_param:
case DeclSpecContext::DSC_new:
+ case DeclSpecContext::DSC_conv_operator:
return ImplicitTypenameContext::Yes;
case DeclSpecContext::DSC_normal:
case DeclSpecContext::DSC_objc_method_result:
case DeclSpecContext::DSC_condition:
case DeclSpecContext::DSC_template_arg:
- case DeclSpecContext::DSC_conv_operator:
case DeclSpecContext::DSC_association:
return ImplicitTypenameContext::No;
}
diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp
index 4f37e1471c29e..03b68cc88369b 100644
--- a/clang/lib/Parse/ParseDecl.cpp
+++ b/clang/lib/Parse/ParseDecl.cpp
@@ -3364,11 +3364,8 @@ void Parser::ParseDeclarationSpecifiers(
// If we are in a operator context, convert it back into a type specifier
// context for better error handling later on.
- if (DSContext == DeclSpecContext::DSC_conv_operator) {
- // No implicit typename here.
- AllowImplicitTypename = ImplicitTypenameContext::No;
+ if (DSContext == DeclSpecContext::DSC_conv_operator)
DSContext = DeclSpecContext::DSC_type_specifier;
- }
bool EnteringContext = (DSContext == DeclSpecContext::DSC_class ||
DSContext == DeclSpecContext::DSC_top_level);
diff --git a/clang/test/CXX/drs/cwg24xx.cpp b/clang/test/CXX/drs/cwg24xx.cpp
index 0a6a05c125451..2acdf42da7275 100644
--- a/clang/test/CXX/drs/cwg24xx.cpp
+++ b/clang/test/CXX/drs/cwg24xx.cpp
@@ -8,6 +8,16 @@
// cwg2406 is in cwg2406.cpp
+namespace cwg2413 { // cwg2413: 23
+#if __cplusplus >= 202002L
+template <typename T>
+struct S {
+ operator T::R();
+ void f() { operator T::R(); }
+};
+#endif
+} // namespace cwg2413
+
namespace cwg2428 { // cwg2428: 19
#if __cplusplus >= 202002L
template <typename>
diff --git a/clang/test/CXX/temp/temp.res/p4.cpp b/clang/test/CXX/temp/temp.res/p4.cpp
index 9dbdd235e925d..6ca609b58cf2c 100644
--- a/clang/test/CXX/temp/temp.res/p4.cpp
+++ b/clang/test/CXX/temp/temp.res/p4.cpp
@@ -157,8 +157,8 @@ template int Test<X>;
template<typename T> struct A {
enum E : T::type {}; // expected-error{{missing 'typename'}}
- operator T::type() {} // expected-error{{missing 'typename'}}
- void f() { this->operator T::type(); } // expected-error{{missing 'typename'}}
+ operator T::type() {}
+ void f() { this->operator T::type(); }
};
template<typename T>
diff --git a/clang/www/cxx_dr_status.html b/clang/www/cxx_dr_status.html
index 2a1e79471ceea..ffb0494cd884d 100755
--- a/clang/www/cxx_dr_status.html
+++ b/clang/www/cxx_dr_status.html
@@ -16684,7 +16684,7 @@ <h2 id="cxxdr">C++ defect report implementation status</h2>
<td>[<a href="https://wg21.link/temp.res">temp.res</a>]</td>
<td>CD6</td>
<td><TT>typename</TT> in <I>conversion-function-id</I>s</td>
- <td class="unknown" align="center">Unknown</td>
+ <td class="full" align="center">Clang 23</td>
</tr>
<tr id="2414">
<td><a href="https://cplusplus.github.io/CWG/issues/2414.html">2414</a></td>
>From f93cbe05497376d70a6f17a28056f5d149c0664a Mon Sep 17 00:00:00 2001
From: Victor Chernyakin <chernyakin.victor.j at outlook.com>
Date: Thu, 30 Apr 2026 21:03:52 -0700
Subject: [PATCH 2/3] full -> unreleased
Co-authored-by: Yanzuo Liu <zwuis at outlook.com>
---
clang/www/cxx_dr_status.html | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/www/cxx_dr_status.html b/clang/www/cxx_dr_status.html
index ffb0494cd884d..be01eaae09774 100755
--- a/clang/www/cxx_dr_status.html
+++ b/clang/www/cxx_dr_status.html
@@ -16684,7 +16684,7 @@ <h2 id="cxxdr">C++ defect report implementation status</h2>
<td>[<a href="https://wg21.link/temp.res">temp.res</a>]</td>
<td>CD6</td>
<td><TT>typename</TT> in <I>conversion-function-id</I>s</td>
- <td class="full" align="center">Clang 23</td>
+ <td class="unreleased" align="center">Clang 23</td>
</tr>
<tr id="2414">
<td><a href="https://cplusplus.github.io/CWG/issues/2414.html">2414</a></td>
>From 7a683f14bb958e5b8596a643ed1a297cc4b742d6 Mon Sep 17 00:00:00 2001
From: Victor Chernyakin <chernyakin.victor.j at outlook.com>
Date: Fri, 1 May 2026 09:52:20 -0700
Subject: [PATCH 3/3] Add tests for errors pre-C++20
---
clang/test/CXX/drs/cwg24xx.cpp | 18 +++++++++---------
1 file changed, 9 insertions(+), 9 deletions(-)
diff --git a/clang/test/CXX/drs/cwg24xx.cpp b/clang/test/CXX/drs/cwg24xx.cpp
index 2acdf42da7275..5d185352f1619 100644
--- a/clang/test/CXX/drs/cwg24xx.cpp
+++ b/clang/test/CXX/drs/cwg24xx.cpp
@@ -1,21 +1,21 @@
-// RUN: %clang_cc1 -std=c++98 -fexceptions -fcxx-exceptions -pedantic-errors %s -verify-directives -verify=expected,cxx98-14
-// RUN: %clang_cc1 -std=c++11 -fexceptions -fcxx-exceptions -pedantic-errors %s -verify-directives -verify=expected,cxx98-14
-// RUN: %clang_cc1 -std=c++14 -fexceptions -fcxx-exceptions -pedantic-errors %s -verify-directives -verify=expected,cxx98-14
-// RUN: %clang_cc1 -std=c++17 -fexceptions -fcxx-exceptions -pedantic-errors %s -verify-directives -verify=expected,since-cxx17
-// RUN: %clang_cc1 -std=c++20 -fexceptions -fcxx-exceptions -pedantic-errors %s -verify-directives -verify=expected,since-cxx20,since-cxx17
-// RUN: %clang_cc1 -std=c++23 -fexceptions -fcxx-exceptions -pedantic-errors %s -verify-directives -verify=expected,since-cxx20,since-cxx17
-// RUN: %clang_cc1 -std=c++2c -fexceptions -fcxx-exceptions -pedantic-errors %s -verify-directives -verify=expected,since-cxx20,since-cxx17
+// RUN: %clang_cc1 -std=c++98 -pedantic-errors %s -verify=expected,cxx98-14,cxx98-17
+// RUN: %clang_cc1 -std=c++11 -pedantic-errors %s -verify=expected,cxx98-14,cxx98-17
+// RUN: %clang_cc1 -std=c++14 -pedantic-errors %s -verify=expected,cxx98-14,cxx98-17
+// RUN: %clang_cc1 -std=c++17 -pedantic-errors %s -verify=expected,since-cxx17,cxx98-17
+// RUN: %clang_cc1 -std=c++20 -pedantic-errors %s -verify=expected,since-cxx20,since-cxx17
+// RUN: %clang_cc1 -std=c++23 -pedantic-errors %s -verify=expected,since-cxx20,since-cxx17
+// RUN: %clang_cc1 -std=c++2c -pedantic-errors %s -verify=expected,since-cxx20,since-cxx17
// cwg2406 is in cwg2406.cpp
namespace cwg2413 { // cwg2413: 23
-#if __cplusplus >= 202002L
template <typename T>
struct S {
operator T::R();
+ // cxx98-17-error at -1 {{missing 'typename' prior to dependent type name 'T::R' is a C++20 extension}}
void f() { operator T::R(); }
+ // cxx98-17-error at -1 {{missing 'typename' prior to dependent type name 'T::R' is a C++20 extension}}
};
-#endif
} // namespace cwg2413
namespace cwg2428 { // cwg2428: 19
More information about the cfe-commits
mailing list