|
| 1 | +// RUN: %target-swift-frontend %s -import-objc-header %S/Inputs/regionbasedisolation.h -verify -c -swift-version 6 |
| 2 | +// RUN: %target-swift-frontend %s -import-objc-header %S/Inputs/regionbasedisolation.h -emit-silgen -swift-version 6 | %FileCheck %s |
| 3 | + |
| 4 | +// REQUIRES: objc_interop |
| 5 | + |
| 6 | +extension ObjCObject { |
| 7 | + // CHECK-LABEL: sil hidden [ossa] @$sSo10ObjCObjectC20regionbasedisolationE11sendObjectsSaySo8NSObjectCGyYaKF : $@convention(method) @async (@guaranteed ObjCObject) -> (@sil_sending @owned Array<NSObject>, @error any Error) { |
| 8 | + // CHECK: bb0([[SELF:%.*]] : @guaranteed $ObjCObject): |
| 9 | + |
| 10 | + // Our result. |
| 11 | + // CHECK: [[RESULT:%.*]] = alloc_stack $Array<NSObject> |
| 12 | + |
| 13 | + // Our method. |
| 14 | + // CHECK: [[METHOD:%.*]] = objc_method [[SELF]], #ObjCObject.loadObjects!foreign : (ObjCObject) -> () async throws -> [NSObject], $@convention(objc_method) (@convention(block) @Sendable (Optional<NSArray>, Optional<NSError>) -> (), ObjCObject) -> () |
| 15 | + |
| 16 | + // Begin setting up the unsafe continuation for our method. Importantly note |
| 17 | + // that [[UNSAFE_CONT]] is Sendable, so we lose any connection from the |
| 18 | + // continuation addr to any uses of the UnsafeContinuation. |
| 19 | + // |
| 20 | + // CHECK: [[CONT:%.*]] = get_async_continuation_addr [throws] Array<NSObject>, [[RESULT]] |
| 21 | + // CHECK: [[UNSAFE_CONT:%.*]] = struct $UnsafeContinuation<Array<NSObject>, any Error> ([[CONT]]) |
| 22 | + |
| 23 | + // Then prepare the block storage. |
| 24 | + // CHECK: [[BLOCK_STORAGE:%.*]] = alloc_stack $@block_storage Any |
| 25 | + // CHECK: [[PROJECT_BLOCK_STORAGE:%.*]] = project_block_storage [[BLOCK_STORAGE]] |
| 26 | + // CHECK: [[EXISTENTIAL_BLOCK_STORAGE:%.*]] = init_existential_addr [[PROJECT_BLOCK_STORAGE]] |
| 27 | + |
| 28 | + // Then create a checked continuation from the unsafe continuation. |
| 29 | + // |
| 30 | + // CHECK: [[CREATE_CHECKED_CONT:%.*]] = function_ref @$ss34_createCheckedThrowingContinuationyScCyxs5Error_pGSccyxsAB_pGnlF : $@convention(thin) <τ_0_0> (UnsafeContinuation<τ_0_0, any Error>) -> @out CheckedContinuation<τ_0_0, any Error> |
| 31 | + // CHECK: [[CHECKED_CONT:%.*]] = alloc_stack $CheckedContinuation<Array<NSObject>, any Error> |
| 32 | + // CHECK: apply [[CREATE_CHECKED_CONT]]<Array<NSObject>>([[CHECKED_CONT]], [[UNSAFE_CONT]]) |
| 33 | + |
| 34 | + // Then place the checked continuation into the block storage and perform a |
| 35 | + // merge_isolation_region in between the block storage and the result to |
| 36 | + // propagate that the result and the block storage are apart of the same |
| 37 | + // region despite the UnsafeContinuation blocking the relation in between |
| 38 | + // them. |
| 39 | + // |
| 40 | + // CHECK: copy_addr [take] [[CHECKED_CONT]] to [init] [[EXISTENTIAL_BLOCK_STORAGE]] |
| 41 | + // CHECK: merge_isolation_region [[BLOCK_STORAGE]], [[RESULT]] |
| 42 | + |
| 43 | + // Then create the actual block. NOTE: Since the block is @Sendable, the block |
| 44 | + // does not propagate regions. |
| 45 | + // |
| 46 | + // CHECK: [[COMPLETION_HANDLER_BLOCK:%.*]] = function_ref @$sSo7NSArrayCSgSo7NSErrorCSgIeyBhyy_SaySo8NSObjectCGTz_ : $@convention(c) @Sendable (@inout_aliasable @block_storage Any, Optional<NSArray>, Optional<NSError>) -> () |
| 47 | + // CHECK: [[COMPLETION_BLOCK:%.*]] = init_block_storage_header [[BLOCK_STORAGE]], invoke [[COMPLETION_HANDLER_BLOCK]] |
| 48 | + // |
| 49 | + // Since the block is @Sendable, it does not propagate the connection in |
| 50 | + // between self and the block storage when we just call the method. Thus we |
| 51 | + // need to perform a merge_isolation_region to communicate that the block |
| 52 | + // storage and self are part of the same region. |
| 53 | + // |
| 54 | + // CHECK: merge_isolation_region [[SELF]], [[BLOCK_STORAGE]] |
| 55 | + // |
| 56 | + // Then call the method. |
| 57 | + // CHECK: apply [[METHOD]]([[COMPLETION_BLOCK]], [[SELF]]) |
| 58 | + // CHECK: } // end sil function '$sSo10ObjCObjectC20regionbasedisolationE11sendObjectsSaySo8NSObjectCGyYaKF' |
| 59 | + |
| 60 | + func sendObjects() async throws -> sending [NSObject] { |
| 61 | + // We emit an error since loadObjects just returns an [NSObject], not a |
| 62 | + // sending [NSObject]. |
| 63 | + try await loadObjects() |
| 64 | + } // expected-error {{task or actor isolated value cannot be sent}} |
| 65 | + |
| 66 | + // Check if we do not mark the block as NS_SWIFT_SENDABLE |
| 67 | + // |
| 68 | + // CHECK-LABEL: sil hidden [ossa] @$sSo10ObjCObjectC20regionbasedisolationE12sendObjects2SaySo8NSObjectCGyYaKF : $@convention(method) @async (@guaranteed ObjCObject) -> (@sil_sending @owned Array<NSObject>, @error any Error) { |
| 69 | + // CHECK: bb0([[SELF:%.*]] : @guaranteed $ObjCObject): |
| 70 | + |
| 71 | + // Our result. |
| 72 | + // CHECK: [[RESULT:%.*]] = alloc_stack $Array<NSObject> |
| 73 | + |
| 74 | + // Our method. |
| 75 | + // CHECK: [[METHOD:%.*]] = objc_method [[SELF]], #ObjCObject.loadObjects2!foreign : (ObjCObject) -> () async throws -> [NSObject], $@convention(objc_method) (@convention(block) @Sendable (Optional<NSArray>, Optional<NSError>) -> (), ObjCObject) -> () |
| 76 | + |
| 77 | + // Begin setting up the unsafe continuation for our method. Importantly note |
| 78 | + // that [[UNSAFE_CONT]] is Sendable, so we lose any connection from the |
| 79 | + // continuation addr to any uses of the UnsafeContinuation. |
| 80 | + // |
| 81 | + // CHECK: [[CONT:%.*]] = get_async_continuation_addr [throws] Array<NSObject>, [[RESULT]] |
| 82 | + // CHECK: [[UNSAFE_CONT:%.*]] = struct $UnsafeContinuation<Array<NSObject>, any Error> ([[CONT]]) |
| 83 | + |
| 84 | + // Then prepare the block storage. |
| 85 | + // CHECK: [[BLOCK_STORAGE:%.*]] = alloc_stack $@block_storage Any |
| 86 | + // CHECK: [[PROJECT_BLOCK_STORAGE:%.*]] = project_block_storage [[BLOCK_STORAGE]] |
| 87 | + // CHECK: [[EXISTENTIAL_BLOCK_STORAGE:%.*]] = init_existential_addr [[PROJECT_BLOCK_STORAGE]] |
| 88 | + |
| 89 | + // Then create a checked continuation from the unsafe continuation. |
| 90 | + // |
| 91 | + // CHECK: [[CREATE_CHECKED_CONT:%.*]] = function_ref @$ss34_createCheckedThrowingContinuationyScCyxs5Error_pGSccyxsAB_pGnlF : $@convention(thin) <τ_0_0> (UnsafeContinuation<τ_0_0, any Error>) -> @out CheckedContinuation<τ_0_0, any Error> |
| 92 | + // CHECK: [[CHECKED_CONT:%.*]] = alloc_stack $CheckedContinuation<Array<NSObject>, any Error> |
| 93 | + // CHECK: apply [[CREATE_CHECKED_CONT]]<Array<NSObject>>([[CHECKED_CONT]], [[UNSAFE_CONT]]) |
| 94 | + |
| 95 | + // Then place the checked continuation into the block storage and perform a |
| 96 | + // merge_isolation_region in between the block storage and the result to |
| 97 | + // propagate that the result and the block storage are apart of the same |
| 98 | + // region despite the UnsafeContinuation blocking the relation in between |
| 99 | + // them. |
| 100 | + // |
| 101 | + // CHECK: copy_addr [take] [[CHECKED_CONT]] to [init] [[EXISTENTIAL_BLOCK_STORAGE]] |
| 102 | + // CHECK: merge_isolation_region [[BLOCK_STORAGE]], [[RESULT]] |
| 103 | + |
| 104 | + // Then create the actual block. NOTE: Since the block is @Sendable, the block |
| 105 | + // does not propagate regions. |
| 106 | + // |
| 107 | + // CHECK: [[COMPLETION_HANDLER_BLOCK:%.*]] = function_ref @$sSo7NSArrayCSgSo7NSErrorCSgIeyBhyy_SaySo8NSObjectCGTz_ : $@convention(c) @Sendable (@inout_aliasable @block_storage Any, Optional<NSArray>, Optional<NSError>) -> () |
| 108 | + // CHECK: [[COMPLETION_BLOCK:%.*]] = init_block_storage_header [[BLOCK_STORAGE]], invoke [[COMPLETION_HANDLER_BLOCK]] |
| 109 | + // |
| 110 | + // Since the block is @Sendable, it does not propagate the connection in |
| 111 | + // between self and the block storage when we just call the method. Thus we |
| 112 | + // need to perform a merge_isolation_region to communicate that the block |
| 113 | + // storage and self are part of the same region. |
| 114 | + // |
| 115 | + // CHECK: merge_isolation_region [[SELF]], [[BLOCK_STORAGE]] |
| 116 | + // |
| 117 | + // Then call the method. |
| 118 | + // CHECK: apply [[METHOD]]([[COMPLETION_BLOCK]], [[SELF]]) |
| 119 | + // CHECK: } // end sil function '$sSo10ObjCObjectC20regionbasedisolationE12sendObjects2SaySo8NSObjectCGyYaKF' |
| 120 | + func sendObjects2() async throws -> sending [NSObject] { |
| 121 | + // We emit an error since loadObjects just returns an [NSObject], not a |
| 122 | + // sending [NSObject]. |
| 123 | + try await loadObjects2() |
| 124 | + } // expected-error {{task or actor isolated value cannot be sent}} |
| 125 | +} |
0 commit comments