Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion stdlib/public/Concurrency/Executor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,13 @@ public struct UnownedSerialExecutor: Sendable {

@available(StdlibDeploymentTarget 6.2, *)
public func asSerialExecutor() -> (any SerialExecutor)? {
return unsafe unsafeBitCast(executor, to: (any SerialExecutor)?.self)
// The low bits of the witness table are used to encode the executor kind.
// any SerialExecutor needs a raw witness table pointer, so mask off the low
// bits, knowing the witness table pointer is aligned to the pointer size.
let rawExecutorData = unsafe unsafeBitCast(executor, to: (UInt, UInt).self)
let mask = ~(UInt(MemoryLayout<UnsafeRawPointer>.alignment) - 1)
let alignedExecutor = unsafe (rawExecutorData.0, rawExecutorData.1 & mask)
return unsafe unsafeBitCast(alignedExecutor, to: (any SerialExecutor)?.self)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ actor MyActor {

func test(expectedExecutor: NaiveQueueExecutor, expectedQueue: DispatchQueue) {
expectedExecutor.preconditionIsolated("Expected deep equality to trigger for \(expectedExecutor) and our \(self.executor)")

// Ensure we get a usable value from asSerialExecutor() when the executor
// has complex equality.
_ = expectedExecutor.asUnownedSerialExecutor().asSerialExecutor()!.asUnownedSerialExecutor()

print("\(Self.self): [\(self.executor.name)] on same context as [\(expectedExecutor.name)]")
}
}
Expand Down