[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
Mon May 25 04:39:59 PDT 2026
https://github.com/mizvekov updated https://github.com/llvm/llvm-project/pull/199425
>From 9901e933189b0ba0bb7e32f99706ea60acd3cf88 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..cf8a7f4e77a52
--- /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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++17
+
+// <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..3dfc3ee2f4991
--- /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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++17
+
+// <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