Skip to content

Commit 087aacc

Browse files
committed
[TaskLocals] Avoid use of defer in back deployed functions in the standard library.
Older versions of the 5.8 compiler have a bug when generating SIL for functions with `@_backDeploy` containing defer blocks (#62444) and for now we need the standard library interface to be compatible with those older compilers. Resolves rdar://104045168
1 parent cc70677 commit 087aacc

File tree

4 files changed

+32
-12
lines changed

4 files changed

+32
-12
lines changed

stdlib/public/BackDeployConcurrency/TaskCancellation.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,14 @@ public func withTaskCancellationHandler<T>(
4646
// unconditionally add the cancellation record to the task.
4747
// if the task was already cancelled, it will be executed right away.
4848
let record = _taskAddCancellationHandler(handler: handler)
49-
defer { _taskRemoveCancellationHandler(record: record) }
50-
51-
return try await operation()
49+
do {
50+
let result = try await operation()
51+
_taskRemoveCancellationHandler(record: record)
52+
return result
53+
} catch {
54+
_taskRemoveCancellationHandler(record: record)
55+
throw error
56+
}
5257
}
5358

5459
@available(SwiftStdlib 5.1, *)

stdlib/public/BackDeployConcurrency/TaskLocal.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,14 @@ public final class TaskLocal<Value: Sendable>: Sendable, CustomStringConvertible
145145
_checkIllegalTaskLocalBindingWithinWithTaskGroup(file: file, line: line)
146146

147147
_taskLocalValuePush(key: key, value: valueDuringOperation)
148-
defer { _taskLocalValuePop() }
149-
150-
return try await operation()
148+
do {
149+
let result = try await operation()
150+
_taskLocalValuePop()
151+
return result
152+
} catch {
153+
_taskLocalValuePop()
154+
throw error
155+
}
151156
}
152157

153158
/// Binds the task-local to the specific value for the duration of the

stdlib/public/Concurrency/TaskCancellation.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,14 @@ public func withTaskCancellationHandler<T>(
4646
// unconditionally add the cancellation record to the task.
4747
// if the task was already cancelled, it will be executed right away.
4848
let record = _taskAddCancellationHandler(handler: handler)
49-
defer { _taskRemoveCancellationHandler(record: record) }
50-
51-
return try await operation()
49+
do {
50+
let result = try await operation()
51+
_taskRemoveCancellationHandler(record: record)
52+
return result
53+
} catch {
54+
_taskRemoveCancellationHandler(record: record)
55+
throw error
56+
}
5257
}
5358

5459
@available(SwiftStdlib 5.1, *)

stdlib/public/Concurrency/TaskLocal.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,14 @@ public final class TaskLocal<Value: Sendable>: Sendable, CustomStringConvertible
145145
_checkIllegalTaskLocalBindingWithinWithTaskGroup(file: file, line: line)
146146

147147
_taskLocalValuePush(key: key, value: valueDuringOperation)
148-
defer { _taskLocalValuePop() }
149-
150-
return try await operation()
148+
do {
149+
let result = try await operation()
150+
_taskLocalValuePop()
151+
return result
152+
} catch {
153+
_taskLocalValuePop()
154+
throw error
155+
}
151156
}
152157

153158
/// Binds the task-local to the specific value for the duration of the

0 commit comments

Comments
 (0)