1- // RUN: %target-swift-frontend -parse-as-library -emit-sil %s | %FileCheck %s
1+ // RUN: %target-swift-frontend -parse-as-library -emit-sil %s -module-name test | %FileCheck %s
22
33// REQUIRES: concurrency
44
@@ -9,16 +9,18 @@ nonisolated(nonsending) func nonisolatedNonsending() async {
99
1010func take( iso: ( any Actor ) ? ) { }
1111
12+ func takeDefaulted( iso: isolated ( any Actor ) ? = #isolation) { }
13+
1214// CHECK-LABEL: // nonisolatedNonsending()
1315// CHECK-NEXT: // Isolation: caller_isolation_inheriting
14- // CHECK-NEXT: sil hidden @$s39isolated_nonsending_isolation_macro_sil21nonisolatedNonsendingyyYaF : $@convention(thin) @async (@sil_isolated @sil_implicit_leading_param @guaranteed Optional<any Actor>) -> () {
16+ // CHECK-NEXT: sil hidden @$s4test21nonisolatedNonsendingyyYaF : $@convention(thin) @async (@sil_isolated @sil_implicit_leading_param @guaranteed Optional<any Actor>) -> () {
1517// CHECK: bb0([[ACTOR:%.*]] : $Optional<any Actor>):
1618// CHECK: retain_value [[ACTOR]]
1719// CHECK: debug_value [[ACTOR]], let, name "iso"
18- // CHECK: [[FUNC:%.*]] = function_ref @$s39isolated_nonsending_isolation_macro_sil4take3isoyScA_pSg_tF : $@convention(thin) (@guaranteed Optional<any Actor>) -> ()
20+ // CHECK: [[FUNC:%.*]] = function_ref @$s4test4take3isoyScA_pSg_tF : $@convention(thin) (@guaranteed Optional<any Actor>) -> ()
1921// CHECK: apply [[FUNC]]([[ACTOR]]) : $@convention(thin) (@guaranteed Optional<any Actor>) -> ()
2022// CHECK: release_value [[ACTOR]]
21- // CHECK: } // end sil function '$s39isolated_nonsending_isolation_macro_sil21nonisolatedNonsendingyyYaF '
23+ // CHECK: } // end sil function '$s4test21nonisolatedNonsendingyyYaF '
2224
2325// Check that we emit #isolation correctly in closures.
2426// CHECK-LABEL: // closure #1 in containsClosure()
@@ -33,6 +35,49 @@ func containsClosure() {
3335 }
3436}
3537
38+ // Check that we capture variables as necessary to emit #isolation
39+ // correctly in defer bodies.
40+ func deferWithIsolatedParam( _ iso: isolated ( any Actor ) ) {
41+ defer {
42+ take ( iso: #isolation)
43+ }
44+ do { }
45+ }
46+ // CHECK-LABEL: sil hidden @$s4test22deferWithIsolatedParamyyScA_pYiF :
47+ // CHECK: bb0(%0 : $any Actor)
48+ // CHECK: [[DEFER:%.*]] = function_ref @$s4test22deferWithIsolatedParamyyScA_pYiF6$deferL_yyF :
49+ // CHECK-NEXT: apply [[DEFER]](%0)
50+
51+ // CHECK-LABEL: sil private @$s4test22deferWithIsolatedParamyyScA_pYiF6$deferL_yyF :
52+ // CHECK: bb0(%0 : @closureCapture $any Actor):
53+ // CHECK: [[T0:%.*]] = enum $Optional<any Actor>, #Optional.some!enumelt, %0
54+ // CHECK: [[FN:%.*]] = function_ref @$s4test4take3isoyScA_pSg_tF :
55+ // CHECK-NEXT: apply [[FN]]([[T0]])
56+
57+ // Check that that happens even with uses in caller-side default
58+ // arguments, which capture analysis was not previously walking into.
59+ func deferWithIsolatedParam_defaultedUse( _ iso: isolated ( any Actor ) ) {
60+ defer {
61+ takeDefaulted ( )
62+ }
63+ do { }
64+ }
65+
66+ // CHECK-LABEL: sil hidden @$s4test35deferWithIsolatedParam_defaultedUseyyScA_pYiF :
67+ // CHECK: bb0(%0 : $any Actor):
68+ // CHECK: [[DEFER:%.*]] = function_ref @$s4test35deferWithIsolatedParam_defaultedUseyyScA_pYiF6$deferL_yyF :
69+ // CHECK-NEXT: apply [[DEFER]](%0)
70+
71+ // CHECK-LABEL: sil private @$s4test35deferWithIsolatedParam_defaultedUseyyScA_pYiF6$deferL_yyF :
72+ // CHECK: bb0(%0 : @closureCapture $any Actor):
73+ // CHECK: [[T0:%.*]] = enum $Optional<any Actor>, #Optional.some!enumelt, %0
74+ // CHECK: [[FN:%.*]] = function_ref @$s4test13takeDefaulted3isoyScA_pSgYi_tF :
75+ // CHECK-NEXT: apply [[FN]]([[T0]])
76+
77+ // TODO: we can't currently call nonisolated(nonsending) functions in
78+ // defer bodies because they have to be async, but that should be
79+ // tested here as well.
80+
3681// Check that we emit #isolation correctly in defer bodies.
3782nonisolated ( nonsending)
3883func hasDefer( ) async {
@@ -41,7 +86,7 @@ func hasDefer() async {
4186 }
4287 do { }
4388}
44- // CHECK-LABEL: sil hidden @$s39isolated_nonsending_isolation_macro_sil8hasDeferyyYaF :
89+ // CHECK-LABEL: sil hidden @$s4test8hasDeferyyYaF :
4590// CHECK: bb0(%0 : $Optional<any Actor>):
4691// CHECK: // function_ref $defer
4792// CHECK-NEXT: [[DEFER:%.*]] = function_ref
@@ -66,7 +111,7 @@ func hasNestedDefer() async {
66111 do { }
67112}
68113
69- // CHECK-LABEL: sil hidden @$s39isolated_nonsending_isolation_macro_sil14hasNestedDeferyyYaF :
114+ // CHECK-LABEL: sil hidden @$s4test14hasNestedDeferyyYaF :
70115// CHECK: bb0(%0 : $Optional<any Actor>):
71116// CHECK: // function_ref $defer #1 () in hasNestedDefer()
72117// CHECK-NEXT: [[DEFER:%.*]] = function_ref
0 commit comments