From c216d91a054b38529495127b8053a5029a98a4d5 Mon Sep 17 00:00:00 2001 From: Mike Ash Date: Tue, 7 Sep 2021 11:07:40 -0400 Subject: [PATCH] [Concurrency] Use overload resolution to stub voucher_needs_adopt. Using has_include to conditionalize the stubbing of voucher_needs_adopt doesn't work when the SDK provides an older header that doesn't declare the function. Instead, always provide a stub, but use overload resolution to prefer the SDK's declaration. Declare the stub as taking `void *`. When calling it with `voucher_t`, the implicit conversion is allowed, but causes the overload to be disfavored when the SDK's declaration takes `voucher_t`. rdar://82797720 --- include/swift/Runtime/VoucherShims.h | 33 ++++++++++++++++------------ 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/include/swift/Runtime/VoucherShims.h b/include/swift/Runtime/VoucherShims.h index a50211215aa08..1d4120b866508 100644 --- a/include/swift/Runtime/VoucherShims.h +++ b/include/swift/Runtime/VoucherShims.h @@ -41,13 +41,6 @@ #if SWIFT_HAS_VOUCHER_HEADER -static inline bool swift_voucher_needs_adopt(voucher_t _Nullable voucher) { - if (__builtin_available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)) { - return voucher_needs_adopt(voucher); - } - return true; -} - #else // If the header isn't available, declare the necessary calls here. @@ -64,10 +57,6 @@ extern "C" voucher_t _Nullable voucher_copy(void); // Consumes argument, returns retained value. extern "C" voucher_t _Nullable voucher_adopt(voucher_t _Nullable voucher); -static inline bool swift_voucher_needs_adopt(voucher_t _Nullable voucher) { - return true; -} - #endif // __has_include() static inline void swift_voucher_release(voucher_t _Nullable voucher) { @@ -88,10 +77,26 @@ static inline voucher_t _Nullable voucher_copy(void) { return nullptr; } static inline voucher_t _Nullable voucher_adopt(voucher_t _Nullable voucher) { return nullptr; } -static inline bool swift_voucher_needs_adopt(voucher_t _Nullable voucher) { - return true; -} static inline void swift_voucher_release(voucher_t _Nullable voucher) {} #endif // __APPLE__ +// Declare our own voucher_needs_adopt for when we don't get it from the SDK. +// This declaration deliberately takes `void *` instead of `voucher_t`. When the +// SDK provides one that takes `voucher_t`, then C++ overload resolution will +// favor that one. When the SDK does not provide a declaration, then the call +// site will invoke this stub instead. +static inline bool voucher_needs_adopt(void * _Nullable voucher) { + return true; +} + +static inline bool swift_voucher_needs_adopt(voucher_t _Nullable voucher) { +#if __APPLE__ + if (__builtin_available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)) + return voucher_needs_adopt(voucher); + return true; +#else + return voucher_needs_adopt(voucher); #endif +} + +#endif // SWIFT_CONCURRENCY_VOUCHERSHIMS_H