|
| 1 | +// RUN: %target-swift-frontend -disable-availability-checking -swift-version 6 -enable-experimental-feature GlobalActorInferenceCutoff -parse-as-library %s -emit-sil -o /dev/null -verify -strict-concurrency=complete |
| 2 | + |
| 3 | +// REQUIRES: concurrency |
| 4 | +// REQUIRES: asserts |
| 5 | + |
| 6 | +@MainActor |
| 7 | +protocol GloballyIsolated {} |
| 8 | + |
| 9 | +// expected-note@+1 {{class 'NonSendable' does not conform to the 'Sendable' protocol}} |
| 10 | +class NonSendable {} |
| 11 | + |
| 12 | +@propertyWrapper struct P { |
| 13 | + var wrappedValue = 0 |
| 14 | +} |
| 15 | + |
| 16 | +// MARK: - Structs |
| 17 | + |
| 18 | +struct ImplicitlySendable { |
| 19 | + var a: Int |
| 20 | + |
| 21 | + // always okay |
| 22 | + nonisolated let b = 0 |
| 23 | + nonisolated var c: Int { 0 } |
| 24 | + nonisolated var d = 0 |
| 25 | + |
| 26 | + // never okay |
| 27 | + nonisolated lazy var e = 0 // expected-error {{'nonisolated' is not supported on lazy properties}} |
| 28 | + @P nonisolated var f = 0 // expected-error {{'nonisolated' is not supported on properties with property wrappers}} |
| 29 | +} |
| 30 | + |
| 31 | +struct ImplicitlyNonSendable { |
| 32 | + let a: NonSendable |
| 33 | + |
| 34 | + // always okay |
| 35 | + nonisolated let b = 0 |
| 36 | + nonisolated var c: Int { 0 } |
| 37 | + nonisolated var d = 0 |
| 38 | + |
| 39 | + // never okay |
| 40 | + nonisolated lazy var e = 0 // expected-error {{'nonisolated' is not supported on lazy properties}} |
| 41 | + @P nonisolated var f = 0 // expected-error {{'nonisolated' is not supported on properties with property wrappers}} |
| 42 | +} |
| 43 | + |
| 44 | +public struct PublicSendable: Sendable { |
| 45 | + // always okay |
| 46 | + nonisolated let b = 0 |
| 47 | + nonisolated var c: Int { 0 } |
| 48 | + nonisolated var d = 0 |
| 49 | + |
| 50 | + // never okay |
| 51 | + nonisolated lazy var e = 0 // expected-error {{'nonisolated' is not supported on lazy properties}} |
| 52 | + @P nonisolated var f = 0 // expected-error {{'nonisolated' is not supported on properties with property wrappers}} |
| 53 | +} |
| 54 | + |
| 55 | +public struct PublicNonSendable { |
| 56 | + // always okay |
| 57 | + nonisolated let b = 0 |
| 58 | + nonisolated var c: Int { 0 } |
| 59 | + nonisolated var d = 0 |
| 60 | + |
| 61 | + // never okay |
| 62 | + nonisolated lazy var e = 0 // expected-error {{'nonisolated' is not supported on lazy properties}} |
| 63 | + @P nonisolated var f = 0 // expected-error {{'nonisolated' is not supported on properties with property wrappers}} |
| 64 | +} |
| 65 | + |
| 66 | + |
| 67 | +nonisolated struct NonisolatedStruct: GloballyIsolated { |
| 68 | + var x: NonSendable |
| 69 | + var y: Int = 1 |
| 70 | + |
| 71 | + init(x: NonSendable) { |
| 72 | + self.x = x // okay |
| 73 | + } |
| 74 | + |
| 75 | + struct Nested: GloballyIsolated { |
| 76 | + // expected-note@+1 {{mutation of this property is only permitted within the actor}} |
| 77 | + var z: NonSendable |
| 78 | + nonisolated init(z: NonSendable) { |
| 79 | + // expected-error@+1 {{main actor-isolated property 'z' can not be mutated from a nonisolated context}} |
| 80 | + self.z = z |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +@MainActor struct S { |
| 86 | + var value: NonSendable // globally-isolated |
| 87 | + struct Nested {} // 'Nested' is not @MainActor-isolated |
| 88 | +} |
| 89 | + |
| 90 | +// expected-note@+1 {{calls to global function 'requireMain()' from outside of its actor context are implicitly asynchronous}} |
| 91 | +@MainActor func requireMain() {} |
| 92 | + |
| 93 | +nonisolated struct S1: GloballyIsolated { |
| 94 | + var x: NonSendable |
| 95 | + func f() { |
| 96 | + // expected-error@+1 {{call to main actor-isolated global function 'requireMain()' in a synchronous nonisolated context}} |
| 97 | + requireMain() |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +// MARK: - Protocols |
| 102 | + |
| 103 | +nonisolated protocol Refined: GloballyIsolated {} |
| 104 | + |
| 105 | +struct A: Refined { |
| 106 | + var x: NonSendable |
| 107 | + init(x: NonSendable) { |
| 108 | + self.x = x // okay |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +// MARK: - Extensions |
| 113 | + |
| 114 | +nonisolated extension GloballyIsolated { |
| 115 | + var x: NonSendable { .init () } |
| 116 | + func implicitlyNonisolated() {} |
| 117 | +} |
| 118 | + |
| 119 | +struct C: GloballyIsolated { |
| 120 | + nonisolated func explicitlyNonisolated() { |
| 121 | + let _ = x // okay |
| 122 | + implicitlyNonisolated() // okay |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +// MARK: - Enums |
| 127 | + |
| 128 | +nonisolated enum E: GloballyIsolated { |
| 129 | + func implicitlyNonisolated() {} |
| 130 | + init() {} |
| 131 | +} |
| 132 | + |
| 133 | +struct TestEnum { |
| 134 | + nonisolated func call() { |
| 135 | + E().implicitlyNonisolated() // okay |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +// MARK: - Classes |
| 140 | + |
| 141 | +nonisolated class K: GloballyIsolated { |
| 142 | + var x: NonSendable |
| 143 | + init(x: NonSendable) { |
| 144 | + self.x = x // okay |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +// MARK: - Storage of non-Sendable |
| 149 | + |
| 150 | +class KlassA { |
| 151 | + nonisolated var test: NonSendable = NonSendable() |
| 152 | +} |
| 153 | + |
| 154 | +// MARK: - Restrictions |
| 155 | + |
| 156 | +@MainActor |
| 157 | +nonisolated struct Conflict {} |
| 158 | +// expected-error@-1 {{struct 'Conflict' has multiple actor-isolation attributes ('nonisolated' and 'MainActor')}} |
| 159 | + |
| 160 | +struct B: Sendable { |
| 161 | + // expected-error@+1 {{'nonisolated' can not be applied to variable with non-'Sendable' type 'NonSendable}} |
| 162 | + nonisolated let test: NonSendable |
| 163 | +} |
| 164 | + |
| 165 | +final class KlassB: Sendable { |
| 166 | + // expected-note@+2 {{convert 'test' to a 'let' constant or consider declaring it 'nonisolated(unsafe)' if manually managing concurrency safety}} |
| 167 | + // expected-error@+1 {{'nonisolated' cannot be applied to mutable stored properties}} |
| 168 | + nonisolated var test: Int = 1 |
| 169 | +} |
0 commit comments