Skip to content

Commit 302b010

Browse files
authored
Merge pull request #72763 from gottesmm/pr-d6b1f7e0bfc91f3d28ecf7bd100f45f41533688a
[region-isolation] Require T in assumeIsolated<T> to be Sendable.
2 parents 51f6e66 + 58c91cf commit 302b010

File tree

5 files changed

+72
-10
lines changed

5 files changed

+72
-10
lines changed

stdlib/public/Concurrency/ExecutorAssertions.swift

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -342,12 +342,10 @@ extension Actor {
342342
/// - Returns: the return value of the `operation`
343343
/// - Throws: rethrows the `Error` thrown by the operation if it threw
344344
@available(SwiftStdlib 5.1, *)
345-
#if !$Embedded
346-
@backDeployed(before: SwiftStdlib 5.9)
347-
#endif
345+
@_alwaysEmitIntoClient
348346
@_unavailableFromAsync(message: "express the closure as an explicit function declared on the specified 'actor' instead")
349347
@_unavailableInEmbedded
350-
public nonisolated func assumeIsolated<T>(
348+
public nonisolated func assumeIsolated<T : Sendable>(
351349
_ operation: (isolated Self) throws -> T,
352350
file: StaticString = #fileID, line: UInt = #line
353351
) rethrows -> T {
@@ -373,6 +371,17 @@ extension Actor {
373371
fatalError("unsupported compiler")
374372
#endif
375373
}
374+
375+
@available(SwiftStdlib 5.9, *)
376+
@usableFromInline
377+
@_unavailableInEmbedded
378+
@_silgen_name("$sScAsE14assumeIsolated_4file4lineqd__qd__xYiKXE_s12StaticStringVSutKlF")
379+
internal nonisolated func __abi__assumeIsolated<T : Sendable>(
380+
_ operation: (isolated Self) throws -> T,
381+
_ file: StaticString, _ line: UInt
382+
) rethrows -> T {
383+
try assumeIsolated(operation, file: file, line: line)
384+
}
376385
}
377386

378387
#endif // not SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY

stdlib/public/Concurrency/MainActor.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,12 @@ extension MainActor {
137137
/// - Returns: the return value of the `operation`
138138
/// - Throws: rethrows the `Error` thrown by the operation if it threw
139139
@available(SwiftStdlib 5.1, *)
140-
@backDeployed(before: SwiftStdlib 5.9)
140+
@_alwaysEmitIntoClient
141141
@_unavailableFromAsync(message: "await the call to the @MainActor closure directly")
142-
public static func assumeIsolated<T>(
142+
public static func assumeIsolated<T : Sendable>(
143143
_ operation: @MainActor () throws -> T,
144144
file: StaticString = #fileID, line: UInt = #line
145145
) rethrows -> T {
146-
147146
typealias YesActor = @MainActor () throws -> T
148147
typealias NoActor = () throws -> T
149148

@@ -166,5 +165,15 @@ extension MainActor {
166165
fatalError("unsupported compiler")
167166
#endif
168167
}
168+
169+
@available(SwiftStdlib 5.9, *)
170+
@usableFromInline
171+
@_silgen_name("$sScM14assumeIsolated_4file4linexxyKScMYcXE_s12StaticStringVSutKlFZ")
172+
internal static func __abi__assumeIsolated<T : Sendable>(
173+
_ operation: @MainActor () throws -> T,
174+
_ file: StaticString, _ line: UInt
175+
) rethrows -> T {
176+
try assumeIsolated(operation, file: file, line: line)
177+
}
169178
}
170179
#endif

stdlib/public/Distributed/DistributedAssertions.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ extension DistributedActor {
153153
/// - Throws: rethrows the `Error` thrown by the operation if it threw
154154
@available(SwiftStdlib 5.9, *)
155155
@_unavailableFromAsync(message: "express the closure as an explicit function declared on the specified 'distributed actor' instead")
156-
public nonisolated func assumeIsolated<T>(
156+
@_alwaysEmitIntoClient
157+
public nonisolated func assumeIsolated<T : Sendable>(
157158
_ operation: (isolated Self) throws -> T,
158159
file: StaticString = #fileID, line: UInt = #line
159160
) rethrows -> T {
@@ -181,6 +182,16 @@ extension DistributedActor {
181182
fatalError("unsupported compiler")
182183
#endif
183184
}
185+
186+
@available(SwiftStdlib 5.9, *)
187+
@usableFromInline
188+
@_silgen_name("$s11Distributed0A5ActorPAAE14assumeIsolated_4file4lineqd__qd__xYiKXE_s12StaticStringVSutKlF")
189+
internal nonisolated func __abi__assumeIsolated<T : Sendable>(
190+
_ operation: (isolated Self) throws -> T,
191+
_ file: StaticString, _ line: UInt
192+
) rethrows -> T {
193+
try assumeIsolated(operation, file: file, line: line)
194+
}
184195
}
185196

186197
/// WARNING: This function will CRASH rather than return `false` in new runtimes

test/Concurrency/assumeIsolated.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// RUN: %target-build-swift -swift-version 5 %s -enable-upcoming-feature RegionBasedIsolation -strict-concurrency=complete -enable-experimental-feature TransferringArgsAndResults -Xfrontend -verify
2+
3+
// REQUIRES: concurrency
4+
// REQUIRES: OS=macosx
5+
6+
class NonSendableKlass {} // expected-note 3{{class 'NonSendableKlass' does not conform to the 'Sendable' protocol}}
7+
8+
@available(macOS 10.15, *)
9+
actor MyActor {
10+
var x = NonSendableKlass()
11+
12+
nonisolated func doSomething() -> NonSendableKlass {
13+
return self.assumeIsolated { isolatedSelf in // expected-warning {{type 'NonSendableKlass' does not conform to the 'Sendable' protocol}}
14+
let x: NonSendableKlass = isolatedSelf.x
15+
return x
16+
}
17+
}
18+
19+
nonisolated func doSomething2() -> NonSendableKlass {
20+
let r: NonSendableKlass = assumeIsolated { isolatedSelf in // expected-warning {{type 'NonSendableKlass' does not conform to the 'Sendable' protocol}}
21+
let x: NonSendableKlass = isolatedSelf.x
22+
return x
23+
}
24+
return r
25+
}
26+
}
27+
28+
@available(macOS 10.15, *)
29+
nonisolated func mainActorAssumeIsolated() -> NonSendableKlass {
30+
return MainActor.assumeIsolated { // expected-warning {{type 'NonSendableKlass' does not conform to the 'Sendable' protocol}}
31+
NonSendableKlass()
32+
}
33+
}

test/IDE/complete_cache_notrecommended.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func testSyncMember(obj: MyActor) -> Int {
4141
// MEMBER_IN_SYNC-DAG: Decl[InstanceMethod]/Super/IsSystem: preconditionIsolated({#(message): String#})[#Void#]; name=preconditionIsolated(:)
4242
// MEMBER_IN_SYNC-DAG: Decl[InstanceMethod]/Super/IsSystem: assertIsolated()[#Void#]; name=assertIsolated()
4343
// MEMBER_IN_SYNC-DAG: Decl[InstanceMethod]/Super/IsSystem: assertIsolated({#(message): String#})[#Void#]; name=assertIsolated(:)
44-
// MEMBER_IN_SYNC-DAG: Decl[InstanceMethod]/Super/IsSystem: assumeIsolated({#(operation): (isolated MyActor) throws -> T##(isolated MyActor) throws -> T#})[' rethrows'][#T#]; name=assumeIsolated(:)
44+
// MEMBER_IN_SYNC-DAG: Decl[InstanceMethod]/Super/IsSystem: assumeIsolated({#(operation): (isolated MyActor) throws -> Sendable##(isolated MyActor) throws -> Sendable#})[' rethrows'][#Sendable#]; name=assumeIsolated(:)
4545
}
4646

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

6161
// RUN: %empty-directory(%t)

0 commit comments

Comments
 (0)