[libcxx-commits] [libcxx] [libc++] Fix the signatures of `std::rethrow_if_nested` (PR #91365)

via libcxx-commits libcxx-commits at lists.llvm.org
Tue May 7 10:45:22 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: A. Jiang (frederick-vs-ja)

<details>
<summary>Changes</summary>

Fixes #<!-- -->54470.

See [[global.functions]/2](https://eel.is/c++draft/global.functions#<!-- -->2):
> A call to a non-member function signature described in [support] through [thread] and [depr] shall behave as if the implementation declared no additional non-member function signatures.

and [[global.functions]/3](https://eel.is/c++draft/global.functions#<!-- -->3):
> An implementation shall not declare a non-member function signature with additional default arguments.

---
Full diff: https://github.com/llvm/llvm-project/pull/91365.diff


2 Files Affected:

- (modified) libcxx/include/__exception/nested_exception.h (+4-6) 
- (modified) libcxx/test/std/language.support/support.exception/except.nested/rethrow_if_nested.pass.cpp (+27) 


``````````diff
diff --git a/libcxx/include/__exception/nested_exception.h b/libcxx/include/__exception/nested_exception.h
index 1bf2df939258a..feb489f87f62f 100644
--- a/libcxx/include/__exception/nested_exception.h
+++ b/libcxx/include/__exception/nested_exception.h
@@ -84,17 +84,15 @@ struct __can_dynamic_cast
     : _BoolConstant< is_polymorphic<_From>::value &&
                      (!is_base_of<_To, _From>::value || is_convertible<const _From*, const _To*>::value)> {};
 
-template <class _Ep>
-inline _LIBCPP_HIDE_FROM_ABI void
-rethrow_if_nested(const _Ep& __e, __enable_if_t< __can_dynamic_cast<_Ep, nested_exception>::value>* = 0) {
+template <class _Ep, __enable_if_t< __can_dynamic_cast<_Ep, nested_exception>::value, int> = 0>
+inline _LIBCPP_HIDE_FROM_ABI void rethrow_if_nested(const _Ep& __e) {
   const nested_exception* __nep = dynamic_cast<const nested_exception*>(std::addressof(__e));
   if (__nep)
     __nep->rethrow_nested();
 }
 
-template <class _Ep>
-inline _LIBCPP_HIDE_FROM_ABI void
-rethrow_if_nested(const _Ep&, __enable_if_t<!__can_dynamic_cast<_Ep, nested_exception>::value>* = 0) {}
+template <class _Ep, __enable_if_t<!__can_dynamic_cast<_Ep, nested_exception>::value, int> = 0>
+inline _LIBCPP_HIDE_FROM_ABI void rethrow_if_nested(const _Ep&) {}
 
 } // namespace std
 
diff --git a/libcxx/test/std/language.support/support.exception/except.nested/rethrow_if_nested.pass.cpp b/libcxx/test/std/language.support/support.exception/except.nested/rethrow_if_nested.pass.cpp
index 39bf62b8193bb..30ce86f5277b0 100644
--- a/libcxx/test/std/language.support/support.exception/except.nested/rethrow_if_nested.pass.cpp
+++ b/libcxx/test/std/language.support/support.exception/except.nested/rethrow_if_nested.pass.cpp
@@ -18,8 +18,10 @@
 // template <class E> void rethrow_if_nested(const E& e);
 
 #include <exception>
+#include <cstddef>
 #include <cstdlib>
 #include <cassert>
+#include <utility>
 
 #include "test_macros.h"
 
@@ -58,6 +60,31 @@ class E1 : public std::nested_exception {};
 class E2 : public std::nested_exception {};
 class E : public E1, public E2 {};
 
+#if TEST_STD_VER >= 11
+template <class, class...>
+struct can_rethrow_if_nested_impl {
+  static constexpr bool value = false;
+};
+
+template <class... Args>
+struct can_rethrow_if_nested_impl<decltype((void)std::rethrow_if_nested(std::declval<Args>()...)), Args...> {
+  static constexpr bool value = true;
+};
+
+template <class... Args>
+struct can_rethrow_if_nested : can_rethrow_if_nested_impl<void, Args...> {};
+
+static_assert(!can_rethrow_if_nested<>::value, "");
+static_assert(can_rethrow_if_nested<A>::value, "");
+static_assert(can_rethrow_if_nested<const A&>::value, "");
+static_assert(can_rethrow_if_nested<B>::value, "");
+static_assert(can_rethrow_if_nested<const B&>::value, "");
+static_assert(!can_rethrow_if_nested<A, int*>::value, "");
+static_assert(!can_rethrow_if_nested<B, int*>::value, "");
+static_assert(!can_rethrow_if_nested<A, std::nullptr_t>::value, "");
+static_assert(!can_rethrow_if_nested<B, std::nullptr_t>::value, "");
+#endif
+
 int main(int, char**)
 {
     {

``````````

</details>


https://github.com/llvm/llvm-project/pull/91365


More information about the libcxx-commits mailing list