[PATCH] D156064: [SemaCXX] Recognise initializer_list injected-class-name types as initializer_lists

Mital Ashok via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Jul 23 13:59:26 PDT 2023


MitalAshok created this revision.
Herald added a project: All.
MitalAshok added reviewers: EricWF, CornedBee, erichkeane.
MitalAshok published this revision for review.
MitalAshok added a comment.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

The implicitly-generated guide for `template<class E> struct initializer_list;`'s copy constructor's first argument is an injected-class-name. When it was not recognised as an initializer_list, it was erroneously excluded when the initializer was a braced-init-list.

Also falls foul of `-Wctad-maybe-unsupported`. Looks like this is being worked on in https://reviews.llvm.org/D133425 but should `std::initializer_list{ a, b, c }` be a built-in exception to this diagnostic? If not, I would recommend using `_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(initializer_list)` in libc++ from https://reviews.llvm.org/D133535. For comparison, GCC does not warn with `-Wctad-maybe-unsupported`.


This allows the implicitly-generated deduction guide for the copy constructor to be recognised as an initializer-list constructor, allowing CTAD for std::iinitializer_list


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D156064

Files:
  clang/docs/ReleaseNotes.rst
  clang/lib/Sema/SemaDeclCXX.cpp
  clang/test/SemaCXX/cxx1z-class-template-argument-deduction.cpp


Index: clang/test/SemaCXX/cxx1z-class-template-argument-deduction.cpp
===================================================================
--- clang/test/SemaCXX/cxx1z-class-template-argument-deduction.cpp
+++ clang/test/SemaCXX/cxx1z-class-template-argument-deduction.cpp
@@ -12,8 +12,6 @@
     size_t n;
     initializer_list();
   };
-  // FIXME: This should probably not be necessary.
-  template<typename T> initializer_list(initializer_list<T>) -> initializer_list<T>;
 }
 
 template<typename T> constexpr bool has_type(...) { return false; }
Index: clang/lib/Sema/SemaDeclCXX.cpp
===================================================================
--- clang/lib/Sema/SemaDeclCXX.cpp
+++ clang/lib/Sema/SemaDeclCXX.cpp
@@ -11703,11 +11703,17 @@
 
     Template = Specialization->getSpecializedTemplate();
     Arguments = Specialization->getTemplateArgs().data();
-  } else if (const TemplateSpecializationType *TST =
-                 Ty->getAs<TemplateSpecializationType>()) {
-    Template = dyn_cast_or_null<ClassTemplateDecl>(
-        TST->getTemplateName().getAsTemplateDecl());
-    Arguments = TST->template_arguments().begin();
+  } else {
+    const TemplateSpecializationType *TST = nullptr;
+    if (auto *ICN = Ty->getAs<InjectedClassNameType>())
+      TST = ICN->getInjectedTST();
+    else
+      TST = Ty->getAs<TemplateSpecializationType>();
+    if (TST) {
+      Template = dyn_cast_or_null<ClassTemplateDecl>(
+          TST->getTemplateName().getAsTemplateDecl());
+      Arguments = TST->template_arguments().begin();
+    }
   }
   if (!Template)
     return false;
Index: clang/docs/ReleaseNotes.rst
===================================================================
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -789,6 +789,9 @@
   (`#63903 <https://github.com/llvm/llvm-project/issues/63903>`_)
 - Fix constraint checking of non-generic lambdas.
   (`#63181 <https://github.com/llvm/llvm-project/issues/63181>`_)
+- Fix CTAD for ``std::initializer_list``. This allows
+  ``std::initializer_list{1, 2, 3}`` to be a ``std::initializer_list<int>``
+  as intended.
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156064.543323.patch
Type: text/x-patch
Size: 2172 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230723/8622ae59/attachment-0001.bin>


More information about the cfe-commits mailing list