Skip to content

Commit 74d7a25

Browse files
committed
[Concurrency] Add more tests for optional isolated parameters.
1 parent 0db14c9 commit 74d7a25

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-build-swift -Xfrontend -disable-availability-checking -enable-experimental-feature OptionalIsolatedParameters %s -o %t/main
3+
// RUN: %target-codesign %t/main
4+
// RUN: %target-run %t/main | %FileCheck %s
5+
6+
// REQUIRES: executable_test
7+
// REQUIRES: concurrency
8+
9+
actor MyActor: CustomStringConvertible {
10+
let description = "MyActor"
11+
}
12+
13+
func optionalIsolated(to actor: isolated (any Actor)?) {
14+
actor?.assertIsolated()
15+
if let actor {
16+
print("isolated to \(actor)")
17+
} else {
18+
print("nonisolated")
19+
}
20+
}
21+
22+
// CHECK: nonisolated
23+
// CHECK: isolated to Swift.MainActor
24+
// CHECK: isolated to MyActor
25+
optionalIsolated(to: nil)
26+
await optionalIsolated(to: MainActor.shared)
27+
await optionalIsolated(to: MyActor())

test/SILGen/isolated_parameters.swift

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-frontend -emit-silgen %s -module-name test -swift-version 5 | %FileCheck %s
1+
// RUN: %target-swift-frontend -emit-silgen -enable-experimental-feature OptionalIsolatedParameters %s -module-name test -swift-version 5 | %FileCheck %s
22
// REQUIRES: concurrency
33

44
@available(SwiftStdlib 5.1, *)
@@ -34,3 +34,34 @@ public func testIsolatedExistential(_ a: isolated Actor) async {
3434
// CHECK: hop_to_executor [[ACTOR_BORROW]] : $any Actor
3535
// CHECK: return
3636
}
37+
38+
@available(SwiftStdlib 5.1, *)
39+
nonisolated func suspend() async {}
40+
41+
// CHECK-LABEL: sil{{.*}} [ossa] @$s4test0A16OptionalIsolatedyyAA1ACSgYiYaF
42+
// CHECK: bb0([[ACTOR:%.*]] : @guaranteed $Optional<A>)
43+
@available(SwiftStdlib 5.1, *)
44+
public func testOptionalIsolated(_ a: isolated A?) async {
45+
await suspend()
46+
// CHECK: [[ACTOR_COPY:%.*]] = copy_value [[ACTOR]] : $Optional<A>
47+
// CHECK: [[ACTOR_BORROW:%.*]] = begin_borrow [[ACTOR_COPY]] : $Optional<A>
48+
// CHECK: hop_to_executor [[ACTOR_BORROW]] : $Optional<A>
49+
// CHECK: [[SUSPEND:%.*]] = function_ref @$s4test7suspendyyYaF : $@convention(thin) @async () -> ()
50+
// CHECK: apply [[SUSPEND]]() : $@convention(thin) @async () -> ()
51+
// CHECK: hop_to_executor [[ACTOR_BORROW]] : $Optional<A>
52+
// CHECK: return
53+
}
54+
55+
// CHECK-LABEL: sil{{.*}} [ossa] @$s4test0A27OptionalIsolatedExistentialyyScA_pSgYiYaF
56+
// CHECK: bb0([[ACTOR:%.*]] : @guaranteed $Optional<any Actor>)
57+
@available(SwiftStdlib 5.1, *)
58+
public func testOptionalIsolatedExistential(_ a: isolated (any Actor)?) async {
59+
await suspend()
60+
// CHECK: [[ACTOR_COPY:%.*]] = copy_value [[ACTOR]] : $Optional<any Actor>
61+
// CHECK: [[ACTOR_BORROW:%.*]] = begin_borrow [[ACTOR_COPY]] : $Optional<any Actor>
62+
// CHECK: hop_to_executor [[ACTOR_BORROW]] : $Optional<any Actor>
63+
// CHECK: [[SUSPEND:%.*]] = function_ref @$s4test7suspendyyYaF : $@convention(thin) @async () -> ()
64+
// CHECK: apply [[SUSPEND]]() : $@convention(thin) @async () -> ()
65+
// CHECK: hop_to_executor [[ACTOR_BORROW]] : $Optional<any Actor>
66+
// CHECK: return
67+
}

test/SILOptimizer/lower_hop_to_actor.sil

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,3 +260,33 @@ bb0(%0 : @guaranteed $Optional<MyActor>):
260260
%r = tuple ()
261261
return %r : $()
262262
}
263+
264+
// CHECK-LABEL: sil [ossa] @optional_existential_actor :
265+
sil [ossa] @optional_existential_actor : $@async (@guaranteed Optional<any Actor>) -> () {
266+
bb0(%0 : @guaranteed $Optional<any Actor>):
267+
// CHECK: bb0(%0 : @guaranteed $Optional<any Actor>):
268+
// CHECK-NEXT: switch_enum %0 : $Optional<any Actor>, case #Optional.some!enumelt: bb1, default bb2
269+
270+
// CHECK: bb1([[ACTOR:%.*]] : @guaranteed $any Actor):
271+
// CHECK-NEXT: [[OPENED:%.*]] = open_existential_ref [[ACTOR]] : $any Actor to $@opened("[[ID:.*]]", any Actor) Self
272+
// CHECK-NEXT: [[WITNESS:%.*]] = witness_method $@opened("[[ID]]", any Actor) Self, #Actor.unownedExecutor!getter : <Self where Self : Actor> (Self) -> () -> UnownedSerialExecutor
273+
// CHECK-NEXT: [[SERIAL_EXEC:%.*]] = apply [[WITNESS]]<@opened("[[ID]]", any Actor) Self>([[OPENED]])
274+
// CHECK-NEXT: [[EXECUTOR:%.*]] = struct_extract [[SERIAL_EXEC]] : $UnownedSerialExecutor, #UnownedSerialExecutor.executor
275+
// CHECK-NEXT: [[SOME:%.*]] = enum $Optional<Builtin.Executor>, #Optional.some!enumelt, [[EXECUTOR]] : $Builtin.Executor
276+
// CHECK-NEXT: br bb3([[SOME]] : $Optional<Builtin.Executor>)
277+
278+
// CHECK: bb2:
279+
// CHECK-NEXT: [[NONE:%.*]] = enum $Optional<Builtin.Executor>, #Optional.none!enumelt
280+
// CHECK-NEXT: br bb3([[NONE]] : $Optional<Builtin.Executor>)
281+
282+
// CHECK: bb3([[OPT_EXEC:%.*]] : $Optional<Builtin.Executor>):
283+
// CHECK-NEXT: [[DEPEND:%.*]] = mark_dependence [[OPT_EXEC]] : $Optional<Builtin.Executor> on %0 : $Optional<any Actor>
284+
// CHECK-NEXT: hop_to_executor [[DEPEND]] : $Optional<Builtin.Executor>
285+
// CHECK-NEXT: hop_to_executor [[DEPEND]] : $Optional<Builtin.Executor>
286+
// CHECK-NEXT: [[RET:%.*]] = tuple ()
287+
// CHECK-NEXT: return [[RET]] : $()
288+
hop_to_executor %0 : $Optional<any Actor>
289+
hop_to_executor %0 : $Optional<any Actor>
290+
%r = tuple ()
291+
return %r : $()
292+
}

0 commit comments

Comments
 (0)