1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
// RUN: %target-swift-frontend -emit-sil -strict-concurrency=complete -disable-availability-checking -swift-version 6 -verify %s -o /dev/null
// REQUIRES: concurrency
// REQUIRES: asserts
// This test validates the behavior of transfernonsendable around initializers.
////////////////////////
// MARK: Declarations //
////////////////////////
class NonSendableKlass {}
@MainActor func transferToMain<T>(_ t: T) async {}
actor CustomActorInstance {}
@globalActor
struct CustomActor {
static let shared = CustomActorInstance()
}
/////////////////
// MARK: Tests //
/////////////////
actor ActorWithSynchronousNonIsolatedInit {
let k: NonSendableKlass
init(_ newK: NonSendableKlass) {
k = NonSendableKlass()
helper(newK)
// TODO: This should say actor isolated.
let _ = { @MainActor in
print(newK) // expected-error {{sending 'newK' risks causing data races}}
// expected-note @-1 {{task-isolated 'newK' is captured by a main actor-isolated closure. main actor-isolated uses in closure may race against later nonisolated uses}}
}
}
init(x newK: NonSendableKlass) {
k = newK
helper(newK)
let _ = { @MainActor in
// TODO: Second part should say later 'self'-isolated uses
print(newK) // expected-error {{sending 'newK' risks causing data races}}
// expected-note @-1 {{'self'-isolated 'newK' is captured by a main actor-isolated closure. main actor-isolated uses in closure may race against later nonisolated uses}}
}
}
init(ns: NonSendableKlass) async {
self.k = NonSendableKlass()
self.isolatedHelper(ns)
}
nonisolated func helper(_ newK: NonSendableKlass) {}
func isolatedHelper(_ newK: NonSendableKlass) {}
}
func initActorWithSyncNonIsolatedInit() {
let k = NonSendableKlass()
// TODO: This should say actor isolated.
_ = ActorWithSynchronousNonIsolatedInit(k) // expected-error {{sending 'k' risks causing data races}}
// expected-note @-1 {{sending 'k' to actor-isolated initializer 'init(_:)' risks causing data races between actor-isolated and local nonisolated uses}}
let _ = { @MainActor in // expected-note {{access can happen concurrently}}
print(k)
}
}
func initActorWithSyncNonIsolatedInit2(_ k: NonSendableKlass) {
// TODO: This should say actor isolated.
_ = ActorWithSynchronousNonIsolatedInit(k) // expected-error {{sending 'k' risks causing data races}}
// expected-note @-1 {{sending task-isolated 'k' to actor-isolated initializer 'init(_:)' risks causing data races between actor-isolated and task-isolated uses}}
let _ = { @MainActor in
print(k) // expected-error {{sending 'k' risks causing data races}}
// expected-note @-1 {{task-isolated 'k' is captured by a main actor-isolated closure. main actor-isolated uses in closure may race against later nonisolated uses}}
}
}
actor ActorWithAsyncIsolatedInit {
init(_ newK: NonSendableKlass) async {
// TODO: This should say actor isolated.
let _ = { @MainActor in
print(newK) // expected-error {{sending 'newK' risks causing data races}}
// expected-note @-1 {{'self'-isolated 'newK' is captured by a main actor-isolated closure. main actor-isolated uses in closure may race against later nonisolated uses}}
}
}
}
func initActorWithAsyncIsolatedInit() async {
let k = NonSendableKlass()
// TODO: This should say actor isolated.
_ = await ActorWithAsyncIsolatedInit(k) // expected-error {{sending 'k' risks causing data races}}
// expected-note @-1 {{sending 'k' to actor-isolated initializer 'init(_:)' risks causing data races between actor-isolated and local nonisolated uses}}
let _ = { @MainActor in // expected-note {{access can happen concurrently}}
print(k)
}
}
func initActorWithAsyncIsolatedInit2(_ k: NonSendableKlass) async {
// TODO: This should say actor isolated.
_ = await ActorWithAsyncIsolatedInit(k) // expected-error {{sending 'k' risks causing data races}}
// expected-note @-1 {{sending task-isolated 'k' to actor-isolated initializer 'init(_:)' risks causing data races between actor-isolated and task-isolated uses}}
let _ = { @MainActor in
print(k) // expected-error {{sending 'k' risks causing data races}}
// expected-note @-1 {{task-isolated 'k' is captured by a main actor-isolated closure. main actor-isolated uses in closure may race against later nonisolated uses}}
}
}
////////////////////////////////
// MARK: Actor Isolated Class //
////////////////////////////////
@CustomActor
class ClassWithSynchronousNonIsolatedInit {
nonisolated init(_ newK: NonSendableKlass) {
// We do not error on this since helper is nonisolated and newK is
// considered task isolated.
helper(newK)
let _ = { @MainActor in
print(newK) // expected-error {{sending 'newK' risks causing data races}}
// expected-note @-1 {{task-isolated 'newK' is captured by a main actor-isolated closure. main actor-isolated uses in closure may race against later nonisolated uses}}
}
}
nonisolated func helper(_ newK: NonSendableKlass) {}
}
func initClassWithSyncNonIsolatedInit() {
let k = NonSendableKlass()
_ = ClassWithSynchronousNonIsolatedInit(k)
let _ = { @MainActor in
print(k)
}
}
func initClassWithSyncNonIsolatedInit2(_ k: NonSendableKlass) {
_ = ClassWithSynchronousNonIsolatedInit(k)
let _ = { @MainActor in
print(k) // expected-error {{sending 'k' risks causing data races}}
// expected-note @-1 {{task-isolated 'k' is captured by a main actor-isolated closure. main actor-isolated uses in closure may race against later nonisolated uses}}
}
}
@CustomActor
class ClassWithAsyncIsolatedInit {
init(_ newK: NonSendableKlass) async {
let _ = { @MainActor in
print(newK) // expected-error {{sending 'newK' risks causing data races}}
// expected-note @-1 {{global actor 'CustomActor'-isolated 'newK' is captured by a main actor-isolated closure. main actor-isolated uses in closure may race against later nonisolated uses}}
}
}
}
func initClassWithAsyncIsolatedInit() async {
let k = NonSendableKlass()
// TODO: Might make sense to emit a more specific error here since the closure
// is MainActor isolated. The actual capture is initially not isolated to
// MainActor.
_ = await ClassWithAsyncIsolatedInit(k) // expected-error {{sending 'k' risks causing data races}}
// expected-note @-1 {{sending 'k' to global actor 'CustomActor'-isolated initializer 'init(_:)' risks causing data races between global actor 'CustomActor'-isolated and local nonisolated uses}}
let _ = { @MainActor in // expected-note {{access can happen concurrently}}
print(k)
}
}
func initClassWithAsyncIsolatedInit2(_ k: NonSendableKlass) async {
_ = await ClassWithAsyncIsolatedInit(k) // expected-error {{sending 'k' risks causing data races}}
// expected-note @-1 {{sending task-isolated 'k' to global actor 'CustomActor'-isolated initializer 'init(_:)' risks causing data races between global actor 'CustomActor'-isolated and task-isolated uses}}
let _ = { @MainActor in
print(k) // expected-error {{sending 'k' risks causing data races}}
// expected-note @-1 {{task-isolated 'k' is captured by a main actor-isolated closure. main actor-isolated uses in closure may race against later nonisolated uses}}
}
}
|