[libcxx-commits] [libcxx] [libc++] remove duplicate assertions for void/reference const any_cast (PR #199425)

Matheus Izvekov via libcxx-commits libcxx-commits at lists.llvm.org
Sun May 24 07:02:05 PDT 2026


https://github.com/mizvekov created https://github.com/llvm/llvm-project/pull/199425

For test cases of the const overload of any_cast, such as:
```C++
void test() {
  std::any a = 0;
  const std::any& a2 = a;
  (void)std::any_cast<int&>(&a2);
}
```
(And similarly for void).

The problem is that the assertions are implemented both in the const and non-const any_cast overloads, but since the const overload delegates to the non-const overload, that ends up producing the same assertion twice.

This separates those test cases, because those assertions are implemented in the function body, and that's only instantiated once per specialization, not once per use.

>From 75f0ec0e6045a8f7216b12e3377734192f977db9 Mon Sep 17 00:00:00 2001
From: Matheus Izvekov <mizvekov at gmail.com>
Date: Sun, 24 May 2026 10:49:36 -0300
Subject: [PATCH] [libc++] remove duplicate assertions for void/reference const
 any_cast

For test cases of the const overload of any_cast, such as:
```C++
void test() {
  std::any a = 0;
  const std::any& a2 = a;
  (void)std::any_cast<int&>(&a2);
}
```
(And similarly for void).

The problem is that the assertions are implemented both in the const and non-const any_cast overloads,
but since the const overload delegates to the non-const overload, that ends up producing the same assertion twice.
---
 libcxx/include/any                            |  2 --
 .../any.cast/const_reference_types.verify.cpp | 33 +++++++++++++++++++
 .../any.cast/reference_types.verify.cpp       | 17 ----------
 .../any.cast/void.const.verify.cpp            | 23 +++++++++++++
 .../any.nonmembers/any.cast/void.verify.cpp   | 17 ++--------
 5 files changed, 59 insertions(+), 33 deletions(-)
 create mode 100644 libcxx/test/std/utilities/any/any.nonmembers/any.cast/const_reference_types.verify.cpp
 create mode 100644 libcxx/test/std/utilities/any/any.nonmembers/any.cast/void.const.verify.cpp

diff --git a/libcxx/include/any b/libcxx/include/any
index 740bcec93835c..4addb3c2b8835 100644
--- a/libcxx/include/any
+++ b/libcxx/include/any
@@ -526,8 +526,6 @@ template <class _ValueType>
 
 template <class _ValueType>
 [[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI add_pointer_t<add_const_t<_ValueType>> any_cast(any const* __any) noexcept {
-  static_assert(!is_void_v<_ValueType>, "_ValueType may not be void.");
-  static_assert(!is_reference_v<_ValueType>, "_ValueType may not be a reference.");
   return std::any_cast<_ValueType>(const_cast<any*>(__any));
 }
 
diff --git a/libcxx/test/std/utilities/any/any.nonmembers/any.cast/const_reference_types.verify.cpp b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/const_reference_types.verify.cpp
new file mode 100644
index 0000000000000..6ef4ccfd365ed
--- /dev/null
+++ b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/const_reference_types.verify.cpp
@@ -0,0 +1,33 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+// <any>
+
+// template <class ValueType>
+// ValueType const* any_cast(any const *) noexcept;
+
+#include <any>
+
+void test() {
+  std::any a1       = 1;
+  const std::any& a = a1;
+
+  // expected-error-re at any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
+  (void)std::any_cast<int&>(&a); // expected-note {{requested here}}
+
+  // expected-error-re at any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
+  (void)std::any_cast<int&&>(&a); // expected-note {{requested here}}
+
+  // expected-error-re at any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
+  (void)std::any_cast<int const&>(&a); // expected-note {{requested here}}
+
+  // expected-error-re at any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
+  (void)std::any_cast<int const&&>(&a); // expected-note {{requested here}}
+}
diff --git a/libcxx/test/std/utilities/any/any.nonmembers/any.cast/reference_types.verify.cpp b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/reference_types.verify.cpp
index d634f73291e0e..e034a749569ca 100644
--- a/libcxx/test/std/utilities/any/any.nonmembers/any.cast/reference_types.verify.cpp
+++ b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/reference_types.verify.cpp
@@ -10,9 +10,6 @@
 
 // <any>
 
-// template <class ValueType>
-// ValueType const* any_cast(any const *) noexcept;
-//
 // template <class ValueType>
 // ValueType * any_cast(any *) noexcept;
 
@@ -32,18 +29,4 @@ void test() {
 
   // expected-error-re at any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
   (void)std::any_cast<int const&&>(&a); // expected-note {{requested here}}
-
-  const std::any& a2 = a;
-
-  // expected-error-re at any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
-  (void)std::any_cast<int&>(&a2); // expected-note {{requested here}}
-
-  // expected-error-re at any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
-  (void)std::any_cast<int&&>(&a2); // expected-note {{requested here}}
-
-  // expected-error-re at any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
-  (void)std::any_cast<int const&>(&a2); // expected-note {{requested here}}
-
-  // expected-error-re at any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
-  (void)std::any_cast<int const&&>(&a2); // expected-note {{requested here}}
 }
diff --git a/libcxx/test/std/utilities/any/any.nonmembers/any.cast/void.const.verify.cpp b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/void.const.verify.cpp
new file mode 100644
index 0000000000000..1c0bb9df5cda3
--- /dev/null
+++ b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/void.const.verify.cpp
@@ -0,0 +1,23 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+// <any>
+
+// template<class T>
+// const T* any_cast(const 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.}}
+  (void)std::any_cast<void>(&ca); // expected-note {{requested here}}
+}
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 d1ec42eaf10c2..c3d1530871d50 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
@@ -10,25 +10,14 @@
 
 // <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.}}
-    (void)std::any_cast<void>(&ca); // expected-note {{requested here}}
-  }
-  {
-    std::any a = 1;
+  std::any a = 1;
 
-    // expected-error-re at any:* {{static assertion failed{{.*}}_ValueType may not be void.}}
-    (void)std::any_cast<void>(&a); // expected-note {{requested here}}
-  }
+  // expected-error-re at any:* {{static assertion failed{{.*}}_ValueType may not be void.}}
+  (void)std::any_cast<void>(&a); // expected-note {{requested here}}
 }



More information about the libcxx-commits mailing list