Skip to content

Commit bd5467b

Browse files
committed
[Freestanding] Disable unsafe continuations.
1 parent c20bd86 commit bd5467b

File tree

3 files changed

+71
-5
lines changed

3 files changed

+71
-5
lines changed

stdlib/public/Concurrency/PartialAsyncTask.swift

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public struct UnownedJob: Sendable {
3535
}
3636
}
3737

38+
#if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
3839
/// A mechanism to interface
3940
/// between synchronous and asynchronous code,
4041
/// without correctness checking.
@@ -213,6 +214,46 @@ extension UnsafeContinuation {
213214
self.resume(returning: ())
214215
}
215216
}
217+
#else
218+
@available(SwiftStdlib 5.1, *)
219+
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
220+
public struct UnsafeContinuation<T, E: Error>: Sendable {
221+
@available(SwiftStdlib 5.1, *)
222+
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
223+
public func resume(returning value: __owned T) where E == Never {
224+
fatalError("Unavailable in task-to-thread concurrency model")
225+
}
226+
@available(SwiftStdlib 5.1, *)
227+
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
228+
public func resume(returning value: __owned T) {
229+
fatalError("Unavailable in task-to-thread concurrency model")
230+
}
231+
@available(SwiftStdlib 5.1, *)
232+
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
233+
public func resume(throwing error: __owned E) {
234+
fatalError("Unavailable in task-to-thread concurrency model")
235+
}
236+
}
237+
@available(SwiftStdlib 5.1, *)
238+
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
239+
extension UnsafeContinuation {
240+
@available(SwiftStdlib 5.1, *)
241+
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
242+
public func resume<Er: Error>(with result: Result<T, Er>) where E == Error {
243+
fatalError("Unavailable in task-to-thread concurrency model")
244+
}
245+
@available(SwiftStdlib 5.1, *)
246+
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
247+
public func resume(with result: Result<T, E>) {
248+
fatalError("Unavailable in task-to-thread concurrency model")
249+
}
250+
@available(SwiftStdlib 5.1, *)
251+
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
252+
public func resume() where T == Void {
253+
fatalError("Unavailable in task-to-thread concurrency model")
254+
}
255+
}
256+
#endif
216257

217258
#if _runtime(_ObjC)
218259

@@ -246,6 +287,8 @@ internal func _resumeUnsafeThrowingContinuationWithError<T>(
246287

247288
#endif
248289

290+
291+
#if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
249292
/// Suspends the current task,
250293
/// then calls the given closure with an unsafe continuation for the current task.
251294
///
@@ -291,3 +334,20 @@ public func withUnsafeThrowingContinuation<T>(
291334
public func _abiEnableAwaitContinuation() {
292335
fatalError("never use this function")
293336
}
337+
#else
338+
@available(SwiftStdlib 5.1, *)
339+
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
340+
public func withUnsafeContinuation<T>(
341+
_ fn: (UnsafeContinuation<T, Never>) -> Void
342+
) async -> T {
343+
fatalError("Unavailable in task-to-thread concurrency model")
344+
}
345+
346+
@available(SwiftStdlib 5.1, *)
347+
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
348+
public func withUnsafeThrowingContinuation<T>(
349+
_ fn: (UnsafeContinuation<T, Error>) -> Void
350+
) async throws -> T {
351+
fatalError("Unavailable in task-to-thread concurrency model")
352+
}
353+
#endif

stdlib/public/Concurrency/SourceCompatibilityShims.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,9 +385,15 @@ extension ThrowingTaskGroup {
385385
}
386386
}
387387

388+
#if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
388389
@available(SwiftStdlib 5.1, *)
389390
@available(*, deprecated, message: "please use UnsafeContinuation<..., Error>")
390391
public typealias UnsafeThrowingContinuation<T> = UnsafeContinuation<T, Error>
392+
#else
393+
@available(SwiftStdlib 5.1, *)
394+
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
395+
public typealias UnsafeThrowingContinuation<T> = UnsafeContinuation<T, Error>
396+
#endif
391397

392398
@available(SwiftStdlib 5.1, *)
393399
@available(*, deprecated, renamed: "UnownedJob")

test/stdlib/freestanding_diags_stdlib.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ func chowMein() async {
1111
@MainActor // expected-error{{not permitted within task-to-thread concurrency model}}
1212
class ChowMein {}
1313

14-
func foo() async {
14+
@available(SwiftStdlib 5.1, *)
15+
func foo() async throws {
1516
Task<Void, Never> {} // expected-error{{Unavailable in task-to-thread concurrency model}}
1617
Task<Void, Error> {} // expected-error{{Unavailable in task-to-thread concurrency model}}
1718
Task<Void, Never>.detached {} // expected-error{{Unavailable in task-to-thread concurrency model}}
@@ -25,12 +26,11 @@ func foo() async {
2526
asyncDetached { () async throws -> () in } // expected-error{{Unavailable in task-to-thread concurrency model}}
2627
_ = MainActor.self // expected-error{{Unavailable in task-to-thread concurrency model}}
2728
let _: Int = await withCheckedContinuation { _ in } // expected-error{{Unavailable in task-to-thread concurrency model}}
28-
do {
2929
let _: Int = try await withCheckedThrowingContinuation { _ in } // expected-error{{Unavailable in task-to-thread concurrency model}}
30-
} catch let error {
31-
_ = error
32-
}
3330
_ = CheckedContinuation<Int, Never>.self // expected-error{{Unavailable in task-to-thread concurrency model}}
31+
let _: Int = await withUnsafeContinuation { _ in } // expected-error{{Unavailable in task-to-thread concurrency model}}
32+
let _: Int = try await withUnsafeThrowingContinuation { _ in } // expected-error{{Unavailable in task-to-thread concurrency model}}
33+
_ = UnsafeContinuation<Int, Never>.self // expected-error{{Unavailable in task-to-thread concurrency model}}
3434
}
3535

3636
func foo2(

0 commit comments

Comments
 (0)