|
| 1 | +// RUN: %target-swift-frontend %s -swift-version 6 -verify -verify-additional-prefix disabled- -c |
| 2 | +// RUN: %target-swift-frontend %s -swift-version 6 -verify -enable-experimental-feature NonIsolatedAsyncInheritsIsolationFromContext -verify-additional-prefix enable- -c -verify-additional-prefix enabled- |
| 3 | + |
| 4 | +// REQUIRES: asserts |
| 5 | +// REQUIRES: concurrency |
| 6 | +// REQUIRES: swift_feature_NonIsolatedAsyncInheritsIsolationFromContext |
| 7 | + |
| 8 | +// This test checks and validates that when |
| 9 | +// NonIsolatedAsyncInheritsIsolationFromContext is enabled, we emit the |
| 10 | +// appropriate diagnostics. It also runs with the mode off so we can validate |
| 11 | +// and compare locally against the normal errors. |
| 12 | + |
| 13 | +////////////////// |
| 14 | +// Declarations // |
| 15 | +////////////////// |
| 16 | + |
| 17 | +class NonSendableKlass {} |
| 18 | + |
| 19 | +func unspecifiedSyncUse<T>(_ t: T) {} |
| 20 | +func unspecifiedAsyncUse<T>(_ t: T) async {} |
| 21 | +nonisolated func nonisolatedSyncUse<T>(_ t: T) {} |
| 22 | +nonisolated func nonisolatedAsyncUse<T>(_ t: T) async {} |
| 23 | + |
| 24 | +func unspecifiedSyncUseWithResult<T>(_ t: T) -> T { t } |
| 25 | +func unspecifiedAsyncUseWithResult<T>(_ t: T) async -> T { t } |
| 26 | +nonisolated func nonisolatedSyncUseWithResult<T>(_ t: T) -> T { t } |
| 27 | +nonisolated func nonisolatedAsyncUseWithResult<T>(_ t: T) async -> T { t } |
| 28 | + |
| 29 | +func unspecifiedSyncResult() -> NonSendableKlass { fatalError() } |
| 30 | +func unspecifiedAsyncResult() async -> NonSendableKlass { fatalError() } |
| 31 | +nonisolated func nonisolatedSyncResult() -> NonSendableKlass { fatalError() } |
| 32 | +nonisolated func nonisolatedAsyncResult() async -> NonSendableKlass { fatalError() } |
| 33 | + |
| 34 | +@MainActor func sendToMain<T>(_ t: T) async {} |
| 35 | + |
| 36 | +actor Custom { |
| 37 | +} |
| 38 | + |
| 39 | +@globalActor |
| 40 | +struct CustomActor { |
| 41 | + static var shared: Custom { |
| 42 | + return Custom() |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +@CustomActor func sendToCustom<T>(_ t: T) async {} |
| 47 | + |
| 48 | +/////////// |
| 49 | +// Tests // |
| 50 | +/////////// |
| 51 | + |
| 52 | +actor ActorTest { |
| 53 | + var ns = NonSendableKlass() |
| 54 | + |
| 55 | + func callNonIsolatedWithParam() async { |
| 56 | + unspecifiedSyncUse(ns) |
| 57 | + await unspecifiedAsyncUse(ns) // expected-disabled-error {{sending 'self.ns' risks causing data races}} |
| 58 | + // expected-disabled-note @-1 {{sending 'self'-isolated 'self.ns' to nonisolated global function 'unspecifiedAsyncUse' risks causing data races between nonisolated and 'self'-isolated uses}} |
| 59 | + nonisolatedSyncUse(ns) |
| 60 | + await nonisolatedAsyncUse(ns) // expected-disabled-error {{sending 'self.ns' risks causing data races}} |
| 61 | + // expected-disabled-note @-1 {{sending 'self'-isolated 'self.ns' to nonisolated global function 'nonisolatedAsyncUse' risks causing data races between nonisolated and 'self'-isolated uses}} |
| 62 | + } |
| 63 | + |
| 64 | + func callNonIsolatedWithResult() async { |
| 65 | + let x1 = unspecifiedSyncUseWithResult(ns) |
| 66 | + await sendToMain(x1) // expected-error {{sending 'x1' risks causing data races}} |
| 67 | + // expected-note @-1 {{sending 'self'-isolated 'x1' to main actor-isolated global function 'sendToMain' risks causing data races between main actor-isolated and 'self'-isolated uses}} |
| 68 | + |
| 69 | + let x2 = await unspecifiedAsyncUseWithResult(ns) // expected-disabled-error {{sending 'self.ns' risks causing data races}} |
| 70 | + // expected-disabled-note @-1 {{sending 'self'-isolated 'self.ns' to nonisolated global function 'unspecifiedAsyncUseWithResult' risks causing data races between nonisolated and 'self'-isolated uses}} |
| 71 | + await sendToMain(x2) // expected-enabled-error {{sending 'x2' risks causing data races}} |
| 72 | + // expected-enabled-note @-1 {{sending 'self'-isolated 'x2' to main actor-isolated global function 'sendToMain' risks causing data races between main actor-isolated and 'self'-isolated uses}} |
| 73 | + |
| 74 | + let x3 = nonisolatedSyncUseWithResult(ns) |
| 75 | + await sendToMain(x3) // expected-error {{sending 'x3' risks causing data races}} |
| 76 | + // expected-note @-1 {{sending 'self'-isolated 'x3' to main actor-isolated global function 'sendToMain' risks causing data races between main actor-isolated and 'self'-isolated uses}} |
| 77 | + |
| 78 | + let x4 = await nonisolatedAsyncUseWithResult(ns) // expected-disabled-error {{sending 'self.ns' risks causing data races}} |
| 79 | + // expected-disabled-note @-1 {{sending 'self'-isolated 'self.ns' to nonisolated global function 'nonisolatedAsyncUseWithResult' risks causing data races between nonisolated and 'self'-isolated uses}} |
| 80 | + await sendToMain(x4) // expected-enabled-error {{sending 'x4' risks causing data races}} |
| 81 | + // expected-enabled-note @-1 {{sending 'self'-isolated 'x4' to main actor-isolated global function 'sendToMain' risks causing data races between main actor-isolated and 'self'-isolated uses}} |
| 82 | + } |
| 83 | + |
| 84 | + func callNonIsolatedWithResult2() async { |
| 85 | + let x1 = unspecifiedSyncResult() |
| 86 | + await sendToMain(x1) |
| 87 | + |
| 88 | + let x2 = await unspecifiedAsyncResult() |
| 89 | + await sendToMain(x2) // expected-enabled-error {{sending 'x2' risks causing data races}} |
| 90 | + // expected-enabled-note @-1 {{sending 'self'-isolated 'x2' to main actor-isolated global function 'sendToMain' risks causing data races between main actor-isolated and 'self'-isolated uses}} |
| 91 | + |
| 92 | + let x3 = nonisolatedSyncResult() |
| 93 | + await sendToMain(x3) |
| 94 | + |
| 95 | + let x4 = await nonisolatedAsyncResult() |
| 96 | + await sendToMain(x4) // expected-enabled-error {{sending 'x4' risks causing data races}} |
| 97 | + // expected-enabled-note @-1 {{sending 'self'-isolated 'x4' to main actor-isolated global function 'sendToMain' risks causing data races between main actor-isolated and 'self'-isolated uses}} |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +@MainActor |
| 102 | +class MainActorKlass { |
| 103 | + var ns = NonSendableKlass() |
| 104 | + |
| 105 | + func callNonIsolatedWithParam() async { |
| 106 | + unspecifiedSyncUse(ns) |
| 107 | + await unspecifiedAsyncUse(ns) // expected-disabled-error {{sending 'self.ns' risks causing data races}} |
| 108 | + // expected-disabled-note @-1 {{sending main actor-isolated 'self.ns' to nonisolated global function 'unspecifiedAsyncUse' risks causing data races between nonisolated and main actor-isolated uses}} |
| 109 | + |
| 110 | + nonisolatedSyncUse(ns) |
| 111 | + await nonisolatedAsyncUse(ns) // expected-disabled-error {{sending 'self.ns' risks causing data races}} |
| 112 | + // expected-disabled-note @-1 {{sending main actor-isolated 'self.ns' to nonisolated global function 'nonisolatedAsyncUse' risks causing data races between nonisolated and main actor-isolated uses}} |
| 113 | + } |
| 114 | + |
| 115 | + func callNonIsolatedWithResult() async { |
| 116 | + let x1 = unspecifiedSyncUseWithResult(ns) |
| 117 | + await sendToCustom(x1) // expected-error {{sending 'x1' risks causing data races}} |
| 118 | + // expected-note @-1 {{sending main actor-isolated 'x1' to global actor 'CustomActor'-isolated global function 'sendToCustom' risks causing data races between global actor 'CustomActor'-isolated and main actor-isolated uses}} |
| 119 | + |
| 120 | + let x2 = await unspecifiedAsyncUseWithResult(ns) // expected-disabled-error {{sending 'self.ns' risks causing data races}} |
| 121 | + // expected-disabled-note @-1 {{sending main actor-isolated 'self.ns' to nonisolated global function 'unspecifiedAsyncUseWithResult' risks causing data races between nonisolated and main actor-isolated uses}} |
| 122 | + await sendToCustom(x2) // expected-enabled-error {{sending 'x2' risks causing data races}} |
| 123 | + // expected-enabled-note @-1 {{sending main actor-isolated 'x2' to global actor 'CustomActor'-isolated global function 'sendToCustom' risks causing data races between global actor 'CustomActor'-isolated and main actor-isolated uses}} |
| 124 | + |
| 125 | + let x3 = nonisolatedSyncUseWithResult(ns) |
| 126 | + await sendToCustom(x3) // expected-error {{sending 'x3' risks causing data races}} |
| 127 | + // expected-note @-1 {{sending main actor-isolated 'x3' to global actor 'CustomActor'-isolated global function 'sendToCustom' risks causing data races between global actor 'CustomActor'-isolated and main actor-isolated uses}} |
| 128 | + |
| 129 | + let x4 = await nonisolatedAsyncUseWithResult(ns) // expected-disabled-error {{sending 'self.ns' risks causing data races}} |
| 130 | + // expected-disabled-note @-1 {{sending main actor-isolated 'self.ns' to nonisolated global function 'nonisolatedAsyncUseWithResult' risks causing data races between nonisolated and main actor-isolated uses}} |
| 131 | + await sendToCustom(x4) // expected-enabled-error {{sending 'x4' risks causing data races}} |
| 132 | + // expected-enabled-note @-1 {{sending main actor-isolated 'x4' to global actor 'CustomActor'-isolated global function 'sendToCustom' risks causing data races between global actor 'CustomActor'-isolated and main actor-isolated uses}} |
| 133 | + } |
| 134 | + |
| 135 | + func callNonIsolatedWithResult2() async { |
| 136 | + let x1 = unspecifiedSyncResult() |
| 137 | + await sendToCustom(x1) |
| 138 | + |
| 139 | + let x2 = await unspecifiedAsyncResult() |
| 140 | + await sendToCustom(x2) // expected-enabled-error {{sending 'x2' risks causing data races}} |
| 141 | + // expected-enabled-note @-1 {{sending main actor-isolated 'x2' to global actor 'CustomActor'-isolated global function 'sendToCustom' risks causing data races between global actor 'CustomActor'-isolated and main actor-isolated uses}} |
| 142 | + |
| 143 | + let x3 = nonisolatedSyncResult() |
| 144 | + await sendToCustom(x3) |
| 145 | + |
| 146 | + let x4 = await nonisolatedAsyncResult() |
| 147 | + await sendToCustom(x4) // expected-enabled-error {{sending 'x4' risks causing data races}} |
| 148 | + // expected-enabled-note @-1 {{sending main actor-isolated 'x4' to global actor 'CustomActor'-isolated global function 'sendToCustom' risks causing data races between global actor 'CustomActor'-isolated and main actor-isolated uses}} |
| 149 | + } |
| 150 | +} |
0 commit comments