1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
|
// RUN: %target-typecheck-verify-swift -enable-upcoming-feature InferSendableFromCaptures -strict-concurrency=complete -enable-upcoming-feature GlobalActorIsolatedTypesUsability
// REQUIRES: concurrency
// REQUIRES: asserts
class NonSendable : Hashable {
var data: Int
init(data: Int = 42) {
self.data = data
}
init(data: [Int]) {
self.data = data.first!
}
static func == (x: NonSendable, y: NonSendable) -> Bool { false }
func hash(into hasher: inout Hasher) {}
}
final class CondSendable<T> : Hashable {
init(_: T) {}
init(_: Int) {}
init(_: T, other: T = 42) {}
init<Q>(_: [Q] = []) {}
static func == (x: CondSendable, y: CondSendable) -> Bool { false }
func hash(into hasher: inout Hasher) {}
}
extension CondSendable : Sendable where T: Sendable {
}
// Test forming sendable key paths without context
do {
class K {
var data: String = ""
subscript<T>(_: T) -> Bool {
get { false }
}
subscript<Q>(_: Int, _: Q) -> Int {
get { 42 }
set {}
}
}
let kp = \K.data // Marked as `& Sendable`
let _: KeyPath<K, String> = kp // Ok
let _: KeyPath<K, String> & Sendable = kp // Ok
func test<V>(_: KeyPath<K, V> & Sendable) {
}
test(kp) // Ok
let nonSendableKP = \K.[NonSendable()]
let _: KeyPath<K, Bool> = \.[NonSendable()] // ok
let _: KeyPath<K, Bool> & Sendable = \.[NonSendable()] // expected-warning {{type 'KeyPath<K, Bool>' does not conform to the 'Sendable' protocol}}
let _: KeyPath<K, Int> & Sendable = \.[42, NonSendable(data: [-1, 0, 1])] // expected-warning {{type 'ReferenceWritableKeyPath<K, Int>' does not conform to the 'Sendable' protocol}}
let _: KeyPath<K, Int> & Sendable = \.[42, -1] // Ok
test(nonSendableKP) // expected-warning {{type 'KeyPath<K, Bool>' does not conform to the 'Sendable' protocol}}
}
// Test using sendable and non-sendable key paths.
do {
class V {
var i: Int = 0
subscript<T>(_: T) -> Int {
get { 42 }
}
subscript<Q>(_: Int, _: Q) -> Int {
get { 42 }
set {}
}
}
func testSendableKP<T, U>(v: T, _ kp: any KeyPath<T, U> & Sendable) {}
func testSendableFn<T, U>(v: T, _: @Sendable (T) -> U) {}
func testNonSendableKP<T, U>(v: T, _ kp: KeyPath<T, U>) {}
func testNonSendableFn<T, U>(v: T, _ kp: (T) -> U) {}
let v = V()
testSendableKP(v: v, \.i) // Ok
testSendableFn(v: v, \.i) // Ok
testSendableKP(v: v, \.[42]) // Ok
testSendableFn(v: v, \.[42]) // Ok
testSendableKP(v: v, \.[NonSendable()]) // expected-warning {{type 'KeyPath<V, Int>' does not conform to the 'Sendable' protocol}}
testSendableFn(v: v, \.[NonSendable()]) // expected-warning {{converting non-sendable function value to '@Sendable (V) -> Int' may introduce data races}}
testNonSendableKP(v: v, \.[NonSendable()]) // Ok
testNonSendableFn(v: v, \.[NonSendable()]) // Ok
let _: @Sendable (V) -> Int = \.[NonSendable()]
// expected-warning@-1 {{converting non-sendable function value to '@Sendable (V) -> Int' may introduce data races}}
let _: KeyPath<V, Int> & Sendable = \.[42, CondSendable(NonSendable(data: [1, 2, 3]))]
// expected-warning@-1 {{type 'ReferenceWritableKeyPath<V, Int>' does not conform to the 'Sendable' protocol}}
let _: KeyPath<V, Int> & Sendable = \.[42, CondSendable(42)] // Ok
struct Root {
let v: V
}
testSendableKP(v: v, \.[42, CondSendable(NonSendable(data: [1, 2, 3]))])
// expected-warning@-1 {{type 'ReferenceWritableKeyPath<V, Int>' does not conform to the 'Sendable' protocol}}
testSendableFn(v: v, \.[42, CondSendable(NonSendable(data: [1, 2, 3]))])
// expected-warning@-1 {{converting non-sendable function value to '@Sendable (V) -> Int' may introduce data races}}
testSendableKP(v: v, \.[42, CondSendable(42)]) // Ok
let nonSendable = NonSendable()
testSendableKP(v: v, \.[42, CondSendable(nonSendable)])
// expected-warning@-1 {{type 'ReferenceWritableKeyPath<V, Int>' does not conform to the 'Sendable' protocol}}
testSendableFn(v: v, \.[42, CondSendable(nonSendable)])
// expected-warning@-1 {{converting non-sendable function value to '@Sendable (V) -> Int' may introduce data races}}
}
// @dynamicMemberLookup with Sendable requirement
do {
@dynamicMemberLookup
struct Test<T> {
var obj: T
subscript<U>(dynamicMember member: KeyPath<T, U> & Sendable) -> U {
get { obj[keyPath: member] }
}
}
_ = Test(obj: "Hello").utf8.count // Ok
}
// Global actor isolated properties.
func testGlobalActorIsolatedReferences() {
@MainActor struct Isolated {
var data: Int = 42
subscript(v: Int) -> Bool { false }
}
let dataKP = \Isolated.data // Ok
let subscriptKP = \Isolated.[42]
// expected-warning@-1 {{cannot form key path to main actor-isolated subscript 'subscript(_:)'; this is an error in the Swift 6 language mode}}
let _: KeyPath<Isolated, Int> & Sendable = dataKP
// expected-warning@-1 {{type 'WritableKeyPath<Isolated, Int>' does not conform to the 'Sendable' protocol}}
let _: KeyPath<Isolated, Bool> & Sendable = subscriptKP
// expected-warning@-1 {{type 'KeyPath<Isolated, Bool>' does not conform to the 'Sendable' protocol}}
func testNonIsolated() {
_ = \Isolated.data // Ok
}
@MainActor func testIsolated() {
_ = \Isolated.data // Ok
}
}
@available(SwiftStdlib 5.1, *)
actor SomeActor {
}
@available(SwiftStdlib 5.1, *)
@globalActor
actor GlobalActor {
static let shared: SomeActor = SomeActor()
}
@available(SwiftStdlib 5.1, *)
func testReferencesToDifferentGlobalActorIsolatedMembers() {
struct Info {
@MainActor var name: String { "" }
}
struct Isolated {
@GlobalActor var info: Info { Info() }
}
@MainActor func testIsolatedToMain() {
_ = \Info.name // Ok
_ = \Isolated.info.name
// expected-warning@-1 {{cannot form key path to global actor 'GlobalActor'-isolated property 'info'; this is an error in the Swift 6 language mode}}
}
@GlobalActor func testIsolatedToCustom() {
_ = \Info.name // Ok
// expected-warning@-1 {{cannot form key path to main actor-isolated property 'name'; this is an error in the Swift 6 language mode}}
_ = \Isolated.info.name
// expected-warning@-1 {{cannot form key path to main actor-isolated property 'name'; this is an error in the Swift 6 language mode}}
}
}
do {
struct S {
var a: Int
var b: String?
}
func test<T: Sendable>(_: T) {}
let kp = [\S.a, \S.b]
test(kp) // Ok
test([\S.a, \S.b]) // Ok
let _: [PartialKeyPath<S>] = [\.a, \.b] // Ok
let _: [any PartialKeyPath<S> & Sendable] = [\.a, \.b] // Ok
}
do {
func kp() -> KeyPath<String, Int> & Sendable {
fatalError()
}
// TODO(rdar://125948508): This shouldn't be ambiguous (@Sendable version should be preferred)
func test() -> KeyPath<String, Int> {
true ? kp() : kp() // expected-error {{type of expression is ambiguous without a type annotation}}
}
func forward<T>(_ v: T) -> T { v }
// TODO(rdar://125948508): This shouldn't be ambiguous (@Sendable version should be preferred)
let _: KeyPath<String, Int> = forward(kp()) // expected-error {{conflicting arguments to generic parameter 'T' ('any KeyPath<String, Int> & Sendable' vs. 'KeyPath<String, Int>')}}
}
do {
final class C<T> {
let immutable: String = ""
}
_ = \C<Int>.immutable as? ReferenceWritableKeyPath // Ok
}
// Should be moved back to sendable_methods.swift once ambiguities are fixed
do {
struct Test {
static func fn() {}
static func otherFn() {}
}
// TODO(rdar://125948508): This shouldn't be ambiguous (@Sendable version should be preferred)
func fnRet(cond: Bool) -> () -> Void {
cond ? Test.fn : Test.otherFn // expected-error {{type of expression is ambiguous without a type annotation}}
}
func forward<T>(_: T) -> T {
}
// TODO(rdar://125948508): This shouldn't be ambiguous (@Sendable version should be preferred)
let _: () -> Void = forward(Test.fn) // expected-error {{conflicting arguments to generic parameter 'T' ('@Sendable () -> ()' vs. '() -> Void')}}
}
|