From 1b33db2dea6a64d5b619b9e888175133c6d7f410 Mon Sep 17 00:00:00 2001 From: Euan Harris Date: Tue, 24 Sep 2024 13:32:18 +0100 Subject: [PATCH] Work around a type checking error when using the Static Linux SDK (#2898) # Motivation Building `swift-nio` with the Static Linux SDK (https://www.swift.org/documentation/articles/static-linux-getting-started.html), fails in `PosixSingletons+ConcurrencyTakeOver.swift`: ``` Building for debugging... error: emit-module command failed with exit code 1 (use -v to see invocation) /private/tmp/vaportest/.build/checkouts/swift-nio/Sources/NIOPosix/PosixSingletons+ConcurrencyTakeOver.swift:76:12: error: conflicting arguments to generic parameter 'T' ('Optional.AtomicRepresentation' vs. 'Optional.AtomicRepresentation') 74 | dlopen(nil, RTLD_NOW), 75 | "swift_task_enqueueGlobal_hook" 76 | )?.assumingMemoryBound(to: UnsafeRawPointer?.AtomicRepresentation.self) | `- error: conflicting arguments to generic parameter 'T' ('Optional.AtomicRepresentation' vs. 'Optional.AtomicRepresentation') 77 | guard let concurrencyEnqueueGlobalHookPtr = concurrencyEnqueueGlobalHookPtr else { 78 | return false ``` This happens because `Foundation` and `swift-atomics` both define `AtomicRepresentation`. A compiler problem (rdar://132885963) makes these extensions globally visible, causing the two `AtomicRepresentation` types to collide. This only affects non-Darwin platforms. # Modification This PR works around the problem by using a local type alias which does not collide with the other `AtomicRepresentation` types. # Result It is now possible to build `swift-nio` with the Static Linux SDK. Fixes: https://github.com/apple/swift-nio/issues/2893 Suggested-by: Alastair Houghton --- .../PosixSingletons+ConcurrencyTakeOver.swift | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Sources/NIOPosix/PosixSingletons+ConcurrencyTakeOver.swift b/Sources/NIOPosix/PosixSingletons+ConcurrencyTakeOver.swift index c75fe9a00b..e56242b12b 100644 --- a/Sources/NIOPosix/PosixSingletons+ConcurrencyTakeOver.swift +++ b/Sources/NIOPosix/PosixSingletons+ConcurrencyTakeOver.swift @@ -73,7 +73,7 @@ extension NIOSingletons { let concurrencyEnqueueGlobalHookPtr = dlsym( dlopen(nil, RTLD_NOW), "swift_task_enqueueGlobal_hook" - )?.assumingMemoryBound(to: UnsafeRawPointer?.AtomicRepresentation.self) + )?.assumingMemoryBound(to: UnsafeRawPointer?.AtomicRep.self) guard let concurrencyEnqueueGlobalHookPtr = concurrencyEnqueueGlobalHookPtr else { return false } @@ -126,3 +126,12 @@ extension NIOSingletons { #endif } } + +// Workaround for https://github.com/apple/swift-nio/issues/2893 +extension Optional +where + Wrapped: AtomicOptionalWrappable, + Wrapped.AtomicRepresentation.Value == Wrapped +{ + typealias AtomicRep = Wrapped.AtomicOptionalRepresentation +}