|
| 1 | +// 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 ) |
| 2 | + |
| 3 | +// REQUIRES: executable_test |
| 4 | +// REQUIRES: concurrency |
| 5 | +// REQUIRES: concurrency_runtime |
| 6 | +// REQUIRES: libdispatch |
| 7 | +// REQUIRES: asserts |
| 8 | +// REQUIRES: swift_feature_UnspecifiedMeansMainActorIsolated |
| 9 | + |
| 10 | +// UNSUPPORTED: freestanding |
| 11 | + |
| 12 | +// For now we do not support back deployment or use os stdlib |
| 13 | +// UNSUPPORTED: back_deployment_concurrency |
| 14 | +// UNSUPPORTED: use_os_stdlib |
| 15 | + |
| 16 | +// Just a runtime test as a sanity check. |
| 17 | + |
| 18 | +import StdlibUnittest |
| 19 | +import Dispatch |
| 20 | + |
| 21 | +//////////////////////// |
| 22 | +// MARK: Declarations // |
| 23 | +//////////////////////// |
| 24 | + |
| 25 | +@_silgen_name("dispatch_assert_queue") |
| 26 | +nonisolated func dispatch_assertQueue(_ ptr: UnsafeRawPointer) |
| 27 | + |
| 28 | +nonisolated func checkIfOnMainQueue() { |
| 29 | + dispatch_assertQueue(getDispatchMain()) |
| 30 | +} |
| 31 | + |
| 32 | +actor Custom { |
| 33 | +} |
| 34 | + |
| 35 | +@globalActor |
| 36 | +struct CustomActor { |
| 37 | + static nonisolated var shared: Custom { |
| 38 | + return Custom() |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +/////////////////////////////////////////// |
| 43 | +// MARK: Scaffolding/Testing Scaffolding // |
| 44 | +/////////////////////////////////////////// |
| 45 | + |
| 46 | +let tests = TestSuite("UnspecifiedIsMainActor") |
| 47 | + |
| 48 | +tests.test("checkIfOnMainQueue does not crash on the main queue") { @MainActor () -> () in |
| 49 | + // Why do we crash if this is synchronous. |
| 50 | + expectCrashLater() |
| 51 | + checkIfOnMainQueue() |
| 52 | +} |
| 53 | + |
| 54 | +tests.test("checkIfOnMainQueue does not crash on the main queue") { @MainActor () async -> () in |
| 55 | + checkIfOnMainQueue() |
| 56 | +} |
| 57 | + |
| 58 | +tests.test("checkIfOnMainQueue crashes off the main queue") { |
| 59 | + expectCrashLater() |
| 60 | + await { @CustomActor in |
| 61 | + print("=> checkIfOnMainQueue crashes off the main queue") |
| 62 | + checkIfOnMainQueue() |
| 63 | + }() |
| 64 | +} |
| 65 | + |
| 66 | +tests.test("checkIfOnMainQueue crashes off the main queue 2") { @CustomActor () async -> () in |
| 67 | + expectCrashLater() |
| 68 | + print("=> checkIfOnMainQueue crashes off the main queue 2") |
| 69 | + checkIfOnMainQueue() |
| 70 | +} |
| 71 | + |
| 72 | +///////////////// |
| 73 | +// MARK: Tests // |
| 74 | +///////////////// |
| 75 | + |
| 76 | +class Klass {} |
| 77 | + |
| 78 | +struct MainActorIsolated { |
| 79 | + init() {} |
| 80 | + |
| 81 | + func test() async { |
| 82 | + checkIfOnMainQueue() |
| 83 | + } |
| 84 | +}; |
| 85 | + |
| 86 | +tests.test("callNominalType") { @CustomActor () -> () in |
| 87 | + let x = MainActorIsolated() |
| 88 | + // We would crash without hopping here. |
| 89 | + await x.test() |
| 90 | +} |
| 91 | + |
| 92 | +await runAllTestsAsync() |
0 commit comments