Skip to content

Commit aa22ef9

Browse files
committed
[Concurrency] Mark expressions as unsafe.
Also fix a missing availability annotation. rdar://141348916
1 parent 3378f37 commit aa22ef9

9 files changed

+57
-54
lines changed

stdlib/public/Concurrency/CFExecutor.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,23 @@ enum CoreFoundation {
2222
static let path =
2323
"/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation"
2424

25-
static let handle = dlopen(path, RTLD_NOLOAD)
25+
static let handle = unsafe dlopen(path, RTLD_NOLOAD)
2626

27-
static var isPresent: Bool { return handle != nil }
27+
static var isPresent: Bool { return unsafe handle != nil }
2828

2929
static func symbol<T>(_ name: String) -> T {
30-
guard let result = dlsym(handle, name) else {
30+
guard let result = unsafe dlsym(handle, name) else {
3131
fatalError("Unable to look up \(name) in CoreFoundation")
3232
}
33-
return unsafeBitCast(result, to: T.self)
33+
return unsafe unsafeBitCast(result, to: T.self)
3434
}
3535

3636
static let CFRunLoopRun: @convention(c) () -> () =
3737
symbol("CFRunLoopRun")
3838
static let CFRunLoopGetMain: @convention(c) () -> OpaquePointer =
39-
symbol("CFRunLoopGetMain")
39+
unsafe symbol("CFRunLoopGetMain")
4040
static let CFRunLoopStop: @convention(c) (OpaquePointer) -> () =
41-
symbol("CFRunLoopStop")
41+
unsafe symbol("CFRunLoopStop")
4242
}
4343

4444
// .. Main Executor ............................................................
@@ -51,7 +51,7 @@ public final class CFMainExecutor: DispatchMainExecutor, @unchecked Sendable {
5151
}
5252

5353
override public func stop() {
54-
CoreFoundation.CFRunLoopStop(CoreFoundation.CFRunLoopGetMain())
54+
unsafe CoreFoundation.CFRunLoopStop(CoreFoundation.CFRunLoopGetMain())
5555
}
5656

5757
}

stdlib/public/Concurrency/Clock.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ public struct ClockTraits: OptionSet {
216216
public static let wallTime = ClockTraits(rawValue: 1 << 2)
217217
}
218218

