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 561 562 563 564 565 566 567 568 569 570 571 572 573 574
|
// RUN: %target-swift-frontend -disable-availability-checking -strict-concurrency=complete -enable-upcoming-feature InferSendableFromCaptures -parse-as-library %s -emit-sil -o /dev/null -verify
// REQUIRES: concurrency
// REQUIRES: asserts
// some utilities
func thrower() throws {}
func asyncer() async {}
func rethrower(_ f : @autoclosure () throws -> Any) rethrows -> Any {
return try f()
}
func asAutoclosure(_ f : @autoclosure () -> Any) -> Any { return f() }
// not a concurrency-safe type
class Box { // expected-note 4{{class 'Box' does not conform to the 'Sendable' protocol}}
var counter : Int = 0
}
actor BankAccount {
private var curBalance : Int
private var accountHolder : String = "unknown"
// expected-note@+1 {{mutation of this property is only permitted within the actor}}
var owner : String {
get { accountHolder }
set { accountHolder = newValue }
}
init(initialDeposit : Int) {
curBalance = initialDeposit
}
func balance() -> Int { return curBalance }
// expected-note@+1 {{calls to instance method 'deposit' from outside of its actor context are implicitly asynchronous}}
func deposit(_ amount : Int) -> Int {
guard amount >= 0 else { return 0 }
curBalance = curBalance + amount
return curBalance
}
func canWithdraw(_ amount : Int) -> Bool {
// call 'balance' from sync through self
return self.balance() >= amount
}
func testSelfBalance() async {
_ = await balance() // expected-warning {{no 'async' operations occur within 'await' expression}}
}
// returns the amount actually withdrawn
func withdraw(_ amount : Int) -> Int {
guard canWithdraw(amount) else { return 0 }
curBalance = curBalance - amount
return amount
}
// returns the balance of this account following the transfer
func transferAll(from : BankAccount) async -> Int {
// call sync methods on another actor
let amountTaken = await from.withdraw(from.balance())
return deposit(amountTaken)
}
func greaterThan(other : BankAccount) async -> Bool {
return await balance() > other.balance()
}
func testTransactions() {
_ = deposit(withdraw(deposit(withdraw(balance()))))
}
func testThrowing() throws {}
var effPropA : Box {
get async {
await asyncer()
return Box()
}
}
var effPropT : Box {
get throws {
try thrower()
return Box()
}
}
var effPropAT : Int {
get async throws {
await asyncer()
try thrower()
return 0
}
}
// expected-note@+1 2 {{add 'async' to function 'effPropertiesFromInsideActorInstance()' to make it asynchronous}}
func effPropertiesFromInsideActorInstance() throws {
// expected-error@+1{{'async' property access in a function that does not support concurrency}}
_ = effPropA
// expected-note@+4{{did you mean to handle error as optional value?}}
// expected-note@+3{{did you mean to use 'try'?}}
// expected-note@+2{{did you mean to disable error propagation?}}
// expected-error@+1{{property access can throw but is not marked with 'try'}}
_ = effPropT
_ = try effPropT
// expected-note@+6 {{did you mean to handle error as optional value?}}
// expected-note@+5 {{did you mean to use 'try'?}}
// expected-note@+4 {{did you mean to disable error propagation?}}
// expected-error@+3 {{property access can throw but is not marked with 'try'}}
// expected-note@+2 {{call is to 'rethrows' function, but argument function can throw}}
// expected-error@+1 {{call can throw but is not marked with 'try'}}
_ = rethrower(effPropT)
// expected-note@+2 {{call is to 'rethrows' function, but argument function can throw}}
// expected-error@+1 {{call can throw but is not marked with 'try'}}
_ = rethrower(try effPropT)
_ = try rethrower(effPropT)
_ = try rethrower(thrower())
_ = try rethrower(try effPropT)
_ = try rethrower(try thrower())
_ = rethrower(effPropA) // expected-error{{'async' property access in an autoclosure that does not support concurrency}}
_ = asAutoclosure(effPropT) // expected-error{{property access can throw, but it is not marked with 'try' and it is executed in a non-throwing autoclosure}}
// expected-note@+5{{did you mean to handle error as optional value?}}
// expected-note@+4{{did you mean to use 'try'?}}
// expected-note@+3{{did you mean to disable error propagation?}}
// expected-error@+2{{property access can throw but is not marked with 'try'}}
// expected-error@+1{{'async' property access in a function that does not support concurrency}}
_ = effPropAT
}
} // end actor
func someAsyncFunc() async {
let deposit1 = 120, deposit2 = 45
let a = BankAccount(initialDeposit: 0)
let b = BankAccount(initialDeposit: deposit2)
let _ = await a.deposit(deposit1)
let afterXfer = await a.transferAll(from: b)
let reportedBal = await a.balance()
// check on account A
guard afterXfer == (deposit1 + deposit2) && afterXfer == reportedBal else {
print("BUG 1!")
return
}
// check on account B
guard await b.balance() == 0 else {
print("BUG 2!")
return
}
_ = await a.deposit(b.withdraw(a.deposit(b.withdraw(b.balance()))))
// expected-error@+1 {{expression is 'async' but is not marked with 'await'}}{{3-3=await }} expected-note@+1 {{call is 'async'}}
a.testSelfBalance()
await a.testThrowing() // expected-error {{call can throw, but it is not marked with 'try' and the error is not handled}}
////////////
// effectful properties from outside the actor instance
// expected-warning@+2 {{non-sendable type 'Box' in asynchronous access to actor-isolated property 'effPropA' cannot cross actor boundary}}
// expected-error@+1{{expression is 'async' but is not marked with 'await'}} {{7-7=await }} expected-note@+1{{property access is 'async'}}
_ = a.effPropA
// expected-warning@+3 {{non-sendable type 'Box' in implicitly asynchronous access to actor-isolated property 'effPropT' cannot cross actor boundary}}
// expected-error@+2{{property access can throw, but it is not marked with 'try' and the error is not handled}}
// expected-error@+1{{expression is 'async' but is not marked with 'await'}} {{7-7=await }} expected-note@+1{{property access is 'async'}}
_ = a.effPropT
// expected-error@+2{{property access can throw, but it is not marked with 'try' and the error is not handled}}
// expected-error@+1{{expression is 'async' but is not marked with 'await'}} {{7-7=await }} expected-note@+1{{property access is 'async'}}
_ = a.effPropAT
// (mostly) corrected ones
_ = await a.effPropA // expected-warning {{non-sendable type 'Box' in asynchronous access to actor-isolated property 'effPropA' cannot cross actor boundary}}
_ = try! await a.effPropT // expected-warning {{non-sendable type 'Box' in implicitly asynchronous access to actor-isolated property 'effPropT' cannot cross actor boundary}}
_ = try? await a.effPropAT
print("ok!")
}
//////////////////
// check for appropriate error messages
//////////////////
extension BankAccount {
func totalBalance(including other: BankAccount) async -> Int {
//expected-error@+1{{expression is 'async' but is not marked with 'await'}}{{12-12=await }}
return balance()
+ other.balance() // expected-note{{calls to instance method 'balance()' from outside of its actor context are implicitly asynchronous}}
}
func breakAccounts(other: BankAccount) async {
// expected-error@+1{{expression is 'async' but is not marked with 'await'}}{{9-9=await }}
_ = other.deposit( // expected-note{{calls to instance method 'deposit' from outside of its actor context are implicitly asynchronous}}
other.withdraw( // expected-note{{calls to instance method 'withdraw' from outside of its actor context are implicitly asynchronous}}
self.deposit(
other.withdraw( // expected-note{{calls to instance method 'withdraw' from outside of its actor context are implicitly asynchronous}}
other.balance())))) // expected-note{{calls to instance method 'balance()' from outside of its actor context are implicitly asynchronous}}
}
}
func anotherAsyncFunc() async {
let a = BankAccount(initialDeposit: 34)
let b = BankAccount(initialDeposit: 35)
// expected-error@+2{{expression is 'async' but is not marked with 'await'}} {{7-7=await }}
// expected-note@+1{{calls to instance method 'deposit' from outside of its actor context are implicitly asynchronous}}
_ = a.deposit(1)
// expected-error@+2{{expression is 'async' but is not marked with 'await'}} {{7-7=await }}
// expected-note@+1{{calls to instance method 'balance()' from outside of its actor context are implicitly asynchronous}}
_ = b.balance()
_ = b.balance // expected-error {{actor-isolated instance method 'balance()' can not be partially applied}}
a.owner = "cat" // expected-error{{actor-isolated property 'owner' can not be mutated from a nonisolated context}}
// expected-error@+1{{expression is 'async' but is not marked with 'await'}} {{7-7=await }} expected-note@+1{{property access is 'async'}}
_ = b.owner
_ = await b.owner == "cat"
}
func regularFunc() {
let a = BankAccount(initialDeposit: 34)
_ = a.deposit //expected-error{{actor-isolated instance method 'deposit' can not be partially applied}}
_ = a.deposit(1) // expected-error{{call to actor-isolated instance method 'deposit' in a synchronous nonisolated context}}
}
actor TestActor {}
@globalActor
struct BananaActor {
static var shared: TestActor { TestActor() }
}
@globalActor
struct OrangeActor {
static var shared: TestActor { TestActor() }
}
func blender(_ peeler : () -> Void) {
peeler()
}
// expected-note@+2 {{var declared here}}
// expected-note@+1 2 {{mutation of this var is only permitted within the actor}}
@BananaActor var dollarsInBananaStand : Int = 250000
@BananaActor func wisk(_ something : Any) { }
@BananaActor func peelBanana() { }
@BananaActor func takeInout(_ x : inout Int) {}
@OrangeActor func makeSmoothie() async {
var money = await dollarsInBananaStand
money -= 1200
dollarsInBananaStand = money // expected-error{{global actor 'BananaActor'-isolated var 'dollarsInBananaStand' can not be mutated from global actor 'OrangeActor'}}
// FIXME: these two errors seem a bit redundant.
// expected-error@+2 {{actor-isolated var 'dollarsInBananaStand' cannot be passed 'inout' to implicitly 'async' function call}}
// expected-error@+1 {{global actor 'BananaActor'-isolated var 'dollarsInBananaStand' can not be used 'inout' from global actor 'OrangeActor'}}
await takeInout(&dollarsInBananaStand)
_ = wisk
await wisk({})
await wisk(1)
await (peelBanana)()
await (((((peelBanana)))))()
await (((wisk)))((wisk)((wisk)(1)))
blender((peelBanana))
// expected-warning@-1 {{converting function value of type '@BananaActor @Sendable () -> ()' to '() -> Void' loses global actor 'BananaActor'}}
await wisk(peelBanana)
await wisk(wisk)
await (((wisk)))(((wisk)))
await {wisk}()(1)
(true ? wisk : {n in return})(1)
// expected-warning@-1 {{converting function value of type '@BananaActor @Sendable (Any) -> ()' to '(Any) -> ()' loses global actor 'BananaActor'; this is an error in the Swift 6 language mode}}
}
actor Chain {
var next : Chain?
}
func walkChain(chain : Chain) async {
// expected-error@+1 {{expression is 'async' but is not marked with 'await'}}{{7-7=await }} expected-note@+1 4 {{property access is 'async'}}
_ = chain.next?.next?.next?.next
// expected-error@+1 {{expression is 'async' but is not marked with 'await'}}{{7-7=await }} expected-note@+1 3 {{property access is 'async'}}
_ = (await chain.next)?.next?.next?.next
// expected-error@+1 {{expression is 'async' but is not marked with 'await'}}{{7-7=await }} expected-note@+1 2 {{property access is 'async'}}
_ = (await chain.next?.next)?.next?.next
}
// want to make sure there is no note about implicitly async on this func.
@BananaActor func rice() async {}
@OrangeActor func quinoa() async {
// expected-error@+1{{expression is 'async' but is not marked with 'await'}}{{3-3=await }} expected-note@+1 {{call is 'async'}}
rice()
}
///////////
// check various curried applications to ensure we mark the right expression.
actor Calculator {
func addCurried(_ x : Int) -> ((Int) -> Int) {
return { (_ y : Int) in x + y }
}
func add(_ x : Int, _ y : Int) -> Int {
return x + y
}
}
@BananaActor func bananaAdd(_ x : Int) -> ((Int) -> Int) {
return { (_ y : Int) in x + y }
}
@OrangeActor func doSomething() async {
let _ = (await bananaAdd(1))(2)
// expected-warning@-1{{non-sendable type '(Int) -> Int' returned by call to global actor 'BananaActor'-isolated function cannot cross actor boundary}}
// expected-note@-2{{a function type must be marked '@Sendable' to conform to 'Sendable'}}
let _ = await (await bananaAdd(1))(2) // expected-warning{{no 'async' operations occur within 'await' expression}}
// expected-warning@-1{{non-sendable type '(Int) -> Int' returned by call to global actor 'BananaActor'-isolated function cannot cross actor boundary}}
// expected-note@-2{{a function type must be marked '@Sendable' to conform to 'Sendable'}}
let calc = Calculator()
let _ = (await calc.addCurried(1))(2)
// expected-warning@-1{{non-sendable type '(Int) -> Int' returned by call to actor-isolated function cannot cross actor boundary}}
// expected-note@-2{{a function type must be marked '@Sendable' to conform to 'Sendable'}}
let _ = await (await calc.addCurried(1))(2) // expected-warning{{no 'async' operations occur within 'await' expression}}
// expected-warning@-1{{non-sendable type '(Int) -> Int' returned by call to actor-isolated function cannot cross actor boundary}}
// expected-note@-2{{a function type must be marked '@Sendable' to conform to 'Sendable'}}
let plusOne = await calc.addCurried(await calc.add(0, 1))
// expected-warning@-1{{non-sendable type '(Int) -> Int' returned by call to actor-isolated function cannot cross actor boundary}}
// expected-note@-2{{a function type must be marked '@Sendable' to conform to 'Sendable'}}
let _ = plusOne(2)
}
///////
// Effectful properties isolated to a global actor
@BananaActor
var effPropA : Int {
get async {
await asyncer()
try thrower() // expected-error{{errors thrown from here are not handled}}
return 0
}
}
@BananaActor
var effPropT : Int { // expected-note{{var declared here}}
get throws {
await asyncer() // expected-error{{'async' call in a function that does not support concurrency}}
try thrower()
return 0
}
}
@BananaActor
var effPropAT : Int {
get async throws {
await asyncer()
try thrower()
return 0
}
}
// expected-note@+1 2 {{add 'async' to function 'tryEffPropsFromBanana()' to make it asynchronous}}
@BananaActor func tryEffPropsFromBanana() throws {
// expected-error@+1{{'async' property access in a function that does not support concurrency}}
_ = effPropA
// expected-note@+4{{did you mean to handle error as optional value?}}
// expected-note@+3{{did you mean to use 'try'?}}
// expected-note@+2{{did you mean to disable error propagation?}}
// expected-error@+1{{property access can throw but is not marked with 'try'}}
_ = effPropT
_ = try effPropT
// expected-note@+6 {{did you mean to handle error as optional value?}}
// expected-note@+5 {{did you mean to use 'try'?}}
// expected-note@+4 {{did you mean to disable error propagation?}}
// expected-error@+3 {{property access can throw but is not marked with 'try'}}
// expected-note@+2 {{call is to 'rethrows' function, but argument function can throw}}
// expected-error@+1 {{call can throw but is not marked with 'try'}}
_ = rethrower(effPropT)
// expected-note@+2 {{call is to 'rethrows' function, but argument function can throw}}
// expected-error@+1 {{call can throw but is not marked with 'try'}}
_ = rethrower(try effPropT)
_ = try rethrower(effPropT)
_ = try rethrower(thrower())
_ = try rethrower(try effPropT)
_ = try rethrower(try thrower())
_ = rethrower(effPropA) // expected-error{{'async' property access in an autoclosure that does not support concurrency}}
_ = asAutoclosure(effPropT) // expected-error{{property access can throw, but it is not marked with 'try' and it is executed in a non-throwing autoclosure}}
// expected-note@+5{{did you mean to handle error as optional value?}}
// expected-note@+4{{did you mean to use 'try'?}}
// expected-note@+3{{did you mean to disable error propagation?}}
// expected-error@+2{{property access can throw but is not marked with 'try'}}
// expected-error@+1{{'async' property access in a function that does not support concurrency}}
_ = effPropAT
}
// expected-note@+2 {{add '@BananaActor' to make global function 'tryEffPropsFromSync()' part of global actor 'BananaActor'}}
// expected-note@+1 2 {{add 'async' to function 'tryEffPropsFromSync()' to make it asynchronous}}
func tryEffPropsFromSync() {
_ = effPropA // expected-error{{'async' property access in a function that does not support concurrency}}
// expected-error@+1 {{property access can throw, but it is not marked with 'try' and the error is not handled}}
_ = effPropT // expected-error{{global actor 'BananaActor'-isolated var 'effPropT' can not be referenced from a nonisolated context}}
// NOTE: that we don't complain about async access on `effPropT` because it's not declared async, and we're not in an async context!
// expected-error@+1 {{property access can throw, but it is not marked with 'try' and the error is not handled}}
_ = effPropAT // expected-error{{'async' property access in a function that does not support concurrency}}
}
@OrangeActor func tryEffPropertiesFromGlobalActor() async throws {
// expected-error@+1{{expression is 'async' but is not marked with 'await'}}{{7-7=await }} expected-note@+1 {{property access is 'async'}}
_ = effPropA
// expected-note@+5{{did you mean to handle error as optional value?}}
// expected-note@+4{{did you mean to use 'try'?}}
// expected-note@+3{{did you mean to disable error propagation?}}
// expected-error@+2{{property access can throw but is not marked with 'try'}}
// expected-error@+1{{expression is 'async' but is not marked with 'await'}}{{7-7=await }} expected-note@+1 {{property access is 'async'}}
_ = effPropT
// expected-note@+5{{did you mean to handle error as optional value?}}
// expected-note@+4{{did you mean to use 'try'?}}
// expected-note@+3{{did you mean to disable error propagation?}}
// expected-error@+2{{property access can throw but is not marked with 'try'}}
// expected-error@+1{{expression is 'async' but is not marked with 'await'}}{{7-7=await }} expected-note@+1 {{property access is 'async'}}
_ = effPropAT
_ = await effPropA
_ = try? await effPropT
_ = try! await effPropAT
}
/////////////
// check subscripts in actors
actor SubscriptA {
subscript(_ i : Int) -> Int {
get async {
try thrower() // expected-error{{errors thrown from here are not handled}}
await asyncer()
return 0
}
}
func f() async {
// expected-error@+1{{expression is 'async' but is not marked with 'await'}} {{9-9=await }} expected-note@+1{{subscript access is 'async'}}
_ = self[0]
}
}
actor SubscriptT {
subscript(_ i : Int) -> Int {
get throws {
try thrower()
await asyncer() // expected-error{{'async' call in a function that does not support concurrency}}
return 0
}
}
func f() throws {
_ = try self[0]
// expected-note@+6 {{did you mean to handle error as optional value?}}
// expected-note@+5 {{did you mean to use 'try'?}}
// expected-note@+4 {{did you mean to disable error propagation?}}
// expected-error@+3 {{subscript access can throw but is not marked with 'try'}}
// expected-note@+2 {{call is to 'rethrows' function, but argument function can throw}}
// expected-error@+1 {{call can throw but is not marked with 'try'}}
_ = rethrower(self[1])
// expected-note@+2 {{call is to 'rethrows' function, but argument function can throw}}
// expected-error@+1 {{call can throw but is not marked with 'try'}}
_ = rethrower(try self[1])
_ = try rethrower(self[1])
_ = try rethrower(try self[1])
}
}
actor SubscriptAT {
subscript(_ i : Int) -> Int {
get async throws {
try thrower()
await asyncer()
return 0
}
}
func f() async throws {
_ = try await self[0]
}
}
func tryTheActorSubscripts(a : SubscriptA, t : SubscriptT, at : SubscriptAT) async throws {
// expected-error@+1 {{expression is 'async' but is not marked with 'await'}}{{7-7=await }} expected-note@+1 {{subscript access is 'async'}}
_ = a[0]
_ = await a[0]
// expected-note@+5{{did you mean to handle error as optional value?}}
// expected-note@+4{{did you mean to use 'try'?}}
// expected-note@+3{{did you mean to disable error propagation?}}
// expected-error@+2{{subscript access can throw but is not marked with 'try'}}
// expected-error@+1 {{expression is 'async' but is not marked with 'await'}}{{7-7=await }} expected-note@+1 {{subscript access is 'async'}}
_ = t[0]
_ = try await t[0]
_ = try! await t[0]
_ = try? await t[0]
// expected-note@+5{{did you mean to handle error as optional value?}}
// expected-note@+4{{did you mean to use 'try'?}}
// expected-note@+3{{did you mean to disable error propagation?}}
// expected-error@+2{{subscript access can throw but is not marked with 'try'}}
// expected-error@+1 {{expression is 'async' but is not marked with 'await'}}{{7-7=await }} expected-note@+1 {{subscript access is 'async'}}
_ = at[0]
_ = try await at[0]
}
|