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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
|
// RUN: %target-swift-frontend -emit-sil -parse-as-library -disable-availability-checking -strict-concurrency=complete -verify %s -o /dev/null -enable-upcoming-feature GlobalActorIsolatedTypesUsability
// REQUIRES: concurrency
// REQUIRES: asserts
////////////////////////
// MARK: Declarations //
////////////////////////
class NonSendableKlass {}
struct NonSendableStruct {
var first = NonSendableKlass()
var second = NonSendableKlass()
}
class KlassWithNonSendableStructPair {
var ns1: NonSendableStruct
var ns2: (NonSendableStruct, NonSendableStruct)
init() {
ns1 = NonSendableStruct()
ns2 = (ns1, ns1)
}
}
final class FinalKlassWithNonSendableStructPair {
var ns1: NonSendableStruct
var ns2: (NonSendableStruct, NonSendableStruct)
init() {
ns1 = NonSendableStruct()
ns2 = (ns1, ns1)
}
}
func useValue<T>(_ t: T) {}
func getAny() -> Any { fatalError() }
actor Custom {
}
@globalActor
struct CustomActor {
static var shared: Custom {
return Custom()
}
}
@MainActor func transferToMain<T>(_ t: T) {}
@CustomActor func transferToCustom<T>(_ t: T) {}
func throwingFunction() throws { fatalError() }
func transferArg(_ x: sending NonSendableKlass) {
}
func transferArgWithOtherParam(_ x: sending NonSendableKlass, _ y: NonSendableKlass) {
}
func transferArgWithOtherParam2(_ x: NonSendableKlass, _ y: sending NonSendableKlass) {
}
func twoTransferArg(_ x: sending NonSendableKlass, _ y: sending NonSendableKlass) {}
@MainActor var globalKlass = NonSendableKlass()
struct MyError : Error {}
func takeClosure(_ x: sending () -> ()) {}
func takeClosureAndParam(_ x: NonSendableKlass, _ y: sending () -> ()) {}
/////////////////
// MARK: Tests //
/////////////////
func testSimpleTransferLet() {
let k = NonSendableKlass()
transferArg(k) // expected-warning {{sending 'k' risks causing data races}}
// expected-note @-1 {{'k' used after being passed as a 'sending' parameter}}
useValue(k) // expected-note {{access can happen concurrently}}
}
func testSimpleTransferVar() {
var k = NonSendableKlass()
k = NonSendableKlass()
transferArg(k) // expected-warning {{sending 'k' risks causing data races}}
// expected-note @-1 {{'k' used after being passed as a 'sending' parameter}}
useValue(k) // expected-note {{access can happen concurrently}}
}
func testSimpleTransferUseOfOtherParamNoError() {
let k = NonSendableKlass()
let k2 = NonSendableKlass()
transferArgWithOtherParam(k, k2)
useValue(k2)
}
func testSimpleTransferUseOfOtherParamNoError2() {
let k = NonSendableKlass()
let k2 = NonSendableKlass()
transferArgWithOtherParam2(k, k2)
useValue(k)
}
@MainActor func transferToMain2(_ x: sending NonSendableKlass, _ y: NonSendableKlass, _ z: NonSendableKlass) async {
}
// TODO: How to test this?
func testNonStrongTransferDoesntMerge() async {
}
//////////////////////////////////
// MARK: Transferring Parameter //
//////////////////////////////////
func testTransferringParameter_canTransfer(_ x: sending NonSendableKlass, _ y: NonSendableKlass) async {
await transferToMain(x)
await transferToMain(y) // expected-warning {{sending 'y' risks causing data races}}
// expected-note @-1 {{sending task-isolated 'y' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and task-isolated uses}}
}
func testTransferringParameter_cannotTransferTwice(_ x: sending NonSendableKlass, _ y: NonSendableKlass) async {
await transferToMain(x) // expected-warning {{sending 'x' risks causing data races}}
// expected-note @-1 {{sending 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// TODO: We should not error on this since we are sending to the same place.
await transferToMain(x) // expected-note {{access can happen concurrently}}
}
func testTransferringParameter_cannotUseAfterTransfer(_ x: sending NonSendableKlass, _ y: NonSendableKlass) async {
await transferToMain(x) // expected-warning {{sending 'x' risks causing data races}}
// expected-note @-1 {{sending 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
useValue(x) // expected-note {{access can happen concurrently}}
}
actor MyActor {
var field = NonSendableKlass()
func canTransferWithTransferringMethodArg(_ x: sending NonSendableKlass, _ y: NonSendableKlass) async {
await transferToMain(x)
await transferToMain(y) // expected-warning {{sending 'y' risks causing data races}}
// expected-note @-1 {{sending 'self'-isolated 'y' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and 'self'-isolated uses}}
}
func getNormalErrorIfTransferTwice(_ x: sending NonSendableKlass) async {
await transferToMain(x) // expected-warning {{sending 'x' risks causing data races}}
// expected-note @-1 {{sending 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local actor-isolated uses}}
await transferToMain(x) // expected-note {{access can happen concurrently}}
}
func getNormalErrorIfUseAfterTransfer(_ x: sending NonSendableKlass) async {
await transferToMain(x) // expected-warning {{sending 'x' risks causing data races}}
// expected-note @-1 {{sending 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local actor-isolated uses}}
useValue(x) // expected-note {{access can happen concurrently}}
}
// After assigning into the actor, we can still use x in the actor as long as
// we don't transfer it.
func assignTransferringIntoActor(_ x: sending NonSendableKlass) async {
field = x
useValue(x)
}
// Once we assign into the actor, we cannot transfer further.
func assignTransferringIntoActor2(_ x: sending NonSendableKlass) async {
field = x
await transferToMain(x) // expected-warning {{sending 'x' risks causing data races}}
// expected-note @-1 {{sending 'self'-isolated 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and 'self'-isolated uses}}
}
}
@MainActor func canAssignTransferringIntoGlobalActor(_ x: sending NonSendableKlass) async {
globalKlass = x
}
@MainActor func canAssignTransferringIntoGlobalActor2(_ x: sending NonSendableKlass) async {
globalKlass = x
// TODO: This is incorrect! sending should be independent of @MainActor.
await transferToCustom(x) // expected-warning {{sending 'x' risks causing data races}}
// expected-note @-1 {{sending main actor-isolated 'x' to global actor 'CustomActor'-isolated global function 'transferToCustom' risks causing data races between global actor 'CustomActor'-isolated and main actor-isolated uses}}
}
@MainActor func canAssignTransferringIntoGlobalActor3(_ x: sending NonSendableKlass) async {
await transferToCustom(globalKlass) // expected-warning {{sending main actor-isolated value of type 'NonSendableKlass' with later accesses to global actor 'CustomActor'-isolated context risks causing data races}}
}
func canTransferAssigningIntoLocal(_ x: sending NonSendableKlass) async {
let _ = x
await transferToMain(x)
}
func canTransferAssigningIntoLocal2(_ x: sending NonSendableKlass) async {
let _ = x
await transferToMain(x)
// We do not error here since we just load the value and do not do anything
// with it.
//
// TODO: We should change let _ = x so that it has a move_value '_' or
// something like that. It will also help move checking as well.
let _ = x
}
func canTransferAssigningIntoLocal2a(_ x: sending NonSendableKlass) async {
let _ = x
await transferToMain(x)
// We do not error here since we just load the value and do not do anything
// with it.
//
// TODO: We should change let _ = x so that it has a move_value '_' or
// something like that. It will also help move checking as well.
_ = x
}
func canTransferAssigningIntoLocal3(_ x: sending NonSendableKlass) async {
let _ = x
await transferToMain(x) // expected-warning {{sending 'x' risks causing data races}}
// expected-note @-1 {{sending 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
let y = x // expected-note {{access can happen concurrently}}
_ = y
}
//////////////////////////////////////
// MARK: Transferring is "var" like //
//////////////////////////////////////
// Assigning into a 'sending' parameter is a merge.
func assigningIsAMerge(_ x: sending NonSendableKlass) async {
let y = NonSendableKlass()
x = y
// We can still transfer y since x is disconnected.
await transferToMain(y)
}
func assigningIsAMergeError(_ x: sending NonSendableKlass) async {
let y = NonSendableKlass()
x = y
// We can still transfer y since x is disconnected.
await transferToMain(y) // expected-warning {{sending 'y' risks causing data races}}
// expected-note @-1 {{sending 'y' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
useValue(x) // expected-note {{access can happen concurrently}}
}
func assigningIsAMergeAny(_ x: sending Any) async {
// Ok, this is disconnected.
let y = getAny()
x = y
await transferToMain(y)
}
func assigningIsAMergeAnyError(_ x: sending Any) async {
// Ok, this is disconnected.
let y = getAny()
x = y
await transferToMain(y) // expected-warning {{sending 'y' risks causing data races}}
// expected-note @-1 {{sending 'y' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
useValue(x) // expected-note {{access can happen concurrently}}
}
func canTransferAfterAssign(_ x: sending Any) async {
// Ok, this is disconnected.
let y = getAny()
// y is transferred into x.
await transferToMain(x)
x = y
useValue(x)
}
func canTransferAfterAssignButUseIsError(_ x: sending Any) async {
// Ok, this is disconnected.
let y = getAny()
// y is transferred into x.
x = y
// TODO: This should refer to the sending parameter.
await transferToMain(x) // expected-warning {{sending 'x' risks causing data races}}
// expected-note @-1 {{sending 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
useValue(x) // expected-note {{access can happen concurrently}}
}
func assignToEntireValueEliminatesEarlierTransfer(_ x: sending Any) async {
// Ok, this is disconnected.
let y = getAny()
useValue(x)
// Transfer x
await transferToMain(x)
// y is transferred into x. This shouldn't error.
x = y
useValue(x)
}
func mergeDoesNotEliminateEarlierTransfer(_ x: sending NonSendableStruct) async {
// Ok, this is disconnected.
let y = NonSendableKlass()
useValue(x)
// Transfer x
await transferToMain(x) // expected-warning {{sending 'x' risks causing data races}}
// expected-note @-1 {{sending 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// y is assigned into a field of x.
x.first = y // expected-note {{access can happen concurrently}}
useValue(x)
}
func mergeDoesNotEliminateEarlierTransfer2(_ x: sending NonSendableStruct) async {
// Ok, this is disconnected.
let y = NonSendableKlass()
useValue(x)
// Transfer x
await transferToMain(x) // expected-warning {{sending 'x' risks causing data races}}
// expected-note @-1 {{sending 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
x.first = y // expected-note {{access can happen concurrently}}
}
func doubleArgument() async {
let x = NonSendableKlass()
twoTransferArg(x, x) // expected-warning {{sending 'x' risks causing data races}}
// expected-note @-1 {{'x' used after being passed as a 'sending' parameter}}
// expected-note @-2 {{access can happen concurrently}}
}
func testTransferSrc(_ x: sending NonSendableKlass) async {
let y = NonSendableKlass()
await transferToMain(y) // expected-warning {{sending 'y' risks causing data races}}
// expected-note @-1 {{sending 'y' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
x = y // expected-note {{access can happen concurrently}}
}
func testTransferOtherParam(_ x: sending NonSendableKlass, y: NonSendableKlass) async {
x = y
}
func testTransferOtherParamTuple(_ x: sending NonSendableKlass, y: (NonSendableKlass, NonSendableKlass)) async {
x = y.0
}
func fakeInitOutside(operation: sending @escaping () async -> ()) {}
func taskIsolatedOutsideError(_ x: @escaping @MainActor () async -> ()) {
fakeInitOutside(operation: x) // okay; x is @Sendable
}
@MainActor func actorIsolatedOutsideError(_ x: @escaping @MainActor () async -> ()) {
fakeInitOutside(operation: x) // okay; x is @Sendable
}
func taskIsolatedInsideError(_ x: @escaping @MainActor () async -> ()) {
func fakeInit(operation: sending @escaping () async -> ()) {}
fakeInit(operation: x) // okay; x is @Sendable
}
@MainActor func actorIsolatedInsideError(_ x: @escaping @MainActor () async -> ()) {
func fakeInit(operation: sending @escaping () async -> ()) {}
// We do not get an error here since fakeInit is main actor isolated since it
// is defined within this function. As a result, we are sending a @MainActor
// isolated thing to a MainActor isolated thing.
fakeInit(operation: x)
}
// Make sure we error here on only the second since x by being assigned a part
// of y becomes task-isolated
func testMergeWithTaskIsolated(_ x: sending NonSendableKlass, y: NonSendableKlass) async {
await transferToMain(x)
x = y
await transferToMain(x) // expected-warning {{sending 'x' risks causing data races}}
// expected-note @-1 {{sending task-isolated 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and task-isolated uses}}
}
@MainActor func testMergeWithActorIsolated(_ x: sending NonSendableKlass, y: NonSendableKlass) async {
x = y
await transferToCustom(x) // expected-warning {{sending 'x' risks causing data races}}
// expected-note @-1 {{sending main actor-isolated 'x' to global actor 'CustomActor'-isolated global function 'transferToCustom' risks causing data races between global actor 'CustomActor'-isolated and main actor-isolated uses}}
}
@available(SwiftStdlib 5.1, *)
actor NonSendableInit {
var first: NonSendableKlass
var second: NonSendableKlass? = nil {
@storageRestrictions(initializes: first)
init(initialValue) {
transferArg(initialValue!) // expected-warning {{sending 'initialValue' risks causing data races}}
// expected-note @-1 {{'self'-isolated 'initialValue' is passed as a 'sending' parameter}}
first = initialValue!
}
get { fatalError() }
set { fatalError() }
}
}
func testNoCrashWhenSendingNoEscapeClosure() async {
func test(_ x: sending () -> ()) async {}
let c = NonSendableKlass()
await test { print(c) }
}
///////////////////////////////
// MARK: InOut Sending Tests //
///////////////////////////////
func testInOutSendingReinit(_ x: inout sending NonSendableKlass) async {
await transferToMain(x) // expected-warning {{sending 'x' risks causing data races}}
// expected-note @-1 {{sending 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
} // expected-note {{'inout sending' parameter must be reinitialized before function exit with a non-actor isolated value}}
func testInOutSendingReinit2(_ x: inout sending NonSendableKlass) async {
await transferToMain(x)
x = NonSendableKlass()
}
func testInOutSendingReinit3(_ x: inout sending NonSendableKlass) async throws {
await transferToMain(x) // expected-warning {{sending 'x' risks causing data races}}
// expected-note @-1 {{sending 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
try throwingFunction() // expected-note {{'inout sending' parameter must be reinitialized before function exit with a non-actor isolated value}}
x = NonSendableKlass()
}
func testInOutSendingReinit4(_ x: inout sending NonSendableKlass) async throws {
await transferToMain(x) // expected-warning {{sending 'x' risks causing data races}}
// expected-note @-1 {{sending 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
do {
try throwingFunction()
x = NonSendableKlass()
} catch {
throw MyError() // expected-note {{'inout sending' parameter must be reinitialized before function exit with a non-actor isolated value}}
}
x = NonSendableKlass()
}
func testInOutSendingReinit5(_ x: inout sending NonSendableKlass) async throws {
await transferToMain(x) // expected-warning {{sending 'x' risks causing data races}}
// expected-note @-1 {{sending 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
do {
try throwingFunction()
} catch {
throw MyError() // expected-note {{'inout sending' parameter must be reinitialized before function exit with a non-actor isolated value}}
}
x = NonSendableKlass()
}
func testInOutSendingReinit6(_ x: inout sending NonSendableKlass) async throws {
await transferToMain(x) // expected-warning {{sending 'x' risks causing data races}}
// expected-note @-1 {{sending 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
do {
try throwingFunction()
} catch {
throw MyError() // expected-note {{'inout sending' parameter must be reinitialized before function exit with a non-actor isolated value}}
}
} // expected-note {{'inout sending' parameter must be reinitialized before function exit with a non-actor isolated value}}
actor InOutSendingWrongIsolationActor {
var ns = NonSendableKlass()
func testWrongIsolation(_ x: inout sending NonSendableKlass) {
x = ns
} // expected-warning {{'inout sending' parameter 'x' cannot be 'self'-isolated at end of function}}
// expected-note @-1 {{'self'-isolated 'x' risks causing races in between 'self'-isolated uses and caller uses since caller assumes value is not actor isolated}}
func testWrongIsolation2(_ x: inout sending NonSendableKlass) {
let z = ns
x = z
} // expected-warning {{'inout sending' parameter 'x' cannot be 'self'-isolated at end of function}}
// expected-note @-1 {{'self'-isolated 'x' risks causing races in between 'self'-isolated uses and caller uses since caller assumes value is not actor isolated}}
}
@MainActor
func testWrongIsolationGlobalIsolation(_ x: inout sending NonSendableKlass) {
x = globalKlass
} // expected-warning {{'inout sending' parameter 'x' cannot be main actor-isolated at end of function}}
// expected-note @-1 {{main actor-isolated 'x' risks causing races in between main actor-isolated uses and caller uses since caller assumes value is not actor isolated}}
func taskIsolatedCaptureInSendingClosureLiteral(_ x: NonSendableKlass) {
Task { // expected-warning {{passing closure as a 'sending' parameter risks causing data races between code in the current task and concurrent execution of the closure}}
print(x) // expected-note {{closure captures 'x' which is accessible to code in the current task}}
}
takeClosure { // expected-warning {{passing closure as a 'sending' parameter risks causing data races between code in the current task and concurrent execution of the closure}}
print(x) // expected-note {{closure captures 'x' which is accessible to code in the current task}}
}
takeClosureAndParam(NonSendableKlass()) { // expected-warning {{passing closure as a 'sending' parameter risks causing data races between code in the current task and concurrent execution of the closure}}
print(x) // expected-note {{closure captures 'x' which is accessible to code in the current task}}
}
let y = (x, x)
Task { // expected-warning {{passing closure as a 'sending' parameter risks causing data races between code in the current task and concurrent execution of the closure}}
print(y) // expected-note {{closure captures 'y' which is accessible to code in the current task}}
}
let z = (x, y)
Task { // expected-warning {{passing closure as a 'sending' parameter risks causing data races between code in the current task and concurrent execution of the closure}}
print(y, z) // expected-note @:11 {{closure captures non-Sendable 'y'}}
// expected-note @-1:14 {{closure captures non-Sendable 'z'}}
}
}
extension MyActor {
func actorIsolatedCaptureInSendingClosureLiteral(_ x: NonSendableKlass) {
Task { // expected-warning {{passing closure as a 'sending' parameter risks causing data races between 'self'-isolated code and concurrent execution of the closure}}
print(x) // expected-note {{closure captures 'self'-isolated 'x'}}
}
takeClosure { // expected-warning {{passing closure as a 'sending' parameter risks causing data races between 'self'-isolated code and concurrent execution of the closure}}
print(x) // expected-note {{closure captures 'self'-isolated 'x'}}
}
takeClosureAndParam(NonSendableKlass()) { // expected-warning {{passing closure as a 'sending' parameter risks causing data races between 'self'-isolated code and concurrent execution of the closure}}
print(x) // expected-note {{closure captures 'self'-isolated 'x'}}
}
let y = (x, x)
Task { // expected-warning {{passing closure as a 'sending' parameter risks causing data races between 'self'-isolated code and concurrent execution of the closure}}
print(y) // expected-note {{closure captures 'y' which is accessible to 'self'-isolated code}}
}
let z = (x, y)
Task { // expected-warning {{passing closure as a 'sending' parameter risks causing data races between 'self'-isolated code and concurrent execution of the closure}}
print(y, z) // expected-note @:13 {{closure captures non-Sendable 'y'}}
// expected-note @-1:16 {{closure captures non-Sendable 'z'}}
}
}
}
|