|
10 | 10 | //===----------------------------------------------------------------------===// |
11 | 11 |
|
12 | 12 | #if swift(>=6.0) |
13 | | -private import class Dispatch.DispatchSemaphore |
| 13 | +@preconcurrency private import class Dispatch.DispatchSemaphore |
| 14 | +internal import class Foundation.NSLock |
14 | 15 | internal import class Foundation.ProcessInfo |
15 | 16 | #else |
16 | | -import class Dispatch.DispatchSemaphore |
| 17 | +@preconcurrency import class Dispatch.DispatchSemaphore |
| 18 | +import class Foundation.NSLock |
17 | 19 | import class Foundation.ProcessInfo |
18 | 20 | #endif |
19 | 21 |
|
@@ -518,16 +520,44 @@ private func asyncCustomCompletions( |
518 | 520 | ) throws -> [String] { |
519 | 521 | let (args, completingArgumentIndex, completingPrefix) = |
520 | 522 | try parseCustomCompletionArguments(from: args) |
521 | | - var completions: [String] = [] |
| 523 | + |
| 524 | + let completionsBox = SendableBox<[String]>([]) |
522 | 525 | let semaphore = DispatchSemaphore(value: 0) |
| 526 | + |
523 | 527 | Task { |
524 | | - completions = await complete( |
525 | | - args, completingArgumentIndex, completingPrefix |
| 528 | + completionsBox.value = await complete( |
| 529 | + args, |
| 530 | + completingArgumentIndex, |
| 531 | + completingPrefix |
526 | 532 | ) |
527 | 533 | semaphore.signal() |
528 | 534 | } |
| 535 | + |
529 | 536 | semaphore.wait() |
530 | | - return completions |
| 537 | + return completionsBox.value |
| 538 | +} |
| 539 | + |
| 540 | +// Helper class to make values sendable across concurrency boundaries |
| 541 | +private final class SendableBox<T>: @unchecked Sendable { |
| 542 | + private let lock = NSLock() |
| 543 | + private var _value: T |
| 544 | + |
| 545 | + init(_ value: T) { |
| 546 | + self._value = value |
| 547 | + } |
| 548 | + |
| 549 | + var value: T { |
| 550 | + get { |
| 551 | + lock.lock() |
| 552 | + defer { lock.unlock() } |
| 553 | + return _value |
| 554 | + } |
| 555 | + set { |
| 556 | + lock.lock() |
| 557 | + defer { lock.unlock() } |
| 558 | + _value = newValue |
| 559 | + } |
| 560 | + } |
531 | 561 | } |
532 | 562 |
|
533 | 563 | // MARK: Building Command Stacks |
|
0 commit comments