[libcxx-commits] [libcxx] [libc++] fix std::make_exception_ptr interaction with ObjC (PR #135386)

James Y Knight via libcxx-commits libcxx-commits at lists.llvm.org
Wed Jun 4 07:11:01 PDT 2025


================
@@ -95,37 +96,49 @@ template <class _Ep>
 _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT {
 #  if _LIBCPP_HAS_EXCEPTIONS
 #    if _LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION && __cplusplus >= 201103L
-  using _Ep2 = __decay_t<_Ep>;
-
-  void* __ex = __cxxabiv1::__cxa_allocate_exception(sizeof(_Ep));
+  // Clang treats throwing ObjC types differently, and we have to preserve original throw-ing behavior
+  // to not break some ObjC invariants. ObjC types are thrown by a pointer, hence the condition;
+  // although it does also trigger for some valid c++ usages, this should be a case rare enough to
+  // not complicate the condition any further
+  if constexpr (std::is_pointer_v<_Ep>) {
+    try {
+      throw __e;
+    } catch (...) {
+      return current_exception();
+    }
+  } else {
+    using _Ep2 = __decay_t<_Ep>;
+
+    void* __ex = __cxxabiv1::__cxa_allocate_exception(sizeof(_Ep));
 #      ifdef __wasm__
-  // In Wasm, a destructor returns its argument
-  (void)__cxxabiv1::__cxa_init_primary_exception(
-      __ex, const_cast<std::type_info*>(&typeid(_Ep)), [](void* __p) -> void* {
+    // In Wasm, a destructor returns its argument
+    (void)__cxxabiv1::__cxa_init_primary_exception(
+        __ex, const_cast<std::type_info*>(&typeid(_Ep)), [](void* __p) -> void* {
 #      else
-  (void)__cxxabiv1::__cxa_init_primary_exception(__ex, const_cast<std::type_info*>(&typeid(_Ep)), [](void* __p) {
+    (void)__cxxabiv1::__cxa_init_primary_exception(__ex, const_cast<std::type_info*>(&typeid(_Ep)), [](void* __p) {
 #      endif
-        std::__destroy_at(static_cast<_Ep2*>(__p));
+          std::__destroy_at(static_cast<_Ep2*>(__p));
 #      ifdef __wasm__
-        return __p;
+          return __p;
 #      endif
-      });
-
-  try {
-    ::new (__ex) _Ep2(__e);
-    return exception_ptr::__from_native_exception_pointer(__ex);
-  } catch (...) {
-    __cxxabiv1::__cxa_free_exception(__ex);
-    return current_exception();
+        });
+
+    try {
+      ::new (__ex) _Ep2(__e);
+      return exception_ptr::__from_native_exception_pointer(__ex);
+    } catch (...) {
+      __cxxabiv1::__cxa_free_exception(__ex);
+      return current_exception();
+    }
   }
-#    else
+#    else // !(_LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION && __cplusplus >= 201703L)
----------------
jyknight wrote:

comment looks wrong? Should be "201103L"

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


More information about the libcxx-commits mailing list