[PATCH] D133609: [Sema] compat warning of using deduced type in non-type template parameter

YingChi Long via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 9 13:04:13 PDT 2022


inclyc created this revision.
Herald added a subscriber: kristof.beyls.
Herald added a project: All.
inclyc added reviewers: aaron.ballman, mizvekov, clang-language-wg.
inclyc published this revision for review.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Seems we are missing C++20 compatibility warning of using deduced type
(e.g. some template class) as a non-type template parameter. Before this
patch the following code crashes clang.

  template<class>
  struct DC {};
  
  template<DC s> // using deduced type, but considered as "auto" type
  auto test() {}

It triggered warn_cxx14_compat_template_nontype_parm_auto_type. However
there is no such "auto" type at all.

Using deduced type within non-type template parameter was introduced in
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0732r2.pdf.

This patch create a new compatibility diagnostic message for this.

Fixes: https://github.com/llvm/llvm-project/issues/57643


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D133609

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/Sema/SemaTemplate.cpp
  clang/test/SemaCXX/template-nontype-args-compat.cpp


Index: clang/test/SemaCXX/template-nontype-args-compat.cpp
===================================================================
--- /dev/null
+++ clang/test/SemaCXX/template-nontype-args-compat.cpp
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -fsyntax-only -Wpre-c++20-compat -std=c++20 -verify=cpp20 %s 
+
+namespace DeducedClass {
+template<class>
+struct DC {};
+
+template<DC s> // cpp20-warning{{non-type template parameters declared with 'DC' are incompatible with C++ standards before C++20}}
+auto test() {}
+} // namespace DeducedClass
Index: clang/lib/Sema/SemaTemplate.cpp
===================================================================
--- clang/lib/Sema/SemaTemplate.cpp
+++ clang/lib/Sema/SemaTemplate.cpp
@@ -1532,9 +1532,17 @@
   CheckValidDeclSpecifiers();
 
   if (TInfo->getType()->isUndeducedType()) {
-    Diag(D.getIdentifierLoc(),
-         diag::warn_cxx14_compat_template_nontype_parm_auto_type)
-      << QualType(TInfo->getType()->getContainedAutoType(), 0);
+    if (TInfo->getType()->getContainedAutoType()) {
+      // template<auto xx>
+      Diag(D.getIdentifierLoc(),
+           diag::warn_cxx14_compat_template_nontype_parm_auto_type)
+          << QualType(TInfo->getType()->getContainedAutoType(), 0);
+    } else {
+      // template<DeducedClass dc>
+      Diag(D.getIdentifierLoc(),
+           diag::warn_cxx17_compat_template_nontype_parm_deduced_class)
+          << TInfo->getType();
+    }
   }
 
   assert(S->isTemplateParamScope() &&
Index: clang/include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -4833,6 +4833,10 @@
   "non-type template parameter of type %0 is incompatible with "
   "C++ standards before C++20">,
   DefaultIgnore, InGroup<CXXPre20Compat>;
+def warn_cxx17_compat_template_nontype_parm_deduced_class : Warning<
+  "non-type template parameters declared with %0 are incompatible with C++ "
+  "standards before C++20">,
+  DefaultIgnore, InGroup<CXXPre20Compat>;
 def warn_cxx14_compat_template_nontype_parm_auto_type : Warning<
   "non-type template parameters declared with %0 are incompatible with C++ "
   "standards before C++17">,
Index: clang/docs/ReleaseNotes.rst
===================================================================
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -95,6 +95,9 @@
   `Issue 57169 <https://github.com/llvm/llvm-project/issues/57169>`_
 - Clang configuration files are now read through the virtual file system
   rather than the physical one, if these are different.
+- Fix `Issue 57643 <https://github.com/llvm/llvm-project/issues/57643>`_.
+  Missing C++20 compatibility warning of deduced type as non-type template
+  parameters.
 
 
 Improvements to Clang's diagnostics


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D133609.459176.patch
Type: text/x-patch
Size: 2859 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220909/48079aff/attachment.bin>


More information about the cfe-commits mailing list