Skip to content

Make (Unsafe|Checked)Continuation's Sendable conformance conditional. #40192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions stdlib/public/Concurrency/CheckedContinuation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal func logFailedCheck(_ message: UnsafeRawPointer)
/// Implementation class that holds the `UnsafeContinuation` instance for
/// a `CheckedContinuation`.
@available(SwiftStdlib 5.1, *)
internal final class CheckedContinuationCanary {
internal final class CheckedContinuationCanary: @unchecked Sendable {
// The instance state is stored in tail-allocated raw memory, so that
// we can atomically check the continuation state.

Expand Down Expand Up @@ -110,7 +110,7 @@ internal final class CheckedContinuationCanary {
/// enough to obtain the extra checking without further source modification in
/// most circumstances.
@available(SwiftStdlib 5.1, *)
public struct CheckedContinuation<T, E: Error>: @unchecked Sendable {
public struct CheckedContinuation<T, E: Error> {
private let canary: CheckedContinuationCanary

/// Initialize a `CheckedContinuation` wrapper around an
Expand Down Expand Up @@ -175,6 +175,9 @@ public struct CheckedContinuation<T, E: Error>: @unchecked Sendable {
}
}

@available(SwiftStdlib 5.1, *)
extension CheckedContinuation: Sendable where T: Sendable { }

@available(SwiftStdlib 5.1, *)
extension CheckedContinuation {
/// Resume the task awaiting the continuation by having it either
Expand Down
5 changes: 4 additions & 1 deletion stdlib/public/Concurrency/PartialAsyncTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public struct UnownedJob: Sendable {

@available(SwiftStdlib 5.1, *)
@frozen
public struct UnsafeContinuation<T, E: Error>: Sendable {
public struct UnsafeContinuation<T, E: Error> {
@usableFromInline internal var context: Builtin.RawUnsafeContinuation

@_alwaysEmitIntoClient
Expand Down Expand Up @@ -106,6 +106,9 @@ public struct UnsafeContinuation<T, E: Error>: Sendable {
}
}

@available(SwiftStdlib 5.1, *)
extension UnsafeContinuation: Sendable where T: Sendable { }

@available(SwiftStdlib 5.1, *)
extension UnsafeContinuation {
/// Resume the task awaiting the continuation by having it either
Expand Down
17 changes: 17 additions & 0 deletions test/Concurrency/async_tasks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,23 @@ func test_unsafeThrowingContinuations() async throws {
// TODO: Potentially could offer some warnings if we know that a continuation was resumed or escaped at all in a closure?
}

// ==== Sendability ------------------------------------------------------------
class NotSendable { }
// expected-note@-1{{class 'NotSendable' does not conform to the 'Sendable' protocol}}

@available(SwiftStdlib 5.1, *)
func test_nonsendableContinuation() async throws {
let _: NotSendable = try await withUnsafeThrowingContinuation { continuation in
continuation.resume(returning: NotSendable())
}

let _: NotSendable = try await withUnsafeThrowingContinuation { continuation in
Task {
continuation.resume(returning: NotSendable()) // expected-warning{{cannot use parameter 'continuation' with a non-sendable type 'UnsafeContinuation<NotSendable, Error>' from concurrently-executed code}}
}
}
}

// ==== Detached Tasks ---------------------------------------------------------

@available(SwiftStdlib 5.1, *)
Expand Down