Skip to content

Commit 050a514

Browse files
committed
[Strict memory safety] Update standard library for unsafe treated as a call effect
1 parent ee9487b commit 050a514

31 files changed

+77
-74
lines changed

stdlib/public/Concurrency/CheckedContinuation.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public struct CheckedContinuation<T, E: Error>: Sendable {
162162
/// the caller. The task continues executing when its executor is
163163
/// able to reschedule it.
164164
public func resume(returning value: sending T) {
165-
if let c: UnsafeContinuation<T, E> = unsafe canary.takeContinuation() {
165+
if let c: UnsafeContinuation<T, E> = canary.takeContinuation() {
166166
unsafe c.resume(returning: value)
167167
} else {
168168
#if !$Embedded
@@ -186,7 +186,7 @@ public struct CheckedContinuation<T, E: Error>: Sendable {
186186
/// the caller. The task continues executing when its executor is
187187
/// able to reschedule it.
188188
public func resume(throwing error: __owned E) {
189-
if let c: UnsafeContinuation<T, E> = unsafe canary.takeContinuation() {
189+
if let c: UnsafeContinuation<T, E> = canary.takeContinuation() {
190190
unsafe c.resume(throwing: error)
191191
} else {
192192
#if !$Embedded

stdlib/public/Concurrency/Executor.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ internal func _task_serialExecutor_getExecutorRef<E>(_ executor: E) -> Builtin.E
886886
@_silgen_name("_task_taskExecutor_getTaskExecutorRef")
887887
internal func _task_taskExecutor_getTaskExecutorRef<E>(_ taskExecutor: E) -> Builtin.Executor
888888
where E: TaskExecutor {
889-
return unsafe taskExecutor.asUnownedTaskExecutor().executor
889+
return taskExecutor.asUnownedTaskExecutor().executor
890890
}
891891

892892
// Used by the concurrency runtime

stdlib/public/Concurrency/PartialAsyncTask.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ public struct ExecutorJob: Sendable, ~Copyable {
324324
/// Returns the result of executing the closure.
325325
@available(SwiftStdlib 6.2, *)
326326
public func withUnsafeExecutorPrivateData<R, E>(body: (UnsafeMutableRawBufferPointer) throws(E) -> R) throws(E) -> R {
327-
let base = unsafe _jobGetExecutorPrivateData(self.context)
327+
let base = _jobGetExecutorPrivateData(self.context)
328328
let size = unsafe 2 * MemoryLayout<OpaquePointer>.stride
329329
return unsafe try body(UnsafeMutableRawBufferPointer(start: base,
330330
count: size))
@@ -476,21 +476,21 @@ extension ExecutorJob {
476476

477477
/// Allocate a specified number of bytes of uninitialized memory.
478478
public func allocate(capacity: Int) -> UnsafeMutableRawBufferPointer {
479-
let base = unsafe _jobAllocate(context, capacity)
479+
let base = _jobAllocate(context, capacity)
480480
return unsafe UnsafeMutableRawBufferPointer(start: base, count: capacity)
481481
}
482482

483483
/// Allocate uninitialized memory for a single instance of type `T`.
484484
public func allocate<T>(as type: T.Type) -> UnsafeMutablePointer<T> {
485-
let base = unsafe _jobAllocate(context, MemoryLayout<T>.size)
485+
let base = _jobAllocate(context, MemoryLayout<T>.size)
486486
return unsafe base.bindMemory(to: type, capacity: 1)
487487
}
488488

489489
/// Allocate uninitialized memory for the specified number of
490490
/// instances of type `T`.
491491
public func allocate<T>(capacity: Int, as type: T.Type)
492492
-> UnsafeMutableBufferPointer<T> {
493-
let base = unsafe _jobAllocate(context, MemoryLayout<T>.stride * capacity)
493+
let base = _jobAllocate(context, MemoryLayout<T>.stride * capacity)
494494
let typedBase = unsafe base.bindMemory(to: T.self, capacity: capacity)
495495
return unsafe UnsafeMutableBufferPointer<T>(start: typedBase, count: capacity)
496496
}

stdlib/public/Concurrency/Task+PriorityEscalation.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ func __withTaskPriorityEscalationHandler0<T, E>(
125125
onPriorityEscalated handler0: @Sendable (UInt8, UInt8) -> Void,
126126
isolation: isolated (any Actor)? = #isolation
127127
) async throws(E) -> T {
128-
let record = unsafe _taskAddPriorityEscalationHandler(handler: handler0)
128+
let record = _taskAddPriorityEscalationHandler(handler: handler0)
129129
defer { unsafe _taskRemovePriorityEscalationHandler(record: record) }
130130

131131
return try await operation()
132-
}
132+
}

stdlib/public/Concurrency/Task+TaskExecutor.swift

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

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

151-
let record = unsafe _pushTaskExecutorPreference(taskExecutorBuiltin)
151+
let record = _pushTaskExecutorPreference(taskExecutorBuiltin)
152152
defer {
153153
unsafe _popTaskExecutorPreference(record: record)
154154
}
@@ -177,9 +177,9 @@ public func _unsafeInheritExecutor_withTaskExecutorPreference<T: Sendable>(
177177
}
178178

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

182-
let record = unsafe _pushTaskExecutorPreference(taskExecutorBuiltin)
182+
let record = _pushTaskExecutorPreference(taskExecutorBuiltin)
183183
defer {
184184
unsafe _popTaskExecutorPreference(record: record)
185185
}

stdlib/public/Concurrency/Task.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,7 +1301,7 @@ extension Task where Success == Never, Failure == Never {
13011301
@available(SwiftStdlib 5.1, *)
13021302
public func withUnsafeCurrentTask<T>(body: (UnsafeCurrentTask?) throws -> T) rethrows -> T {
13031303
guard let _task = _getCurrentAsyncTask() else {
1304-
return try unsafe body(nil)
1304+
return try body(nil)
13051305
}
13061306

13071307
// FIXME: This retain seems pretty wrong, however if we don't we WILL crash
@@ -1315,7 +1315,7 @@ public func withUnsafeCurrentTask<T>(body: (UnsafeCurrentTask?) throws -> T) ret
13151315
@available(SwiftStdlib 6.0, *)
13161316
public func withUnsafeCurrentTask<T>(body: (UnsafeCurrentTask?) async throws -> T) async rethrows -> T {
13171317
guard let _task = _getCurrentAsyncTask() else {
1318-
return try unsafe await body(nil)
1318+
return try await body(nil)
13191319
}
13201320

13211321
// FIXME: This retain seems pretty wrong, however if we don't we WILL crash
@@ -1601,7 +1601,7 @@ internal func _getCurrentTaskName() -> UnsafePointer<UInt8>?
16011601

16021602
@available(SwiftStdlib 6.2, *)
16031603
internal func _getCurrentTaskNameString() -> String? {
1604-
if let stringPtr = unsafe _getCurrentTaskName() {
1604+
if let stringPtr = _getCurrentTaskName() {
16051605
unsafe String(cString: stringPtr)
16061606
} else {
16071607
nil

stdlib/public/Concurrency/TaskCancellation.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public func withTaskCancellationHandler<T>(
7777
) async rethrows -> T {
7878
// unconditionally add the cancellation record to the task.
7979
// if the task was already cancelled, it will be executed right away.
80-
let record = unsafe _taskAddCancellationHandler(handler: handler)
80+
let record = _taskAddCancellationHandler(handler: handler)
8181
defer { unsafe _taskRemoveCancellationHandler(record: record) }
8282

8383
return try await operation()
@@ -98,7 +98,7 @@ public func _unsafeInheritExecutor_withTaskCancellationHandler<T>(
9898
) async rethrows -> T {
9999
// unconditionally add the cancellation record to the task.
100100
// if the task was already cancelled, it will be executed right away.
101-
let record = unsafe _taskAddCancellationHandler(handler: handler)
101+
let record = _taskAddCancellationHandler(handler: handler)
102102
defer { unsafe _taskRemoveCancellationHandler(record: record) }
103103

104104
return try await operation()

stdlib/public/Concurrency/TaskLocal.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public final class TaskLocal<Value: Sendable>: Sendable, CustomStringConvertible
178178
/// or if the task-local has no value bound, this will return the `defaultValue`
179179
/// of the task local.
180180
public func get() -> Value {
181-
guard let rawValue = unsafe _taskLocalValueGet(key: key) else {
181+
guard let rawValue = _taskLocalValueGet(key: key) else {
182182
return self.defaultValue
183183
}
184184

stdlib/public/Distributed/DistributedActorSystem.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ extension DistributedActorSystem {
549549
errorCode: .typeDeserializationFailure)
550550
}
551551

552-
guard let resultBuffer = _openExistential(returnTypeFromTypeInfo, do: unsafe doAllocateReturnTypeBuffer) else {
552+
guard let resultBuffer = _openExistential(returnTypeFromTypeInfo, do: doAllocateReturnTypeBuffer) else {
553553
throw ExecuteDistributedTargetError(
554554
message: "Failed to allocate buffer for distributed target return type",
555555
errorCode: .typeDeserializationFailure)

stdlib/public/core/Array.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,7 @@ extension Array: RangeReplaceableCollection {
951951
/// Entry point for `Array` literal construction; builds and returns
952952
/// an Array of `count` uninitialized elements.
953953
@inlinable
954+
@unsafe
954955
@_semantics("array.uninitialized")
955956
internal static func _allocateUninitialized(
956957
_ count: Int
@@ -966,6 +967,7 @@ extension Array: RangeReplaceableCollection {
966967
///
967968
/// - Precondition: `storage is _ContiguousArrayStorage`.
968969
@inlinable
970+
@unsafe
969971
@_semantics("array.uninitialized")
970972
@_effects(escaping storage => return.0.value**)
971973
@_effects(escaping storage.class*.value** => return.0.value**.class*.value**)

0 commit comments

Comments
 (0)