Skip to content

[region-isolation] Require T in assumeIsolated<T> to be Sendable. #72763

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
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
17 changes: 13 additions & 4 deletions stdlib/public/Concurrency/ExecutorAssertions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -342,12 +342,10 @@ extension Actor {
/// - Returns: the return value of the `operation`
/// - Throws: rethrows the `Error` thrown by the operation if it threw
@available(SwiftStdlib 5.1, *)
#if !$Embedded
@backDeployed(before: SwiftStdlib 5.9)
#endif
@_alwaysEmitIntoClient
@_unavailableFromAsync(message: "express the closure as an explicit function declared on the specified 'actor' instead")
@_unavailableInEmbedded
public nonisolated func assumeIsolated<T>(
public nonisolated func assumeIsolated<T : Sendable>(
_ operation: (isolated Self) throws -> T,
file: StaticString = #fileID, line: UInt = #line
) rethrows -> T {
Expand All @@ -373,6 +371,17 @@ extension Actor {
fatalError("unsupported compiler")
#endif
}

@available(SwiftStdlib 5.9, *)
@usableFromInline
@_unavailableInEmbedded
@_silgen_name("$sScAsE14assumeIsolated_4file4lineqd__qd__xYiKXE_s12StaticStringVSutKlF")
internal nonisolated func __abi__assumeIsolated<T : Sendable>(
_ operation: (isolated Self) throws -> T,
_ file: StaticString, _ line: UInt
) rethrows -> T {
try assumeIsolated(operation, file: file, line: line)
}
}

#endif // not SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
15 changes: 12 additions & 3 deletions stdlib/public/Concurrency/MainActor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,12 @@ extension MainActor {
/// - Returns: the return value of the `operation`
/// - Throws: rethrows the `Error` thrown by the operation if it threw
@available(SwiftStdlib 5.1, *)
@backDeployed(before: SwiftStdlib 5.9)
@_alwaysEmitIntoClient
@_unavailableFromAsync(message: "await the call to the @MainActor closure directly")
public static func assumeIsolated<T>(
public static func assumeIsolated<T : Sendable>(
_ operation: @MainActor () throws -> T,
file: StaticString = #fileID, line: UInt = #line
) rethrows -> T {

typealias YesActor = @MainActor () throws -> T
typealias NoActor = () throws -> T

Expand All @@ -166,5 +165,15 @@ extension MainActor {
fatalError("unsupported compiler")
#endif
}

@available(SwiftStdlib 5.9, *)
@usableFromInline
@_silgen_name("$sScM14assumeIsolated_4file4linexxyKScMYcXE_s12StaticStringVSutKlFZ")
internal static func __abi__assumeIsolated<T : Sendable>(
_ operation: @MainActor () throws -> T,
_ file: StaticString, _ line: UInt
) rethrows -> T {
try assumeIsolated(operation, file: file, line: line)
}
}
#endif
13 changes: 12 additions & 1 deletion stdlib/public/Distributed/DistributedAssertions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ extension DistributedActor {
/// - Throws: rethrows the `Error` thrown by the operation if it threw
@available(SwiftStdlib 5.9, *)
@_unavailableFromAsync(message: "express the closure as an explicit function declared on the specified 'distributed actor' instead")
public nonisolated func assumeIsolated<T>(
@_alwaysEmitIntoClient
public nonisolated func assumeIsolated<T : Sendable>(
_ operation: (isolated Self) throws -> T,
file: StaticString = #fileID, line: UInt = #line
) rethrows -> T {
Expand Down Expand Up @@ -181,6 +182,16 @@ extension DistributedActor {
fatalError("unsupported compiler")
#endif
}

@available(SwiftStdlib 5.9, *)
@usableFromInline
@_silgen_name("$s11Distributed0A5ActorPAAE14assumeIsolated_4file4lineqd__qd__xYiKXE_s12StaticStringVSutKlF")
internal nonisolated func __abi__assumeIsolated<T : Sendable>(
_ operation: (isolated Self) throws -> T,
_ file: StaticString, _ line: UInt
) rethrows -> T {
try assumeIsolated(operation, file: file, line: line)
}
}

/// WARNING: This function will CRASH rather than return `false` in new runtimes
Expand Down
33 changes: 33 additions & 0 deletions test/Concurrency/assumeIsolated.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// RUN: %target-build-swift -swift-version 5 %s -enable-upcoming-feature RegionBasedIsolation -strict-concurrency=complete -enable-experimental-feature TransferringArgsAndResults -Xfrontend -verify

// REQUIRES: concurrency
// REQUIRES: OS=macosx

class NonSendableKlass {} // expected-note 3{{class 'NonSendableKlass' does not conform to the 'Sendable' protocol}}

@available(macOS 10.15, *)
actor MyActor {
var x = NonSendableKlass()

nonisolated func doSomething() -> NonSendableKlass {
return self.assumeIsolated { isolatedSelf in // expected-warning {{type 'NonSendableKlass' does not conform to the 'Sendable' protocol}}
let x: NonSendableKlass = isolatedSelf.x
return x
}
}

nonisolated func doSomething2() -> NonSendableKlass {
let r: NonSendableKlass = assumeIsolated { isolatedSelf in // expected-warning {{type 'NonSendableKlass' does not conform to the 'Sendable' protocol}}
let x: NonSendableKlass = isolatedSelf.x
return x
}
return r
}
}

@available(macOS 10.15, *)
nonisolated func mainActorAssumeIsolated() -> NonSendableKlass {
return MainActor.assumeIsolated { // expected-warning {{type 'NonSendableKlass' does not conform to the 'Sendable' protocol}}
NonSendableKlass()
}
}
4 changes: 2 additions & 2 deletions test/IDE/complete_cache_notrecommended.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func testSyncMember(obj: MyActor) -> Int {
// MEMBER_IN_SYNC-DAG: Decl[InstanceMethod]/Super/IsSystem: preconditionIsolated({#(message): String#})[#Void#]; name=preconditionIsolated(:)
// MEMBER_IN_SYNC-DAG: Decl[InstanceMethod]/Super/IsSystem: assertIsolated()[#Void#]; name=assertIsolated()
// MEMBER_IN_SYNC-DAG: Decl[InstanceMethod]/Super/IsSystem: assertIsolated({#(message): String#})[#Void#]; name=assertIsolated(:)
// MEMBER_IN_SYNC-DAG: Decl[InstanceMethod]/Super/IsSystem: assumeIsolated({#(operation): (isolated MyActor) throws -> T##(isolated MyActor) throws -> T#})[' rethrows'][#T#]; name=assumeIsolated(:)
// MEMBER_IN_SYNC-DAG: Decl[InstanceMethod]/Super/IsSystem: assumeIsolated({#(operation): (isolated MyActor) throws -> Sendable##(isolated MyActor) throws -> Sendable#})[' rethrows'][#Sendable#]; name=assumeIsolated(:)
}

func testSyncMember(obj: MyActor) async -> Int {
Expand All @@ -55,7 +55,7 @@ func testSyncMember(obj: MyActor) async -> Int {
// MEMBER_IN_ASYNC-DAG: Decl[InstanceMethod]/Super/IsSystem: preconditionIsolated({#(message): String#})[#Void#]; name=preconditionIsolated(:)
// MEMBER_IN_ASYNC-DAG: Decl[InstanceMethod]/Super/IsSystem: assertIsolated()[#Void#]; name=assertIsolated()
// MEMBER_IN_ASYNC-DAG: Decl[InstanceMethod]/Super/IsSystem: assertIsolated({#(message): String#})[#Void#]; name=assertIsolated(:)
// MEMBER_IN_ASYNC-DAG: Decl[InstanceMethod]/Super/IsSystem: assumeIsolated({#(operation): (isolated MyActor) throws -> T##(isolated MyActor) throws -> T#})[' rethrows'][#T#]; name=assumeIsolated(:)
// MEMBER_IN_ASYNC-DAG: Decl[InstanceMethod]/Super/IsSystem: assumeIsolated({#(operation): (isolated MyActor) throws -> Sendable##(isolated MyActor) throws -> Sendable#})[' rethrows'][#Sendable#]; name=assumeIsolated(:)
}

// RUN: %empty-directory(%t)
Expand Down