From 55322cc78a251e3b9e0bb2452813d98f8cb73ebb Mon Sep 17 00:00:00 2001 From: Hristo Hristov Date: Fri, 21 Nov 2025 18:09:46 +0200 Subject: [PATCH] Reformat and refactor any_cast tests ...in preparation for https://github.com/llvm/llvm-project/pull/168826 as requested in the review. --- .../any.cast/any_cast_pointer.pass.cpp | 237 ++++----- .../any.cast/any_cast_reference.pass.cpp | 483 +++++++++--------- ..._request_invalid_value_category.verify.cpp | 62 +-- .../any.cast/const_correctness.verify.cpp | 20 +- .../not_copy_constructible.verify.cpp | 30 +- .../any.cast/reference_types.verify.cpp | 41 +- 6 files changed, 428 insertions(+), 445 deletions(-) diff --git a/libcxx/test/std/utilities/any/any.nonmembers/any.cast/any_cast_pointer.pass.cpp b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/any_cast_pointer.pass.cpp index 5143befffce73..a5f0e8b80ebec 100644 --- a/libcxx/test/std/utilities/any/any.nonmembers/any.cast/any_cast_pointer.pass.cpp +++ b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/any_cast_pointer.pass.cpp @@ -25,156 +25,157 @@ // Test that the operators are properly noexcept. void test_cast_is_noexcept() { - std::any a; - ASSERT_NOEXCEPT(std::any_cast(&a)); + std::any a; + ASSERT_NOEXCEPT(std::any_cast(&a)); - const std::any& ca = a; - ASSERT_NOEXCEPT(std::any_cast(&ca)); + const std::any& ca = a; + ASSERT_NOEXCEPT(std::any_cast(&ca)); } // Test that the return type of any_cast is correct. void test_cast_return_type() { - std::any a; - ASSERT_SAME_TYPE(decltype(std::any_cast(&a)), int*); - ASSERT_SAME_TYPE(decltype(std::any_cast(&a)), int const*); + std::any a; + ASSERT_SAME_TYPE(decltype(std::any_cast(&a)), int*); + ASSERT_SAME_TYPE(decltype(std::any_cast(&a)), int const*); - const std::any& ca = a; - ASSERT_SAME_TYPE(decltype(std::any_cast(&ca)), int const*); - ASSERT_SAME_TYPE(decltype(std::any_cast(&ca)), int const*); + const std::any& ca = a; + ASSERT_SAME_TYPE(decltype(std::any_cast(&ca)), int const*); + ASSERT_SAME_TYPE(decltype(std::any_cast(&ca)), int const*); } // Test that any_cast handles null pointers. void test_cast_nullptr() { - std::any *a = nullptr; - assert(nullptr == std::any_cast(a)); - assert(nullptr == std::any_cast(a)); + std::any* a = nullptr; + assert(nullptr == std::any_cast(a)); + assert(nullptr == std::any_cast(a)); - const std::any *ca = nullptr; - assert(nullptr == std::any_cast(ca)); - assert(nullptr == std::any_cast(ca)); + const std::any* ca = nullptr; + assert(nullptr == std::any_cast(ca)); + assert(nullptr == std::any_cast(ca)); } // Test casting an empty object. void test_cast_empty() { - { - std::any a; - assert(nullptr == std::any_cast(&a)); - assert(nullptr == std::any_cast(&a)); - - const std::any& ca = a; - assert(nullptr == std::any_cast(&ca)); - assert(nullptr == std::any_cast(&ca)); - } - // Create as non-empty, then make empty and run test. - { - std::any a(42); - a.reset(); - assert(nullptr == std::any_cast(&a)); - assert(nullptr == std::any_cast(&a)); - - const std::any& ca = a; - assert(nullptr == std::any_cast(&ca)); - assert(nullptr == std::any_cast(&ca)); - } + { + std::any a; + assert(nullptr == std::any_cast(&a)); + assert(nullptr == std::any_cast(&a)); + + const std::any& ca = a; + assert(nullptr == std::any_cast(&ca)); + assert(nullptr == std::any_cast(&ca)); + } + // Create as non-empty, then make empty and run test. + { + std::any a(42); + a.reset(); + assert(nullptr == std::any_cast(&a)); + assert(nullptr == std::any_cast(&a)); + + const std::any& ca = a; + assert(nullptr == std::any_cast(&ca)); + assert(nullptr == std::any_cast(&ca)); + } } template void test_cast() { - assert(Type::count == 0); - Type::reset(); - { - std::any a = Type(42); - const std::any& ca = a; - assert(Type::count == 1); - assert(Type::copied == 0); - assert(Type::moved == 1); - - // Try a cast to a bad type. - // NOTE: Type cannot be an int. - assert(std::any_cast(&a) == nullptr); - assert(std::any_cast(&a) == nullptr); - assert(std::any_cast(&a) == nullptr); - - // Try a cast to the right type, but as a pointer. - assert(std::any_cast(&a) == nullptr); - assert(std::any_cast(&a) == nullptr); - - // Check getting a unqualified type from a non-const any. - Type* v = std::any_cast(&a); - assert(v != nullptr); - assert(v->value == 42); - - // change the stored value and later check for the new value. - v->value = 999; - - // Check getting a const qualified type from a non-const any. - Type const* cv = std::any_cast(&a); - assert(cv != nullptr); - assert(cv == v); - assert(cv->value == 999); - - // Check getting a unqualified type from a const any. - cv = std::any_cast(&ca); - assert(cv != nullptr); - assert(cv == v); - assert(cv->value == 999); - - // Check getting a const-qualified type from a const any. - cv = std::any_cast(&ca); - assert(cv != nullptr); - assert(cv == v); - assert(cv->value == 999); - - // Check that no more objects were created, copied or moved. - assert(Type::count == 1); - assert(Type::copied == 0); - assert(Type::moved == 1); - } - assert(Type::count == 0); + assert(Type::count == 0); + Type::reset(); + { + std::any a = Type(42); + const std::any& ca = a; + assert(Type::count == 1); + assert(Type::copied == 0); + assert(Type::moved == 1); + + // Try a cast to a bad type. + // NOTE: Type cannot be an int. + assert(std::any_cast(&a) == nullptr); + assert(std::any_cast(&a) == nullptr); + assert(std::any_cast(&a) == nullptr); + + // Try a cast to the right type, but as a pointer. + assert(std::any_cast(&a) == nullptr); + assert(std::any_cast(&a) == nullptr); + + // Check getting a unqualified type from a non-const any. + Type* v = std::any_cast(&a); + assert(v != nullptr); + assert(v->value == 42); + + // change the stored value and later check for the new value. + v->value = 999; + + // Check getting a const qualified type from a non-const any. + Type const* cv = std::any_cast(&a); + assert(cv != nullptr); + assert(cv == v); + assert(cv->value == 999); + + // Check getting a unqualified type from a const any. + cv = std::any_cast(&ca); + assert(cv != nullptr); + assert(cv == v); + assert(cv->value == 999); + + // Check getting a const-qualified type from a const any. + cv = std::any_cast(&ca); + assert(cv != nullptr); + assert(cv == v); + assert(cv->value == 999); + + // Check that no more objects were created, copied or moved. + assert(Type::count == 1); + assert(Type::copied == 0); + assert(Type::moved == 1); + } + assert(Type::count == 0); } -void test_cast_non_copyable_type() -{ - // Even though 'any' never stores non-copyable types - // we still need to support any_cast(ptr) - struct NoCopy { NoCopy(NoCopy const&) = delete; }; - std::any a(42); - std::any const& ca = a; - assert(std::any_cast(&a) == nullptr); - assert(std::any_cast(&ca) == nullptr); +void test_cast_non_copyable_type() { + // Even though 'any' never stores non-copyable types + // we still need to support any_cast(ptr) + struct NoCopy { + NoCopy(NoCopy const&) = delete; + }; + std::any a(42); + std::any const& ca = a; + assert(std::any_cast(&a) == nullptr); + assert(std::any_cast(&ca) == nullptr); } void test_cast_array() { - int arr[3]; - std::any a(arr); - RTTI_ASSERT(a.type() == typeid(int*)); // contained value is decayed - // We can't get an array out - int (*p)[3] = std::any_cast(&a); - assert(p == nullptr); + int arr[3]; + std::any a(arr); + RTTI_ASSERT(a.type() == typeid(int*)); // contained value is decayed + // We can't get an array out + int (*p)[3] = std::any_cast(&a); + assert(p == nullptr); } void test_fn() {} void test_cast_function_pointer() { - using T = void(*)(); - std::any a(test_fn); - // An any can never store a function type, but we should at least be able - // to ask. - assert(std::any_cast(&a) == nullptr); - T fn_ptr = std::any_cast(a); - assert(fn_ptr == test_fn); + using T = void (*)(); + std::any a(test_fn); + // An any can never store a function type, but we should at least be able + // to ask. + assert(std::any_cast(&a) == nullptr); + T fn_ptr = std::any_cast(a); + assert(fn_ptr == test_fn); } int main(int, char**) { - test_cast_is_noexcept(); - test_cast_return_type(); - test_cast_nullptr(); - test_cast_empty(); - test_cast(); - test_cast(); - test_cast_non_copyable_type(); - test_cast_array(); - test_cast_function_pointer(); + test_cast_is_noexcept(); + test_cast_return_type(); + test_cast_nullptr(); + test_cast_empty(); + test_cast(); + test_cast(); + test_cast_non_copyable_type(); + test_cast_array(); + test_cast_function_pointer(); return 0; } diff --git a/libcxx/test/std/utilities/any/any.nonmembers/any.cast/any_cast_reference.pass.cpp b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/any_cast_reference.pass.cpp index 079be66771944..4ba9bf6f30823 100644 --- a/libcxx/test/std/utilities/any/any.nonmembers/any.cast/any_cast_reference.pass.cpp +++ b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/any_cast_reference.pass.cpp @@ -29,278 +29,275 @@ // Test that the operators are NOT marked noexcept. void test_cast_is_not_noexcept() { - std::any a; - static_assert(!noexcept(std::any_cast(static_cast(a))), ""); - static_assert(!noexcept(std::any_cast(static_cast(a))), ""); - static_assert(!noexcept(std::any_cast(static_cast(a))), ""); + std::any a; + static_assert(!noexcept(std::any_cast(static_cast(a))), ""); + static_assert(!noexcept(std::any_cast(static_cast(a))), ""); + static_assert(!noexcept(std::any_cast(static_cast(a))), ""); } // Test that the return type of any_cast is correct. void test_cast_return_type() { - std::any a; - static_assert(std::is_same(a)), int>::value, ""); - static_assert(std::is_same(a)), int>::value, ""); - static_assert(std::is_same(a)), int&>::value, ""); - static_assert(std::is_same(a)), int const&>::value, ""); - static_assert(std::is_same(a)), int&&>::value, ""); - static_assert(std::is_same(a)), int const&&>::value, ""); - - static_assert(std::is_same(std::move(a))), int>::value, ""); - static_assert(std::is_same(std::move(a))), int>::value, ""); - static_assert(std::is_same(std::move(a))), int&>::value, ""); - static_assert(std::is_same(std::move(a))), int const&>::value, ""); - static_assert(std::is_same(std::move(a))), int&&>::value, ""); - static_assert(std::is_same(std::move(a))), int const&&>::value, ""); - - const std::any& ca = a; - static_assert(std::is_same(ca)), int>::value, ""); - static_assert(std::is_same(ca)), int>::value, ""); - static_assert(std::is_same(ca)), int const&>::value, ""); - static_assert(std::is_same(ca)), int const&&>::value, ""); + std::any a; + static_assert(std::is_same(a)), int>::value, ""); + static_assert(std::is_same(a)), int>::value, ""); + static_assert(std::is_same(a)), int&>::value, ""); + static_assert(std::is_same(a)), int const&>::value, ""); + static_assert(std::is_same(a)), int&&>::value, ""); + static_assert(std::is_same(a)), int const&&>::value, ""); + + static_assert(std::is_same(std::move(a))), int>::value, ""); + static_assert(std::is_same(std::move(a))), int>::value, ""); + static_assert(std::is_same(std::move(a))), int&>::value, ""); + static_assert(std::is_same(std::move(a))), int const&>::value, ""); + static_assert(std::is_same(std::move(a))), int&&>::value, ""); + static_assert(std::is_same(std::move(a))), int const&&>::value, ""); + + const std::any& ca = a; + static_assert(std::is_same(ca)), int>::value, ""); + static_assert(std::is_same(ca)), int>::value, ""); + static_assert(std::is_same(ca)), int const&>::value, ""); + static_assert(std::is_same(ca)), int const&&>::value, ""); } template -void checkThrows(std::any& a) -{ +void checkThrows(std::any& a) { #if !defined(TEST_HAS_NO_EXCEPTIONS) - try { - TEST_IGNORE_NODISCARD std::any_cast(a); - assert(false); - } catch (const std::bad_any_cast&) { - // do nothing - } catch (...) { - assert(false); - } - - try { - TEST_IGNORE_NODISCARD std::any_cast(static_cast(a)); - assert(false); - } catch (const std::bad_any_cast&) { - // do nothing - } catch (...) { - assert(false); - } - - try { - using RefType = typename std::conditional< - std::is_lvalue_reference::value, - typename std::remove_reference::type&&, - Type - >::type; - TEST_IGNORE_NODISCARD std::any_cast(static_cast(a)); - assert(false); - } catch (const std::bad_any_cast&) { - // do nothing - } catch (...) { - assert(false); - } + try { + TEST_IGNORE_NODISCARD std::any_cast(a); + assert(false); + } catch (const std::bad_any_cast&) { + // do nothing + } catch (...) { + assert(false); + } + + try { + TEST_IGNORE_NODISCARD std::any_cast(static_cast(a)); + assert(false); + } catch (const std::bad_any_cast&) { + // do nothing + } catch (...) { + assert(false); + } + + try { + using RefType = typename std:: + conditional< std::is_lvalue_reference::value, typename std::remove_reference::type&&, Type >::type; + TEST_IGNORE_NODISCARD std::any_cast(static_cast(a)); + assert(false); + } catch (const std::bad_any_cast&) { + // do nothing + } catch (...) { + assert(false); + } #else - (TEST_IGNORE_NODISCARD a); + (TEST_IGNORE_NODISCARD a); #endif } void test_cast_empty() { - // None of these operations should allocate. - DisableAllocationGuard g; (TEST_IGNORE_NODISCARD g); - std::any a; - checkThrows(a); + // None of these operations should allocate. + DisableAllocationGuard g; + (TEST_IGNORE_NODISCARD g); + std::any a; + checkThrows(a); } template void test_cast_to_reference() { - assert(Type::count == 0); - Type::reset(); + assert(Type::count == 0); + Type::reset(); + { + std::any a = Type(42); + const std::any& ca = a; + assert(Type::count == 1); + assert(Type::copied == 0); + assert(Type::moved == 1); + + // Try a cast to a bad type. + // NOTE: Type cannot be an int. + checkThrows(a); + checkThrows(a); + checkThrows(a); + checkThrows(a); + + // Check getting a type by reference from a non-const lvalue any. + { + Type& v = std::any_cast(a); + assert(v.value == 42); + + Type const& cv = std::any_cast(a); + assert(&cv == &v); + } + // Check getting a type by reference from a const lvalue any. { - std::any a = Type(42); - const std::any& ca = a; - assert(Type::count == 1); - assert(Type::copied == 0); - assert(Type::moved == 1); - - // Try a cast to a bad type. - // NOTE: Type cannot be an int. - checkThrows(a); - checkThrows(a); - checkThrows(a); - checkThrows(a); - - // Check getting a type by reference from a non-const lvalue any. - { - Type& v = std::any_cast(a); - assert(v.value == 42); - - Type const &cv = std::any_cast(a); - assert(&cv == &v); - } - // Check getting a type by reference from a const lvalue any. - { - Type const& v = std::any_cast(ca); - assert(v.value == 42); - - Type const &cv = std::any_cast(ca); - assert(&cv == &v); - } - // Check getting a type by reference from a const rvalue any. - { - Type const& v = std::any_cast(std::move(ca)); - assert(v.value == 42); - - Type const &cv = std::any_cast(std::move(ca)); - assert(&cv == &v); - } - // Check getting a type by reference from a const rvalue any. - { - Type&& v = std::any_cast(std::move(a)); - assert(v.value == 42); - assert(std::any_cast(a).value == 42); - - Type&& cv = std::any_cast(std::move(a)); - assert(&cv == &v); - assert(std::any_cast(a).value == 42); - } - // Check getting a type by reference from a const rvalue any. - { - Type const&& v = std::any_cast(std::move(a)); - assert(v.value == 42); - assert(std::any_cast(a).value == 42); - - Type const&& cv = std::any_cast(std::move(a)); - assert(&cv == &v); - assert(std::any_cast(a).value == 42); - } - // Check that the original object hasn't been changed. - assertContains(a, 42); - - // Check that no objects have been created/copied/moved. - assert(Type::count == 1); - assert(Type::copied == 0); - assert(Type::moved == 1); + Type const& v = std::any_cast(ca); + assert(v.value == 42); + + Type const& cv = std::any_cast(ca); + assert(&cv == &v); } - assert(Type::count == 0); + // Check getting a type by reference from a const rvalue any. + { + Type const& v = std::any_cast(std::move(ca)); + assert(v.value == 42); + + Type const& cv = std::any_cast(std::move(ca)); + assert(&cv == &v); + } + // Check getting a type by reference from a const rvalue any. + { + Type&& v = std::any_cast(std::move(a)); + assert(v.value == 42); + assert(std::any_cast(a).value == 42); + + Type&& cv = std::any_cast(std::move(a)); + assert(&cv == &v); + assert(std::any_cast(a).value == 42); + } + // Check getting a type by reference from a const rvalue any. + { + Type const&& v = std::any_cast(std::move(a)); + assert(v.value == 42); + assert(std::any_cast(a).value == 42); + + Type const&& cv = std::any_cast(std::move(a)); + assert(&cv == &v); + assert(std::any_cast(a).value == 42); + } + // Check that the original object hasn't been changed. + assertContains(a, 42); + + // Check that no objects have been created/copied/moved. + assert(Type::count == 1); + assert(Type::copied == 0); + assert(Type::moved == 1); + } + assert(Type::count == 0); } template void test_cast_to_value() { - assert(Type::count == 0); + assert(Type::count == 0); + Type::reset(); + { + std::any a = Type(42); + assert(Type::count == 1); + assert(Type::copied == 0); + assert(Type::moved == 1); + + // Try a cast to a bad type. + // NOTE: Type cannot be an int. + checkThrows(a); + checkThrows(a); + checkThrows(a); + checkThrows(a); + + Type::reset(); // NOTE: reset does not modify Type::count + // Check getting Type by value from a non-const lvalue any. + // This should cause the non-const copy constructor to be called. + { + Type t = std::any_cast(a); + + assert(Type::count == 2); + assert(Type::copied == 1); + assert(Type::const_copied == 0); + assert(Type::non_const_copied == 1); + assert(Type::moved == 0); + assert(t.value == 42); + } + assert(Type::count == 1); + Type::reset(); + // Check getting const Type by value from a non-const lvalue any. + // This should cause the const copy constructor to be called. + { + Type t = std::any_cast(a); + + assert(Type::count == 2); + assert(Type::copied == 1); + assert(Type::const_copied == 0); + assert(Type::non_const_copied == 1); + assert(Type::moved == 0); + assert(t.value == 42); + } + assert(Type::count == 1); Type::reset(); + // Check getting Type by value from a non-const lvalue any. + // This should cause the const copy constructor to be called. { - std::any a = Type(42); - assert(Type::count == 1); - assert(Type::copied == 0); - assert(Type::moved == 1); - - // Try a cast to a bad type. - // NOTE: Type cannot be an int. - checkThrows(a); - checkThrows(a); - checkThrows(a); - checkThrows(a); - - Type::reset(); // NOTE: reset does not modify Type::count - // Check getting Type by value from a non-const lvalue any. - // This should cause the non-const copy constructor to be called. - { - Type t = std::any_cast(a); - - assert(Type::count == 2); - assert(Type::copied == 1); - assert(Type::const_copied == 0); - assert(Type::non_const_copied == 1); - assert(Type::moved == 0); - assert(t.value == 42); - } - assert(Type::count == 1); - Type::reset(); - // Check getting const Type by value from a non-const lvalue any. - // This should cause the const copy constructor to be called. - { - Type t = std::any_cast(a); - - assert(Type::count == 2); - assert(Type::copied == 1); - assert(Type::const_copied == 0); - assert(Type::non_const_copied == 1); - assert(Type::moved == 0); - assert(t.value == 42); - } - assert(Type::count == 1); - Type::reset(); - // Check getting Type by value from a non-const lvalue any. - // This should cause the const copy constructor to be called. - { - Type t = std::any_cast(static_cast(a)); - - assert(Type::count == 2); - assert(Type::copied == 1); - assert(Type::const_copied == 1); - assert(Type::non_const_copied == 0); - assert(Type::moved == 0); - assert(t.value == 42); - } - assert(Type::count == 1); - Type::reset(); - // Check getting Type by value from a non-const rvalue any. - // This should cause the non-const copy constructor to be called. - { - Type t = std::any_cast(static_cast(a)); - - assert(Type::count == 2); - assert(Type::moved == 1); - assert(Type::copied == 0); - assert(Type::const_copied == 0); - assert(Type::non_const_copied == 0); - assert(t.value == 42); - assert(std::any_cast(a).value == 0); - std::any_cast(a).value = 42; // reset the value - } - assert(Type::count == 1); - Type::reset(); - // Check getting const Type by value from a non-const rvalue any. - // This should cause the const copy constructor to be called. - { - Type t = std::any_cast(static_cast(a)); - - assert(Type::count == 2); - assert(Type::copied == 0); - assert(Type::const_copied == 0); - assert(Type::non_const_copied == 0); - assert(Type::moved == 1); - assert(t.value == 42); - assert(std::any_cast(a).value == 0); - std::any_cast(a).value = 42; // reset the value - } - assert(Type::count == 1); - Type::reset(); - // Check getting Type by value from a const rvalue any. - // This should cause the const copy constructor to be called. - { - Type t = std::any_cast(static_cast(a)); - - assert(Type::count == 2); - assert(Type::copied == 1); - assert(Type::const_copied == 1); - assert(Type::non_const_copied == 0); - assert(Type::moved == 0); - assert(t.value == 42); - assert(std::any_cast(a).value == 42); - } - // Ensure we still only have 1 Type object alive. - assert(Type::count == 1); - - // Check that the original object hasn't been changed. - assertContains(a, 42); + Type t = std::any_cast(static_cast(a)); + + assert(Type::count == 2); + assert(Type::copied == 1); + assert(Type::const_copied == 1); + assert(Type::non_const_copied == 0); + assert(Type::moved == 0); + assert(t.value == 42); } - assert(Type::count == 0); + assert(Type::count == 1); + Type::reset(); + // Check getting Type by value from a non-const rvalue any. + // This should cause the non-const copy constructor to be called. + { + Type t = std::any_cast(static_cast(a)); + + assert(Type::count == 2); + assert(Type::moved == 1); + assert(Type::copied == 0); + assert(Type::const_copied == 0); + assert(Type::non_const_copied == 0); + assert(t.value == 42); + assert(std::any_cast(a).value == 0); + std::any_cast(a).value = 42; // reset the value + } + assert(Type::count == 1); + Type::reset(); + // Check getting const Type by value from a non-const rvalue any. + // This should cause the const copy constructor to be called. + { + Type t = std::any_cast(static_cast(a)); + + assert(Type::count == 2); + assert(Type::copied == 0); + assert(Type::const_copied == 0); + assert(Type::non_const_copied == 0); + assert(Type::moved == 1); + assert(t.value == 42); + assert(std::any_cast(a).value == 0); + std::any_cast(a).value = 42; // reset the value + } + assert(Type::count == 1); + Type::reset(); + // Check getting Type by value from a const rvalue any. + // This should cause the const copy constructor to be called. + { + Type t = std::any_cast(static_cast(a)); + + assert(Type::count == 2); + assert(Type::copied == 1); + assert(Type::const_copied == 1); + assert(Type::non_const_copied == 0); + assert(Type::moved == 0); + assert(t.value == 42); + assert(std::any_cast(a).value == 42); + } + // Ensure we still only have 1 Type object alive. + assert(Type::count == 1); + + // Check that the original object hasn't been changed. + assertContains(a, 42); + } + assert(Type::count == 0); } int main(int, char**) { - test_cast_is_not_noexcept(); - test_cast_return_type(); - test_cast_empty(); - test_cast_to_reference(); - test_cast_to_reference(); - test_cast_to_value(); - test_cast_to_value(); + test_cast_is_not_noexcept(); + test_cast_return_type(); + test_cast_empty(); + test_cast_to_reference(); + test_cast_to_reference(); + test_cast_to_value(); + test_cast_to_value(); return 0; } diff --git a/libcxx/test/std/utilities/any/any.nonmembers/any.cast/any_cast_request_invalid_value_category.verify.cpp b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/any_cast_request_invalid_value_category.verify.cpp index 34f785fdff6bc..c9800c5a01431 100644 --- a/libcxx/test/std/utilities/any/any.nonmembers/any.cast/any_cast_request_invalid_value_category.verify.cpp +++ b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/any_cast_request_invalid_value_category.verify.cpp @@ -19,47 +19,35 @@ struct TestType {}; -void test_const_lvalue_cast_request_non_const_lvalue() -{ - const std::any a; - // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}} - // expected-error@any:* {{drops 'const' qualifier}} - std::any_cast(a); // expected-note {{requested here}} - - const std::any a2(42); - // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}} - // expected-error@any:* {{drops 'const' qualifier}} - std::any_cast(a2); // expected-note {{requested here}} -} - -void test_lvalue_any_cast_request_rvalue() -{ - std::any a; - // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be an lvalue reference or a CopyConstructible type}} - std::any_cast(a); // expected-note {{requested here}} - - std::any a2(42); - // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be an lvalue reference or a CopyConstructible type}} - std::any_cast(a2); // expected-note {{requested here}} +void test_const_lvalue_cast_request_non_const_lvalue() { + const std::any a; + // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}} + // expected-error@any:* {{drops 'const' qualifier}} + std::any_cast(a); // expected-note {{requested here}} + + const std::any a2(42); + // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}} + // expected-error@any:* {{drops 'const' qualifier}} + std::any_cast(a2); // expected-note {{requested here}} } -void test_rvalue_any_cast_request_lvalue() -{ - std::any a; - // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be an rvalue reference or a CopyConstructible type}} - // expected-error@any:* {{non-const lvalue reference to type 'TestType' cannot bind to a temporary}} - std::any_cast(std::move(a)); // expected-note {{requested here}} +void test_lvalue_any_cast_request_rvalue() { + std::any a; + // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be an lvalue reference or a CopyConstructible type}} + std::any_cast(a); // expected-note {{requested here}} - // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be an rvalue reference or a CopyConstructible type}} - // expected-error@any:* {{non-const lvalue reference to type 'int' cannot bind to a temporary}} - std::any_cast(42); + std::any a2(42); + // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be an lvalue reference or a CopyConstructible type}} + std::any_cast(a2); // expected-note {{requested here}} } -int main(int, char**) -{ - test_const_lvalue_cast_request_non_const_lvalue(); - test_lvalue_any_cast_request_rvalue(); - test_rvalue_any_cast_request_lvalue(); +void test_rvalue_any_cast_request_lvalue() { + std::any a; + // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be an rvalue reference or a CopyConstructible type}} + // expected-error@any:* {{non-const lvalue reference to type 'TestType' cannot bind to a temporary}} + std::any_cast(std::move(a)); // expected-note {{requested here}} - return 0; + // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be an rvalue reference or a CopyConstructible type}} + // expected-error@any:* {{non-const lvalue reference to type 'int' cannot bind to a temporary}} + std::any_cast(42); } diff --git a/libcxx/test/std/utilities/any/any.nonmembers/any.cast/const_correctness.verify.cpp b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/const_correctness.verify.cpp index 1830626d7f410..a60bb1bbc8709 100644 --- a/libcxx/test/std/utilities/any/any.nonmembers/any.cast/const_correctness.verify.cpp +++ b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/const_correctness.verify.cpp @@ -27,18 +27,18 @@ struct TestType {}; struct TestType2 {}; -void f() { - std::any a; +void test() { + std::any a; - // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}} - std::any_cast(static_cast(a)); // expected-note {{requested here}} + // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}} + std::any_cast(static_cast(a)); // expected-note {{requested here}} - // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}} - std::any_cast(static_cast(a)); // expected-note {{requested here}} + // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}} + std::any_cast(static_cast(a)); // expected-note {{requested here}} - // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}} - std::any_cast(static_cast(a)); // expected-note {{requested here}} + // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}} + std::any_cast(static_cast(a)); // expected-note {{requested here}} - // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}} - std::any_cast(static_cast(a)); // expected-note {{requested here}} + // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}} + std::any_cast(static_cast(a)); // expected-note {{requested here}} } diff --git a/libcxx/test/std/utilities/any/any.nonmembers/any.cast/not_copy_constructible.verify.cpp b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/not_copy_constructible.verify.cpp index 3b9aac581a083..8b25d5ee520b7 100644 --- a/libcxx/test/std/utilities/any/any.nonmembers/any.cast/not_copy_constructible.verify.cpp +++ b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/not_copy_constructible.verify.cpp @@ -31,27 +31,27 @@ #include struct no_copy { - no_copy() {} - no_copy(no_copy &&) {} - no_copy(no_copy const &) = delete; + no_copy() {} + no_copy(no_copy&&) {} + no_copy(no_copy const&) = delete; }; struct no_move { - no_move() {} - no_move(no_move&&) = delete; - no_move(no_move const&) {} + no_move() {} + no_move(no_move&&) = delete; + no_move(no_move const&) {} }; -void f() { - std::any a; - // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be an lvalue reference or a CopyConstructible type}} - std::any_cast(static_cast(a)); // expected-note {{requested here}} +void test() { + std::any a; + // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be an lvalue reference or a CopyConstructible type}} + std::any_cast(static_cast(a)); // expected-note {{requested here}} - // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}} - std::any_cast(static_cast(a)); // expected-note {{requested here}} + // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}} + std::any_cast(static_cast(a)); // expected-note {{requested here}} - std::any_cast(static_cast(a)); // OK + std::any_cast(static_cast(a)); // OK - // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be an rvalue reference or a CopyConstructible type}} - std::any_cast(static_cast(a)); + // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be an rvalue reference or a CopyConstructible type}} + std::any_cast(static_cast(a)); } 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 0a13fdf0c6d80..a055dcbf7f39c 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 @@ -18,35 +18,32 @@ #include -int main(int, char**) -{ - std::any a = 1; +void test() { + std::any a = 1; - // expected-error-re@any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}} - std::any_cast(&a); // expected-note {{requested here}} + // expected-error-re@any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}} + std::any_cast(&a); // expected-note {{requested here}} - // expected-error-re@any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}} - std::any_cast(&a); // expected-note {{requested here}} + // expected-error-re@any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}} + std::any_cast(&a); // expected-note {{requested here}} - // expected-error-re@any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}} - std::any_cast(&a); // expected-note {{requested here}} + // expected-error-re@any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}} + std::any_cast(&a); // expected-note {{requested here}} - // expected-error-re@any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}} - std::any_cast(&a); // expected-note {{requested here}} + // expected-error-re@any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}} + std::any_cast(&a); // expected-note {{requested here}} - const std::any& a2 = a; + const std::any& a2 = a; - // expected-error-re@any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}} - std::any_cast(&a2); // expected-note {{requested here}} + // expected-error-re@any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}} + std::any_cast(&a2); // expected-note {{requested here}} - // expected-error-re@any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}} - std::any_cast(&a2); // expected-note {{requested here}} + // expected-error-re@any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}} + std::any_cast(&a2); // expected-note {{requested here}} - // expected-error-re@any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}} - std::any_cast(&a2); // expected-note {{requested here}} + // expected-error-re@any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}} + std::any_cast(&a2); // expected-note {{requested here}} - // expected-error-re@any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}} - std::any_cast(&a2); // expected-note {{requested here}} - - return 0; + // expected-error-re@any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}} + std::any_cast(&a2); // expected-note {{requested here}} }