Files
swift-mirror/test/Concurrency/Runtime/unspecified_is_main_actor.swift
Holly Borla 28948965dd [Concurrency] Two isolation inference changes for default isolation mode.
1. Non-actor initializers should participate in custom default isolation
   inference. Otherwise, they will not be able to touch the type's stored
   properties to initialize them if those properties have the default
   isolation inferred.
2. All declarations inside actors and distributed actors do not participate
   in default isolation inference; they remain unspecified, so the existing
   isolation inference rules apply.
2025-01-30 11:54:57 +00:00

95 lines
2.3 KiB
Swift

// RUN: %target-run-simple-swift( -Xfrontend -disable-availability-checking -swift-version 6 -g -import-objc-header %S/Inputs/RunOnMainActor.h %import-libdispatch -enable-experimental-feature UnspecifiedMeansMainActorIsolated )
// REQUIRES: executable_test
// REQUIRES: concurrency
// REQUIRES: concurrency_runtime
// REQUIRES: libdispatch
// REQUIRES: asserts
// REQUIRES: swift_feature_UnspecifiedMeansMainActorIsolated
// UNSUPPORTED: freestanding
// For now we do not support back deployment or use os stdlib
// UNSUPPORTED: back_deployment_concurrency
// UNSUPPORTED: use_os_stdlib
// Just a runtime test as a sanity check.
import StdlibUnittest
import Dispatch
////////////////////////
// MARK: Declarations //
////////////////////////
@_silgen_name("dispatch_assert_queue")
nonisolated func dispatch_assertQueue(_ ptr: UnsafeRawPointer)
nonisolated func checkIfOnMainQueue() {
dispatch_assertQueue(getDispatchMain())
}
actor Custom {
}
@globalActor
struct CustomActor {
static nonisolated var shared: Custom {
return Custom()
}
}
///////////////////////////////////////////
// MARK: Scaffolding/Testing Scaffolding //
///////////////////////////////////////////
let tests = TestSuite("UnspecifiedIsMainActor")
tests.test("checkIfOnMainQueue does not crash on the main queue") { @MainActor () -> () in
// Why do we crash if this is synchronous.
expectCrashLater()
checkIfOnMainQueue()
}
tests.test("checkIfOnMainQueue does not crash on the main queue") { @MainActor () async -> () in
checkIfOnMainQueue()
}
tests.test("checkIfOnMainQueue crashes off the main queue") {
expectCrashLater()
await { @CustomActor in
print("=> checkIfOnMainQueue crashes off the main queue")
checkIfOnMainQueue()
}()
}
tests.test("checkIfOnMainQueue crashes off the main queue 2") { @CustomActor () async -> () in
expectCrashLater()
print("=> checkIfOnMainQueue crashes off the main queue 2")
checkIfOnMainQueue()
}
/////////////////
// MARK: Tests //
/////////////////
class Klass {}
struct MainActorIsolated {
init() {
checkIfOnMainQueue()
}
func test() async {
checkIfOnMainQueue()
}
};
tests.test("callNominalType") { @CustomActor () -> () in
let x = await MainActorIsolated()
// We would crash without hopping here.
await x.test()
}
await runAllTestsAsync()