|
| 1 | +// RUN: %target-swift-frontend -target %target-swift-5.1-abi-triple -swift-version 6 -parse-as-library %s -emit-sil -o /dev/null -verify |
| 2 | + |
| 3 | +// REQUIRES: asserts |
| 4 | +// REQUIRES: concurrency |
| 5 | + |
| 6 | +/////////////////////// |
| 7 | +// MARK: Declaration // |
| 8 | +/////////////////////// |
| 9 | + |
| 10 | +actor Custom { |
| 11 | +} |
| 12 | + |
| 13 | +@globalActor |
| 14 | +struct CustomActor { |
| 15 | + static var shared: Custom { |
| 16 | + return Custom() |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +class NonSendable {} // expected-note 3{{}} |
| 21 | + |
| 22 | +func passNonSendable(_: NonSendable) async { } |
| 23 | + |
| 24 | +func returnsNonSendable() async -> NonSendable { NonSendable() } |
| 25 | + |
| 26 | +@MainActor |
| 27 | +func mainActorPassNonSendable(_: NonSendable) async { } |
| 28 | + |
| 29 | +@MainActor |
| 30 | +func mainActorReturnNonSendable() async -> NonSendable { NonSendable() } |
| 31 | + |
| 32 | +@MainActor |
| 33 | +func mainActorGenericPassNonSendable<T>(_: T) async { } |
| 34 | + |
| 35 | +@MainActor |
| 36 | +func mainActorGenericReturnNonSendable<T>() async -> T { fatalError() } |
| 37 | + |
| 38 | +@MainActor |
| 39 | +func mainActorAsyncFunc3() async -> ((Int) -> Int) { |
| 40 | + return { (_ y: Int) in y } |
| 41 | +} |
| 42 | + |
| 43 | +///////////////// |
| 44 | +// MARK: Tests // |
| 45 | +///////////////// |
| 46 | + |
| 47 | +@MainActor func mainActorResult(_ x : Int) -> ((Int) -> Int) { |
| 48 | + return { (_ y : Int) in x + y } |
| 49 | +} |
| 50 | + |
| 51 | +actor Calculator { |
| 52 | + func addCurried(_ x : Int) -> ((Int) -> Int) { |
| 53 | + return { (_ y : Int) in x + y } |
| 54 | + } |
| 55 | + |
| 56 | + func add(_ x : Int, _ y : Int) -> Int { |
| 57 | + return x + y |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +@CustomActor |
| 62 | +func testActorCrossingBoundary() async { |
| 63 | + let _ = (await mainActorResult(1))(5) |
| 64 | + // expected-error @-1 {{non-Sendable '(Int) -> Int'-typed result can not be returned from main actor-isolated global function 'mainActorResult' to global actor 'CustomActor'-isolated context}} |
| 65 | + // expected-note @-2 {{a function type must be marked '@Sendable' to conform to 'Sendable'}} |
| 66 | + let _ = await (await mainActorResult(1))(2) |
| 67 | + // expected-error @-1 {{non-Sendable '(Int) -> Int'-typed result can not be returned from main actor-isolated global function 'mainActorResult' to global actor 'CustomActor'-isolated context}} |
| 68 | + // expected-note @-2 {{a function type must be marked '@Sendable' to conform to 'Sendable'}} |
| 69 | + // expected-warning @-3 {{no 'async' operations occur within 'await' expression}} |
| 70 | + |
| 71 | + let calc = Calculator() |
| 72 | + |
| 73 | + let _ = (await calc.addCurried(1))(2) |
| 74 | + // expected-error @-1 {{non-Sendable '(Int) -> Int'-typed result can not be returned from actor-isolated instance method 'addCurried' to global actor 'CustomActor'-isolated context}} |
| 75 | + // expected-note@-2{{a function type must be marked '@Sendable' to conform to 'Sendable'}} |
| 76 | + let _ = await (await calc.addCurried(1))(2) // expected-warning{{no 'async' operations occur within 'await' expression}} |
| 77 | + // expected-error @-1 {{non-Sendable '(Int) -> Int'-typed result can not be returned from actor-isolated instance method 'addCurried' to global actor 'CustomActor'-isolated context}} |
| 78 | + // expected-note @-2 {{a function type must be marked '@Sendable' to conform to 'Sendable'}} |
| 79 | + |
| 80 | + let plusOne = await calc.addCurried(await calc.add(0, 1)) |
| 81 | + // expected-error @-1 {{non-Sendable '(Int) -> Int'-typed result can not be returned from actor-isolated instance method 'addCurried' to global actor 'CustomActor'-isolated context}} |
| 82 | + // expected-note @-2 {{a function type must be marked '@Sendable' to conform to 'Sendable'}} |
| 83 | + let _ = plusOne(2) |
| 84 | +} |
| 85 | + |
| 86 | +actor A { |
| 87 | + let actorNS = NonSendable() |
| 88 | + |
| 89 | + func actorTakesNS(_ : NonSendable) async {} |
| 90 | + |
| 91 | + func actorRetsNS() async -> NonSendable { NonSendable() } |
| 92 | + |
| 93 | + func callNonisolatedFuncsFromActor(ns: NonSendable) async { |
| 94 | + // Non-sendable value passed from nonisolated to actor isolated |
| 95 | + |
| 96 | + await passNonSendable(ns) |
| 97 | + // expected-error @-1 {{sending 'ns' risks causing data races}} |
| 98 | + // expected-note @-2 {{sending 'self'-isolated 'ns' to nonisolated global function 'passNonSendable' risks causing data races between nonisolated and 'self'-isolated uses}} |
| 99 | + |
| 100 | + _ = await returnsNonSendable() |
| 101 | + } |
| 102 | + |
| 103 | + func callActorFuncsFromDiffActor(ns : NonSendable, a : A) async { |
| 104 | + // Non-sendable value passed between the isolation of two different actors |
| 105 | + |
| 106 | + await a.actorTakesNS(ns) |
| 107 | + // expected-error @-1 {{sending 'ns' risks causing data races}} |
| 108 | + // expected-note @-2 {{sending 'self'-isolated 'ns' to actor-isolated instance method 'actorTakesNS' risks causing data races between actor-isolated and 'self'-isolated uses}} |
| 109 | + |
| 110 | + _ = await a.actorRetsNS() |
| 111 | + // expected-error @-1 {{non-Sendable 'NonSendable'-typed result can not be returned from actor-isolated instance method 'actorRetsNS()' to actor-isolated context}} |
| 112 | + } |
| 113 | + |
| 114 | + func validateErrorForPassingIsolatedNonSendable(_ ns: NonSendable) async { |
| 115 | + await mainActorGenericPassNonSendable(ns) |
| 116 | + // expected-error @-1 {{sending 'ns' risks causing data races}} |
| 117 | + // expected-note @-2 {{sending 'self'-isolated 'ns' to main actor-isolated global function 'mainActorGenericPassNonSendable' risks causing data races between main actor-isolated and 'self'-isolated uses}} |
| 118 | + } |
| 119 | + |
| 120 | + func validateErrorReturningFromNonIsolated() async { |
| 121 | + let _ = await returnsNonSendable() |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +func callActorFuncsFromNonisolated(a : A, ns : NonSendable) async { |
| 126 | + await a.actorTakesNS(ns) |
| 127 | + // expected-error @-1 {{sending 'ns' risks causing data races}} |
| 128 | + // expected-note @-2 {{sending task-isolated 'ns' to actor-isolated instance method 'actorTakesNS' risks causing data races between actor-isolated and task-isolated uses}} |
| 129 | + |
| 130 | + _ = await a.actorRetsNS() |
| 131 | + // expected-error @-1 {{non-Sendable 'NonSendable'-typed result can not be returned from actor-isolated instance method 'actorRetsNS()' to nonisolated context}} |
| 132 | +} |
| 133 | + |
| 134 | +func testGenericResults() async { |
| 135 | + let _: NonSendable = await mainActorGenericReturnNonSendable() |
| 136 | + // expected-error @-1 {{non-Sendable 'NonSendable'-typed result can not be returned from main actor-isolated global function 'mainActorGenericReturnNonSendable()' to nonisolated context}} |
| 137 | +} |
0 commit comments