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
|
// RUN: %empty-directory(%t)
// RUN: %target-build-swift -Xfrontend -disable-availability-checking -parse-as-library %s -o %t/a.out
// RUN: %target-codesign %t/a.out
// RUN: %env-SWIFT_IS_CURRENT_EXECUTOR_LEGACY_MODE_OVERRIDE=legacy %target-run %t/a.out
// REQUIRES: executable_test
// REQUIRES: concurrency
// REQUIRES: concurrency_runtime
// TODO: The actual reason is that we do these %env- tricks, which e.g. Windows is confused about
// REQUIRES: libdispatch
// UNSUPPORTED: back_deployment_runtime
// UNSUPPORTED: back_deploy_concurrency
// UNSUPPORTED: use_os_stdlib
// UNSUPPORTED: freestanding
// rdar://119743909 fails in optimize tests.
// UNSUPPORTED: swift_test_mode_optimize
// UNSUPPORTED: swift_test_mode_optimize_size
import StdlibUnittest
func checkPreconditionMainActor() /* synchronous! */ {
MainActor.shared.preconditionIsolated()
MainActor.preconditionIsolated()
// check for the existence of the assert version of APIs
MainActor.shared.assertIsolated()
MainActor.assertIsolated()
}
func checkPreconditionFamousActor() /* synchronous! */ {
FamousActor.shared.preconditionIsolated() // instance version for global actor
FamousActor.preconditionIsolated() // static version for global actor
}
@MainActor
func mainActorCallCheck() {
checkPreconditionMainActor()
}
@globalActor actor FamousActor: GlobalActor {
public static let shared = FamousActor()
func callCheckFamousActor() {
checkPreconditionFamousActor() // ok
}
func callCheckMainActor() {
checkPreconditionMainActor() // bad
}
}
actor MainFriend {
nonisolated var unownedExecutor: UnownedSerialExecutor {
MainActor.sharedUnownedExecutor
}
func callCheckMainActor() {
checkPreconditionMainActor()
}
}
actor Someone {
func callCheckMainActor() {
checkPreconditionMainActor()
}
}
@MainActor let global = TestStaticVar()
@MainActor
struct TestStaticVar {
@MainActor static let shared = TestStaticVar()
init() {
checkPreconditionMainActor()
}
}
@main struct Main {
static func main() async {
let tests = TestSuite("AssertPreconditionActorExecutor")
if #available(SwiftStdlib 5.9, *) {
// === MainActor --------------------------------------------------------
tests.test("precondition on actor (main): from 'main() async', with await") {
await checkPreconditionMainActor()
}
// FIXME: calling without await from main() should also succeed, we must set the executor while we're kicking off main
tests.test("MainActor.preconditionIsolated(): from Main friend") {
await MainFriend().callCheckMainActor()
}
tests.test("MainActor.assertIsolated() from static let initializer") {
_ = await TestStaticVar.shared
_ = await global
}
#if !os(WASI)
tests.test("precondition on actor (main): wrongly assume the main executor, from actor on other executor") {
expectCrashLater(withMessage: "Incorrect actor executor assumption; Expected 'MainActor' executor.")
await Someone().callCheckMainActor()
}
#endif
// === Global actor -----------------------------------------------------
tests.test("precondition on actor (main): assume FamousActor, from FamousActor") {
await FamousActor.shared.callCheckFamousActor()
}
}
await runAllTestsAsync()
}
}
|