[clang] [llvm] [libc] [mlir] [polly] [libcxx] [compiler-rt] [lldb] [flang] [clang-tools-extra] [libunwind] [libcxxabi] [libc++][any] LWG3305: `any_cast<void>` (PR #78215)

Hristo Hristov via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 18 10:10:50 PST 2024


https://github.com/H-G-Hristov updated https://github.com/llvm/llvm-project/pull/78215

>From 328c55879848d98a9bc068436d959b409b239bcc Mon Sep 17 00:00:00 2001
From: Zingam <zingam at outlook.com>
Date: Thu, 11 Jan 2024 09:46:26 +0200
Subject: [PATCH 1/2] [libc++][any] LWG3305: `any_cast<void>`

Implements: https://wg21.link/LWG3305
- https://eel.is/c++draft/any.nonmembers
---
 libcxx/docs/Status/Cxx2cIssues.csv            |  2 +-
 libcxx/include/any                            |  7 ++++
 .../any.nonmembers/any.cast/void.verify.cpp   | 34 +++++++++++++++++++
 3 files changed, 42 insertions(+), 1 deletion(-)
 create mode 100644 libcxx/test/std/utilities/any/any.nonmembers/any.cast/void.verify.cpp

diff --git a/libcxx/docs/Status/Cxx2cIssues.csv b/libcxx/docs/Status/Cxx2cIssues.csv
index fe0f13f6e8cb2c..29f25b63ccb863 100644
--- a/libcxx/docs/Status/Cxx2cIssues.csv
+++ b/libcxx/docs/Status/Cxx2cIssues.csv
@@ -19,7 +19,7 @@
 "","","","","",""
 "`2392 <https://wg21.link/LWG2392>`__","""character type"" is used but not defined","Kona November 2023","","",""
 "`3203 <https://wg21.link/LWG3203>`__","``span`` element access invalidation","Kona November 2023","","",""
-"`3305 <https://wg21.link/LWG3305>`__","``any_cast<void>``","Kona November 2023","","",""
+"`3305 <https://wg21.link/LWG3305>`__","``any_cast<void>``","Kona November 2023","|Complete|","18.0",""
 "`3431 <https://wg21.link/LWG3431>`__","``<=>`` for containers should require ``three_way_comparable<T>`` instead of ``<=>``","Kona November 2023","","",""
 "`3749 <https://wg21.link/LWG3749>`__","``common_iterator`` should handle integer-class difference types","Kona November 2023","","",""
 "`3809 <https://wg21.link/LWG3809>`__","Is ``std::subtract_with_carry_engine<uint16_t>`` supposed to work","Kona November 2023","","",""
diff --git a/libcxx/include/any b/libcxx/include/any
index b9e0a8d94550cc..a157d763c63121 100644
--- a/libcxx/include/any
+++ b/libcxx/include/any
@@ -98,6 +98,7 @@ namespace std {
 #include <__type_traits/is_nothrow_move_constructible.h>
 #include <__type_traits/is_reference.h>
 #include <__type_traits/is_same.h>
+#include <__type_traits/is_void.h>
 #include <__type_traits/remove_cv.h>
 #include <__type_traits/remove_cvref.h>
 #include <__type_traits/remove_reference.h>
@@ -555,6 +556,9 @@ inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST _ValueType
 
 template <class _ValueType>
 inline _LIBCPP_HIDE_FROM_ABI add_pointer_t<add_const_t<_ValueType>> any_cast(any const* __any) _NOEXCEPT {
+#  if _LIBCPP_STD_VER >= 26
+  static_assert(!is_void_v<_ValueType>, "_ValueType may not be void.");
+#  endif
   static_assert(!is_reference<_ValueType>::value, "_ValueType may not be a reference.");
   return std::any_cast<_ValueType>(const_cast<any*>(__any));
 }
@@ -572,6 +576,9 @@ inline _LIBCPP_HIDE_FROM_ABI _RetType __pointer_or_func_cast(void*, /*IsFunction
 template <class _ValueType>
 _LIBCPP_HIDE_FROM_ABI add_pointer_t<_ValueType> any_cast(any* __any) _NOEXCEPT {
   using __any_imp::_Action;
+#  if _LIBCPP_STD_VER >= 26
+  static_assert(!is_void_v<_ValueType>, "_ValueType may not be void.");
+#  endif
   static_assert(!is_reference<_ValueType>::value, "_ValueType may not be a reference.");
   typedef add_pointer_t<_ValueType> _ReturnType;
   if (__any && __any->__h_) {
diff --git a/libcxx/test/std/utilities/any/any.nonmembers/any.cast/void.verify.cpp b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/void.verify.cpp
new file mode 100644
index 00000000000000..c0733e544dcb24
--- /dev/null
+++ b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/void.verify.cpp
@@ -0,0 +1,34 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
+
+// <any>
+
+// template<class T>
+// const T* any_cast(const any* operand) noexcept;
+
+// template<class T>
+// T* any_cast(any* operand) noexcept;
+
+#include <any>
+
+void test() {
+  {
+    const std::any ca = 1;
+
+    // expected-error-re at any:* {{static assertion failed{{.*}}_ValueType may not be void.}}
+    std::any_cast<void>(&ca); // expected-note {{requested here}}
+  }
+  {
+    std::any a = 1;
+
+    // expected-error-re at any:* {{static assertion failed{{.*}}_ValueType may not be void.}}
+    std::any_cast<void>(&a); // expected-note {{requested here}}
+  }
+}

>From 80fc2ca1ce009aa892f148b319129356e2c818c4 Mon Sep 17 00:00:00 2001
From: Zingam <zingam at outlook.com>
Date: Wed, 17 Jan 2024 18:16:04 +0200
Subject: [PATCH 2/2] Addressed comments

---
 libcxx/include/any                                            | 4 ----
 .../std/utilities/any/any.nonmembers/any.cast/void.verify.cpp | 2 +-
 2 files changed, 1 insertion(+), 5 deletions(-)

diff --git a/libcxx/include/any b/libcxx/include/any
index a157d763c63121..378dfb6e21b536 100644
--- a/libcxx/include/any
+++ b/libcxx/include/any
@@ -556,9 +556,7 @@ inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST _ValueType
 
 template <class _ValueType>
 inline _LIBCPP_HIDE_FROM_ABI add_pointer_t<add_const_t<_ValueType>> any_cast(any const* __any) _NOEXCEPT {
-#  if _LIBCPP_STD_VER >= 26
   static_assert(!is_void_v<_ValueType>, "_ValueType may not be void.");
-#  endif
   static_assert(!is_reference<_ValueType>::value, "_ValueType may not be a reference.");
   return std::any_cast<_ValueType>(const_cast<any*>(__any));
 }
@@ -576,9 +574,7 @@ inline _LIBCPP_HIDE_FROM_ABI _RetType __pointer_or_func_cast(void*, /*IsFunction
 template <class _ValueType>
 _LIBCPP_HIDE_FROM_ABI add_pointer_t<_ValueType> any_cast(any* __any) _NOEXCEPT {
   using __any_imp::_Action;
-#  if _LIBCPP_STD_VER >= 26
   static_assert(!is_void_v<_ValueType>, "_ValueType may not be void.");
-#  endif
   static_assert(!is_reference<_ValueType>::value, "_ValueType may not be a reference.");
   typedef add_pointer_t<_ValueType> _ReturnType;
   if (__any && __any->__h_) {
diff --git a/libcxx/test/std/utilities/any/any.nonmembers/any.cast/void.verify.cpp b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/void.verify.cpp
index c0733e544dcb24..9530d63e07b0a1 100644
--- a/libcxx/test/std/utilities/any/any.nonmembers/any.cast/void.verify.cpp
+++ b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/void.verify.cpp
@@ -6,7 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
-// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
+// UNSUPPORTED: c++03, c++11, c++14
 
 // <any>
 



More information about the cfe-commits mailing list