For Swift 5.7, I'm getting odd behaviour when I call Task.init
at the top-level. Here's an executable Swift package's main.swift
(Repository for source code):
actor CounterActor {
var count = 0
var maximum = 0
}
let counterActor = CounterActor()
Task.init {
await print(counterActor.count)
await print(counterActor.maximum)
}
Thread.sleep(forTimeInterval: 5)
It outputs nothing from swift run
no matter how much time we sleep for.
If we wrap the top-level code in a function, then it outputs quickly:
actor CounterActor { /* same as before */ }
func f() {
let counterActor = CounterActor()
Task {
await print(counterActor.count)
await print(counterActor.maximum)
}
Thread.sleep(forTimeInterval: 0.5)
}
f()
Thread.sleep(forTimeInterval: 0.1)
Am I doing something wrong or is this behaviour a regression? (It works as I would expect with Xcode 13.3.1)
Environment:
$ swift --version
swift-driver version: 1.55.1 Apple Swift version 5.7 (swiftlang-5.7.0.113.202 clang-1400.0.16.2)
Target: arm64-apple-macosx12.0
OS: macOS Monterey 12.4
Chip: Apple M1 Max