Files
swift-mirror/test/Concurrency/async_let_isolation.swift
Doug Gregor a554ad632b [Concurrency] Diagnose mutating accesses to locals from concurrent code.
Replace the existing warning about any access to a local variable from
concurrently-executing code with a more tailored error:
concurrently-executing code may read a mutable varable, but cannot
modify it. This is safe so long as we either always do by-value
captures in concurrent closures or we ensure that no mutation of that
variable can occur after the point of capture.

We'll follow up with one of those. For now... be careful out there.

Since we're promoting this to an error, narrow it down to concurrent
closures and local functions, dropping the assumption that escaping
closures "may execute concurrently."
2021-01-29 14:27:24 -08:00

38 lines
1.2 KiB
Swift

// RUN: %target-typecheck-verify-swift -enable-experimental-concurrency
// REQUIRES: concurrency
actor class MyActor {
let immutable: Int = 17
var text: [String] = []
func synchronous() -> String { text.first ?? "nothing" } // expected-note 2 {{calls to instance method 'synchronous()' from outside of its actor context are implicitly asynchronous}}
func asynchronous() async -> String { synchronous() }
func testAsyncLetIsolation() async {
async let x = self.synchronous()
// expected-error @-1{{actor-isolated instance method 'synchronous()' cannot be referenced from 'async let' initializer}}
async let y = await self.asynchronous()
async let z = synchronous()
// expected-error @-1{{actor-isolated instance method 'synchronous()' cannot be referenced from 'async let' initializer}}
var localText = text
async let w = localText.removeLast()
// expected-error@-1{{mutation of captured var 'localText' in concurrently-executing code}}
_ = await x
_ = await y
_ = await z
_ = await w
}
}
func outside() async {
let a = MyActor()
async let x = a.synchronous() // okay, await is implicit
async let y = await a.synchronous()
_ = await x
_ = await y
}