I can't explain why, but the error disappears if you capture the variable:
import os
@main
enum ScratchPad {
static func main () {
simple ()
}
static var lock = OSAllocatedUnfairLock ()
static func simple() {
var failures = [Int]()
// lock.withLock {print (failures.count) }
// Error: ^^^ Reference to captured var 'failures' in concurrently-executing code
lock.withLock { [failures] in print (failures.count) }
// Okay
}
}
Or if you do this:
import os
@main
enum ScratchPad {
static func main () {
simple ()
}
static var lock = OSAllocatedUnfairLock ()
class State {
var failures = [Int]()
}
static func simple() {
let state = State ()
lock.withLock {print (state.failures.count) }
}
}