|
| 1 | +// RUN: %target-typecheck-verify-swift -disable-availability-checking -swift-version 6 |
| 2 | +// REQUIRES: concurrency |
| 3 | + |
| 4 | +@_predatesConcurrency func unsafelySendableClosure(_ closure: @Sendable () -> Void) { } |
| 5 | + |
| 6 | +@_predatesConcurrency func unsafelyMainActorClosure(_ closure: @MainActor () -> Void) { } |
| 7 | + |
| 8 | +@_predatesConcurrency func unsafelyDoEverythingClosure(_ closure: @MainActor @Sendable () -> Void) { } |
| 9 | + |
| 10 | +struct X { |
| 11 | + @_predatesConcurrency func unsafelyDoEverythingClosure(_ closure: @MainActor @Sendable () -> Void) { } |
| 12 | + |
| 13 | + @_predatesConcurrency var sendableVar: @Sendable () -> Void |
| 14 | + @_predatesConcurrency var mainActorVar: @MainActor () -> Void |
| 15 | + |
| 16 | + @_predatesConcurrency |
| 17 | + subscript(_: @MainActor () -> Void) -> (@Sendable () -> Void) { {} } |
| 18 | + |
| 19 | + @_predatesConcurrency |
| 20 | + static subscript(statically _: @MainActor () -> Void) -> (@Sendable () -> Void) { { } } |
| 21 | +} |
| 22 | + |
| 23 | +@MainActor func onMainActor() { } |
| 24 | + |
| 25 | +func testInAsync(x: X) async { |
| 26 | + let _: Int = unsafelySendableClosure // expected-error{{type '(@Sendable () -> Void) -> ()'}} |
| 27 | + let _: Int = unsafelyMainActorClosure // expected-error{{type '(@MainActor () -> Void) -> ()'}} |
| 28 | + let _: Int = unsafelyDoEverythingClosure // expected-error{{type '(@MainActor @Sendable () -> Void) -> ()'}} |
| 29 | + let _: Int = x.unsafelyDoEverythingClosure // expected-error{{type '(@MainActor @Sendable () -> Void) -> ()'}} |
| 30 | + let _: Int = X.unsafelyDoEverythingClosure // expected-error{{type '(X) -> (@MainActor @Sendable () -> Void) -> ()'}} |
| 31 | + let _: Int = (X.unsafelyDoEverythingClosure)(x) // expected-error{{type '(@MainActor @Sendable () -> Void) -> ()'}} |
| 32 | + |
| 33 | + let _: Int = x.sendableVar // expected-error{{type '@Sendable () -> Void'}} |
| 34 | + let _: Int = x.mainActorVar // expected-error{{type '@MainActor () -> Void'}} |
| 35 | + |
| 36 | + let _: Int = x[{ onMainActor() }] // expected-error{{type '@Sendable () -> Void'}} |
| 37 | + let _: Int = X[statically: { onMainActor() }] // expected-error{{type '@Sendable () -> Void'}} |
| 38 | +} |
| 39 | + |
| 40 | +func testElsewhere(x: X) { |
| 41 | + let _: Int = unsafelySendableClosure // expected-error{{type '(@Sendable () -> Void) -> ()'}} |
| 42 | + let _: Int = unsafelyMainActorClosure // expected-error{{type '(@MainActor () -> Void) -> ()'}} |
| 43 | + let _: Int = unsafelyDoEverythingClosure // expected-error{{type '(@MainActor @Sendable () -> Void) -> ()'}} |
| 44 | + let _: Int = x.unsafelyDoEverythingClosure // expected-error{{type '(@MainActor @Sendable () -> Void) -> ()'}} |
| 45 | + let _: Int = X.unsafelyDoEverythingClosure // expected-error{{type '(X) -> (@MainActor @Sendable () -> Void) -> ()'}} |
| 46 | + let _: Int = (X.unsafelyDoEverythingClosure)(x) // expected-error{{type '(@MainActor @Sendable () -> Void) -> ()'}} |
| 47 | + |
| 48 | + let _: Int = x.sendableVar // expected-error{{type '@Sendable () -> Void'}} |
| 49 | + let _: Int = x.mainActorVar // expected-error{{type '@MainActor () -> Void'}} |
| 50 | + |
| 51 | + let _: Int = x[{ onMainActor() }] // expected-error{{type '@Sendable () -> Void'}} |
| 52 | + let _: Int = X[statically: { onMainActor() }] // expected-error{{type '@Sendable () -> Void'}} |
| 53 | +} |
| 54 | + |
| 55 | +@MainActor @_predatesConcurrency func onMainActorAlways() { } |
| 56 | +// expected-note@-1{{are implicitly asynchronous}} |
| 57 | + |
| 58 | +@_predatesConcurrency @MainActor class MyModelClass { |
| 59 | + // expected-note@-1{{are implicitly asynchronous}} |
| 60 | + func f() { } |
| 61 | + // expected-note@-1{{are implicitly asynchronous}} |
| 62 | +} |
| 63 | + |
| 64 | +func testCalls(x: X) { |
| 65 | + // expected-note@-1 3{{add '@MainActor' to make global function 'testCalls(x:)' part of global actor 'MainActor'}} |
| 66 | + onMainActorAlways() // expected-error{{call to main actor-isolated global function 'onMainActorAlways()' in a synchronous nonisolated context}} |
| 67 | + |
| 68 | + let _: () -> Void = onMainActorAlways // expected-error{{converting function value of type '@MainActor () -> ()' to '() -> Void' loses global actor 'MainActor'}} |
| 69 | + |
| 70 | + let c = MyModelClass() // expected-error{{call to main actor-isolated initializer 'init()' in a synchronous nonisolated context}} |
| 71 | + c.f() // expected-error{{call to main actor-isolated instance method 'f()' in a synchronous nonisolated context}} |
| 72 | +} |
| 73 | + |
| 74 | +func testCallsWithAsync() async { |
| 75 | + onMainActorAlways() // expected-error{{expression is 'async' but is not marked with 'await'}} |
| 76 | + // expected-note@-1{{calls to global function 'onMainActorAlways()' from outside of its actor context are implicitly asynchronous}} |
| 77 | + |
| 78 | + let _: () -> Void = onMainActorAlways // expected-error{{converting function value of type '@MainActor () -> ()' to '() -> Void' loses global actor 'MainActor'}} |
| 79 | + |
| 80 | + let c = MyModelClass() // expected-error{{expression is 'async' but is not marked with 'await'}} |
| 81 | + // expected-note@-1{{calls to initializer 'init()' from outside of its actor context are implicitly asynchronous}} |
| 82 | + c.f() // expected-error{{expression is 'async' but is not marked with 'await'}} |
| 83 | + // expected-note@-1{{calls to instance method 'f()' from outside of its actor context are implicitly asynchronous}} |
| 84 | +} |
| 85 | + |
| 86 | +// --------------------------------------------------------------------------- |
| 87 | +// Protocols that inherit Sendable and predate concurrency. |
| 88 | +// --------------------------------------------------------------------------- |
| 89 | +@_predatesConcurrency protocol P: Sendable { } |
| 90 | +protocol Q: P { } |
| 91 | + |
| 92 | +class NS { } // expected-note 3{{class 'NS' does not conform to the 'Sendable' protocol}} |
| 93 | + |
| 94 | +struct S1: P { |
| 95 | + var ns: NS // expected-error{{stored property 'ns' of 'Sendable'-conforming struct 'S1' has non-sendable type 'NS'}} |
| 96 | +} |
| 97 | + |
| 98 | +struct S2: Q { |
| 99 | + var ns: NS // expected-error{{stored property 'ns' of 'Sendable'-conforming struct 'S2' has non-sendable type 'NS'}} |
| 100 | +} |
| 101 | + |
| 102 | +struct S3: Q, Sendable { |
| 103 | + var ns: NS // expected-error{{stored property 'ns' of 'Sendable'-conforming struct 'S3' has non-sendable type 'NS'}} |
| 104 | +} |
| 105 | + |
| 106 | +// --------------------------------------------------------------------------- |
| 107 | +// Historical attribute names do nothing (but are permitted) |
| 108 | +// --------------------------------------------------------------------------- |
| 109 | +func aFailedExperiment(@_unsafeSendable _ body: @escaping () -> Void) { } |
| 110 | +// expected-warning@-1{{'_unsafeSendable' attribute has been removed in favor of @_predatesConcurrency}} |
| 111 | + |
| 112 | +func anothingFailedExperiment(@_unsafeMainActor _ body: @escaping () -> Void) { } |
| 113 | +// expected-warning@-1{{'_unsafeMainActor' attribute has been removed in favor of @_predatesConcurrency}} |
0 commit comments