219+
@available(SwiftStdlib 6.2, *)
219220
extension Clock {
220221
/// The traits associated with this clock instance.
221222
@available(SwiftStdlib 6.2, *)

stdlib/public/Concurrency/DispatchExecutor.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,25 +87,25 @@ extension DispatchMainExecutor: EventableExecutor {
8787
public func registerEvent(
8888
handler: @escaping @Sendable () -> ()
8989
) -> ExecutorEvent {
90-
let source = _createDispatchEvent(handler: handler)
90+
let source = unsafe _createDispatchEvent(handler: handler)
9191

9292
// Stash the pointer in the id of the ExecutorEvent struct
93-
let eventId = unsafeBitCast(source, to: Int.self)
93+
let eventId = unsafe unsafeBitCast(source, to: Int.self)
9494
return ExecutorEvent(id: eventId)
9595
}
9696

9797
/// Deregister the given event.
9898
public func deregister(event: ExecutorEvent) {
9999
// Extract the source and cancel it
100-
let source = unsafeBitCast(event.id, to: OpaquePointer.self)
101-
_destroyDispatchEvent(source)
100+
let source = unsafe unsafeBitCast(event.id, to: OpaquePointer.self)
101+
unsafe _destroyDispatchEvent(source)
102102
}
103103

104104
/// Notify the executor of an event.
105105
public func notify(event: ExecutorEvent) {
106106
// Extract the source, but don't release it
107-
let source = unsafeBitCast(event.id, to: OpaquePointer.self)
108-
_signalDispatchEvent(source)
107+
let source = unsafe unsafeBitCast(event.id, to: OpaquePointer.self)
108+
unsafe _signalDispatchEvent(source)
109109
}
110110

111111
}

stdlib/public/Concurrency/Executor.swift

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ public protocol TaskExecutor: Executor {
418418
@available(SwiftStdlib 6.0, *)
419419
extension TaskExecutor {
420420
public func asUnownedTaskExecutor() -> UnownedTaskExecutor {
421-
UnownedTaskExecutor(ordinary: self)
421+
unsafe UnownedTaskExecutor(ordinary: self)
422422
}
423423
}
424424

@@ -648,11 +648,11 @@ extension Task where Success == Never, Failure == Never {
648648
@available(SwiftStdlib 6.2, *)
649649
@_unavailableInEmbedded
650650
public static var currentExecutor: (any Executor)? {
651-
if let taskExecutor = _getPreferredTaskExecutor().asTaskExecutor() {
651+
if let taskExecutor = unsafe _getPreferredTaskExecutor().asTaskExecutor() {
652652
return taskExecutor
653-
} else if let activeExecutor = _getActiveExecutor().asSerialExecutor() {
653+
} else if let activeExecutor = unsafe _getActiveExecutor().asSerialExecutor() {
654654
return activeExecutor
655-
} else if let taskExecutor = _getCurrentTaskExecutor().asTaskExecutor() {
655+
} else if let taskExecutor = unsafe _getCurrentTaskExecutor().asTaskExecutor() {
656656
return taskExecutor
657657
}
658658
return nil
@@ -717,9 +717,9 @@ public struct UnownedSerialExecutor: Sendable {
717717
@inlinable
718718
public init<E: SerialExecutor>(_ executor: __shared E) {
719719
if executor._isComplexEquality {
720-
self.executor = Builtin.buildComplexEqualitySerialExecutorRef(executor)
720+
unsafe self.executor = Builtin.buildComplexEqualitySerialExecutorRef(executor)
721721
} else {
722-
self.executor = Builtin.buildOrdinarySerialExecutorRef(executor)
722+
unsafe self.executor = Builtin.buildOrdinarySerialExecutorRef(executor)
723723
}
724724
}
725725

@@ -731,12 +731,13 @@ public struct UnownedSerialExecutor: Sendable {
731731

732732
@available(SwiftStdlib 6.2, *)
733733
public func asSerialExecutor() -> (any SerialExecutor)? {
734-
return unsafeBitCast(executor, to: (any SerialExecutor)?.self)
734+
return unsafe unsafeBitCast(executor, to: (any SerialExecutor)?.self)
735735
}
736736
}
737737

738738

739739
@available(SwiftStdlib 6.0, *)
740+
@unsafe
740741
@frozen
741742
public struct UnownedTaskExecutor: Sendable {
742743
@usableFromInline
@@ -746,28 +747,28 @@ public struct UnownedTaskExecutor: Sendable {
746747
/// which needs to reach for this from an @_transparent function which prevents @_spi use.
747748
@available(SwiftStdlib 6.0, *)
748749
public var _executor: Builtin.Executor {
749-
self.executor
750+
unsafe self.executor
750751
}
751752

752753
@inlinable
753754
public init(_ executor: Builtin.Executor) {
754-
self.executor = executor
755+
unsafe self.executor = executor
755756
}
756757

757758
@inlinable
758759
public init<E: TaskExecutor>(ordinary executor: __shared E) {
759-
self.executor = Builtin.buildOrdinaryTaskExecutorRef(executor)
760+
unsafe self.executor = Builtin.buildOrdinaryTaskExecutorRef(executor)
760761
}
761762

762763
@available(SwiftStdlib 6.2, *)
763764
@inlinable
764765
public init<E: TaskExecutor>(_ executor: __shared E) {
765-
self.executor = Builtin.buildOrdinaryTaskExecutorRef(executor)
766+
unsafe self.executor = Builtin.buildOrdinaryTaskExecutorRef(executor)
766767
}
767768

768769
@available(SwiftStdlib 6.2, *)
769770
public func asTaskExecutor() -> (any TaskExecutor)? {
770-
return unsafeBitCast(executor, to: (any TaskExecutor)?.self)
771+
return unsafe unsafeBitCast(executor, to: (any TaskExecutor)?.self)
771772
}
772773
}
773774

@@ -869,7 +870,7 @@ internal func _task_serialExecutor_getExecutorRef<E>(_ executor: E) -> Builtin.E
869870
@_silgen_name("_task_taskExecutor_getTaskExecutorRef")
870871
internal func _task_taskExecutor_getTaskExecutorRef<E>(_ taskExecutor: E) -> Builtin.Executor
871872
where E: TaskExecutor {
872-
return taskExecutor.asUnownedTaskExecutor().executor
873+
return unsafe taskExecutor.asUnownedTaskExecutor().executor
873874
}
874875

875876
// Used by the concurrency runtime

stdlib/public/Concurrency/ExecutorBridge.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,11 @@ fileprivate class DispatchEventHandlerBox {
130130
@available(SwiftStdlib 6.2, *)
131131
internal func _createDispatchEvent(handler: @escaping @Sendable () -> ()) -> OpaquePointer {
132132
let boxed = DispatchEventHandlerBox(handler: handler)
133-
let opaqueHandlerBox = Unmanaged.passRetained(boxed).toOpaque()
134-
return _createDispatchEventC(
133+
let opaqueHandlerBox = unsafe Unmanaged.passRetained(boxed).toOpaque()
134+
return unsafe _createDispatchEventC(
135135
handler: { context in
136-
let unmanaged = Unmanaged<DispatchEventHandlerBox>.fromOpaque(context)
137-
unmanaged.takeUnretainedValue().handler()
136+
let unmanaged = unsafe Unmanaged<DispatchEventHandlerBox>.fromOpaque(context)
137+
unsafe unmanaged.takeUnretainedValue().handler()
138138
},
139139
context: opaqueHandlerBox
140140
)
@@ -150,9 +150,9 @@ internal func _getDispatchEventContext(_ event: OpaquePointer) -> UnsafeMutableR
150150

151151
@available(SwiftStdlib 6.2, *)
152152
internal func _destroyDispatchEvent(_ event: OpaquePointer) {
153-
let context = _getDispatchEventContext(event)
154-
Unmanaged<DispatchEventHandlerBox>.fromOpaque(context).release()
155-
_destroyDispatchEventC(event)
153+
let context = unsafe _getDispatchEventContext(event)
154+
unsafe Unmanaged<DispatchEventHandlerBox>.fromOpaque(context).release()
155+
unsafe _destroyDispatchEventC(event)
156156
}
157157

158158
@available(SwiftStdlib 6.2, *)

stdlib/public/Concurrency/ExecutorImpl.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ internal func dontateToGlobalExecutor(
3434
context: UnsafeMutableRawPointer
3535
) {
3636
if let runnableExecutor = Task.defaultExecutor as? RunLoopExecutor {
37-
try! runnableExecutor.run(until: { Bool(condition(context)) })
37+
try! runnableExecutor.run(until: { unsafe Bool(condition(context)) })
3838
} else {
3939
fatalError("Global executor does not support thread donation")
4040
}
@@ -43,7 +43,7 @@ internal func dontateToGlobalExecutor(
4343
@available(SwiftStdlib 6.2, *)
4444
@_silgen_name("swift_task_getMainExecutorImpl")
4545
internal func getMainExecutor() -> UnownedSerialExecutor {
46-
return UnownedSerialExecutor(MainActor.executor)
46+
return unsafe UnownedSerialExecutor(MainActor.executor)
4747
}
4848

4949
@available(SwiftStdlib 6.2, *)

stdlib/public/Concurrency/GlobalConcurrentExecutor.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ internal final class _DefaultGlobalConcurrentExecutor: TaskExecutor {
6262
// We represent it as the `(0, 0)` ExecutorRef and it is handled properly
6363
// by the runtime, without having to call through to the
6464
// `_DefaultGlobalConcurrentExecutor` declared in Swift.
65-
UnownedTaskExecutor(_getUndefinedTaskExecutor())
65+
unsafe UnownedTaskExecutor(_getUndefinedTaskExecutor())
6666
}
6767
}
6868

stdlib/public/Concurrency/PartialAsyncTask.swift

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public struct UnownedJob: Sendable {
149149
@_alwaysEmitIntoClient
150150
@inlinable
151151
public func runSynchronously(on executor: UnownedTaskExecutor) {
152-
_swiftJobRunOnTaskExecutor(self, executor)
152+
unsafe _swiftJobRunOnTaskExecutor(self, executor)
153153
}
154154

155155
/// Run this job isolated to the passed in serial executor, while executing it on the specified task executor.
@@ -324,9 +324,10 @@ public struct ExecutorJob: Sendable, ~Copyable {
324324
/// Returns the result of executing the closure.
325325
@available(SwiftStdlib 6.2, *)
326326
public func withUnsafeExecutorPrivateData<R>(body: (UnsafeMutableRawBufferPointer) throws -> R) rethrows -> R {
327-
let base = _jobGetExecutorPrivateData(self.context)
328-
let size = 2 * MemoryLayout<OpaquePointer>.stride
329-
return try body(UnsafeMutableRawBufferPointer(start: base, count: size))
327+
let base = unsafe _jobGetExecutorPrivateData(self.context)
328+
let size = unsafe 2 * MemoryLayout<OpaquePointer>.stride
329+
return unsafe try body(UnsafeMutableRawBufferPointer(start: base,
330+
count: size))
330331
}
331332

332333
/// Kinds of schedulable jobs
@@ -418,7 +419,7 @@ extension ExecutorJob {
418419
@_alwaysEmitIntoClient
419420
@inlinable
420421
__consuming public func runSynchronously(on executor: UnownedTaskExecutor) {
421-
_swiftJobRunOnTaskExecutor(UnownedJob(self), executor)
422+
unsafe _swiftJobRunOnTaskExecutor(UnownedJob(self), executor)
422423
}
423424

424425
/// Run this job isolated to the passed in serial executor, while executing it on the specified task executor.
@@ -476,44 +477,44 @@ extension ExecutorJob {
476477

477478
/// Allocate a specified number of bytes of uninitialized memory.
478479
public func allocate(capacity: Int) -> UnsafeMutableRawBufferPointer {
479-
let base = _jobAllocate(context, capacity)
480-
return UnsafeMutableRawBufferPointer(start: base, count: capacity)
480+
let base = unsafe _jobAllocate(context, capacity)
481+
return unsafe UnsafeMutableRawBufferPointer(start: base, count: capacity)
481482
}
482483

483484
/// Allocate uninitialized memory for a single instance of type `T`.
484485
public func allocate<T>(as type: T.Type) -> UnsafeMutablePointer<T> {
485-
let base = _jobAllocate(context, MemoryLayout<T>.size)
486-
return base.bindMemory(to: type, capacity: 1)
486+
let base = unsafe _jobAllocate(context, MemoryLayout<T>.size)
487+
return unsafe base.bindMemory(to: type, capacity: 1)
487488
}
488489

489490
/// Allocate uninitialized memory for the specified number of
490491
/// instances of type `T`.
491492
public func allocate<T>(capacity: Int, as type: T.Type)
492493
-> UnsafeMutableBufferPointer<T> {
493-
let base = _jobAllocate(context, MemoryLayout<T>.stride * capacity)
494-
let typedBase = base.bindMemory(to: T.self, capacity: capacity)
495-
return UnsafeMutableBufferPointer<T>(start: typedBase, count: capacity)
494+
let base = unsafe _jobAllocate(context, MemoryLayout<T>.stride * capacity)
495+
let typedBase = unsafe base.bindMemory(to: T.self, capacity: capacity)
496+
return unsafe UnsafeMutableBufferPointer<T>(start: typedBase, count: capacity)
496497
}
497498

498499
/// Deallocate previously allocated memory. Note that the task
499500
/// allocator is stack disciplined, so if you deallocate a block of
500501
/// memory, all memory allocated after that block is also deallocated.
501502
public func deallocate(_ buffer: UnsafeMutableRawBufferPointer) {
502-
_jobDeallocate(context, buffer.baseAddress!)
503+
unsafe _jobDeallocate(context, buffer.baseAddress!)
503504
}
504505

505506
/// Deallocate previously allocated memory. Note that the task
506507
/// allocator is stack disciplined, so if you deallocate a block of
507508
/// memory, all memory allocated after that block is also deallocated.
508509
public func deallocate<T>(_ pointer: UnsafeMutablePointer<T>) {
509-
_jobDeallocate(context, UnsafeMutableRawPointer(pointer))
510+
unsafe _jobDeallocate(context, UnsafeMutableRawPointer(pointer))
510511
}
511512

512513
/// Deallocate previously allocated memory. Note that the task
513514
/// allocator is stack disciplined, so if you deallocate a block of
514515
/// memory, all memory allocated after that block is also deallocated.
515516
public func deallocate<T>(_ buffer: UnsafeMutableBufferPointer<T>) {
516-
_jobDeallocate(context, UnsafeMutableRawPointer(buffer.baseAddress!))
517+
unsafe _jobDeallocate(context, UnsafeMutableRawPointer(buffer.baseAddress!))
517518
}
518519

519520
}

stdlib/public/Concurrency/Task+TaskExecutor.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public func withTaskExecutorPreference<T, Failure>(
146146
}
147147

148148
let taskExecutorBuiltin: Builtin.Executor =
149-
taskExecutor.asUnownedTaskExecutor().executor
149+
unsafe taskExecutor.asUnownedTaskExecutor().executor
150150

151151
let record = unsafe _pushTaskExecutorPreference(taskExecutorBuiltin)
152152
defer {
@@ -177,7 +177,7 @@ public func _unsafeInheritExecutor_withTaskExecutorPreference<T: Sendable>(
177177
}
178178

179179
let taskExecutorBuiltin: Builtin.Executor =
180-
taskExecutor.asUnownedTaskExecutor().executor
180+
unsafe taskExecutor.asUnownedTaskExecutor().executor
181181

182182
let record = unsafe _pushTaskExecutorPreference(taskExecutorBuiltin)
183183
defer {
@@ -458,7 +458,7 @@ extension UnsafeCurrentTask {
458458
@available(SwiftStdlib 6.0, *)
459459
public var unownedTaskExecutor: UnownedTaskExecutor? {
460460
let ref = _getPreferredUnownedTaskExecutor()
461-
return UnownedTaskExecutor(ref)
461+
return unsafe UnownedTaskExecutor(ref)
462462
}
463463
}
464464

0 commit comments

Comments
 (0)