-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathobjc_bridging_generics.swift
318 lines (276 loc) · 12.4 KB
/
objc_bridging_generics.swift
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// RUN: %target-swift-frontend -sdk %S/../Inputs/clang-importer-sdk -I %S/../Inputs/clang-importer-sdk/swift-modules -enable-source-import -parse -parse-as-library -verify %s
// REQUIRES: objc_interop
import Foundation
import objc_generics
func testNSArrayBridging(_ hive: Hive) {
_ = hive.bees as [Bee]
}
func testNSDictionaryBridging(_ hive: Hive) {
_ = hive.beesByName as [String : Bee] // expected-error{{value of optional type '[String : Bee]?' not unwrapped; did you mean to use '!' or '?'?}}
var dict1 = hive.anythingToBees
let dict2: [NSObject : Bee] = dict1
dict1 = dict2
}
func testNSSetBridging(_ hive: Hive) {
_ = hive.allBees as Set<Bee>
}
public func expectType<T>(_: T.Type, _ x: inout T) {}
func testNSMutableDictionarySubscript(
_ dict: NSMutableDictionary, key: NSCopying, value: AnyObject) {
var oldValue = dict[key]
expectType(Optional<AnyObject>.self, &oldValue)
dict[key] = value
}
class C {}
struct S {}
func f(_ x: GenericClass<NSString>) -> NSString? { return x.thing() }
func f1(_ x: GenericClass<NSString>) -> NSString? { return x.otherThing() }
func f2(_ x: GenericClass<NSString>) -> Int32 { return x.count() }
func f3(_ x: GenericClass<NSString>) -> NSString? { return x.propertyThing }
func f4(_ x: GenericClass<NSString>) -> [NSString] { return x.arrayOfThings() }
func f5(_ x: GenericClass<C>) -> [C] { return x.arrayOfThings() }
func f6(_ x: GenericSubclass<NSString>) -> NSString? { return x.thing() }
func f6(_ x: GenericSubclass<C>) -> C? { return x.thing() }
func g() -> NSString? { return GenericClass<NSString>.classThing() }
func g1() -> NSString? { return GenericClass<NSString>.otherClassThing() }
func h(_ s: NSString?) -> GenericClass<NSString> {
return GenericClass(thing: s)
}
func j(_ x: GenericClass<NSString>?) {
takeGenericClass(x)
}
class Desk {}
class Rock: NSObject, Pettable {
required init(fur: AnyObject) {}
func other() -> Self { return self }
class func adopt() -> Self { fatalError("") }
func pet() {}
func pet(with other: Pettable) {}
class var needingMostPets: Pettable {
get { fatalError("") }
set { }
}
}
class Porcupine: Animal {
}
class Cat: Animal, Pettable {
required init(fur: AnyObject) {}
func other() -> Self { return self }
class func adopt() -> Self { fatalError("") }
func pet() {}
func pet(with other: Pettable) {}
class var needingMostPets: Pettable {
get { fatalError("") }
set { }
}
}
func testImportedTypeParamRequirements() {
let _ = PettableContainer<Desk>() // expected-error{{type 'Desk' does not conform to protocol 'Pettable'}}
let _ = PettableContainer<Rock>()
let _ = PettableContainer<Porcupine>() // expected-error{{type 'Porcupine' does not conform to protocol 'Pettable'}}
let _ = PettableContainer<Cat>()
let _ = AnimalContainer<Desk>() // expected-error{{'AnimalContainer' requires that 'Desk' inherit from 'Animal'}} expected-note{{requirement specified as 'T' : 'Animal' [with T = Desk]}}
let _ = AnimalContainer<Rock>() // expected-error{{'AnimalContainer' requires that 'Rock' inherit from 'Animal'}} expected-note{{requirement specified as 'T' : 'Animal' [with T = Rock]}}
let _ = AnimalContainer<Porcupine>()
let _ = AnimalContainer<Cat>()
let _ = PettableAnimalContainer<Desk>() // expected-error{{'PettableAnimalContainer' requires that 'Desk' inherit from 'Animal'}} expected-note{{requirement specified as 'T' : 'Animal' [with T = Desk]}}
let _ = PettableAnimalContainer<Rock>() // expected-error{{'PettableAnimalContainer' requires that 'Rock' inherit from 'Animal'}} expected-note{{requirement specified as 'T' : 'Animal' [with T = Rock]}}
let _ = PettableAnimalContainer<Porcupine>() // expected-error{{type 'Porcupine' does not conform to protocol 'Pettable'}}
let _ = PettableAnimalContainer<Cat>()
}
extension GenericClass {
func doesntUseGenericParam() {}
@objc func doesntUseGenericParam2() -> Self {}
// Doesn't use 'T', since ObjC class type params are type-erased
func doesntUseGenericParam3() -> GenericClass<T> {}
// Doesn't use 'T', since its metadata isn't necessary to pass around instance
@objc func doesntUseGenericParam4(_ x: T, _ y: T.Type) -> T {
_ = x
_ = y
return x
}
// Doesn't use 'T', since its metadata isn't necessary to erase to AnyObject
// or to existential metatype
func doesntUseGenericParam5(_ x: T, _ y: T.Type) -> T {
_ = y as AnyObject.Type
_ = y as Any.Type
_ = y as AnyObject
_ = x as AnyObject
}
// expected-error@+1{{extension of a generic Objective-C class cannot access the class's generic parameters}}
func usesGenericParamC(_ x: [(T, T)]?) {} // expected-note{{used here}}
// expected-error@+1{{extension of a generic Objective-C class cannot access the class's generic parameters}}
func usesGenericParamD(_ x: Int) {
_ = T.self // expected-note{{used here}}
}
// expected-error@+1{{extension of a generic Objective-C class cannot access the class's generic parameters}}
func usesGenericParamE(_ x: Int) {
_ = x as? T // expected-note{{used here}}
}
// expected-error@+1{{extension of a generic Objective-C class cannot access the class's generic parameters}}
func usesGenericParamF(_ x: Int) {
_ = x is T // expected-note{{used here}}
}
// expected-error@+1{{extension of a generic Objective-C class cannot access the class's generic parameters}}
func usesGenericParamG(_ x: T) {
_ = T.self // expected-note{{used here}}
}
// expected-error@+1{{extension of a generic Objective-C class cannot access the class's generic parameters}}
func usesGenericParamH(_ x: T) {
_ = x as Any // expected-note{{used here}}
}
// expected-error@+1{{extension of a generic Objective-C class cannot access the class's generic parameters}}
func usesGenericParamI(_ y: T.Type) {
_ = y as Any // expected-note{{used here}}
}
// expected-error@+1{{extension of a generic Objective-C class cannot access the class's generic parameters}}
func usesGenericParamJ() -> [(T, T)]? {} // expected-note{{used here}}
static func doesntUseGenericParam() {}
static func doesntUseGenericParam2() -> Self {}
// Doesn't technically use 'T', since it's type-erased at runtime
static func doesntUseGenericParam3() -> GenericClass<T> {}
// expected-error@+1{{extension of a generic Objective-C class cannot access the class's generic parameters}}
static func usesGenericParamC(_ x: [(T, T)]?) {} // expected-note{{used here}}
// expected-error@+1{{extension of a generic Objective-C class cannot access the class's generic parameters}}
static func usesGenericParamD(_ x: Int) {
_ = T.self // expected-note{{used here}}
}
// expected-error@+1{{extension of a generic Objective-C class cannot access the class's generic parameters}}
static func usesGenericParamE(_ x: Int) {
_ = x as? T // expected-note{{used here}}
}
// expected-error@+1{{extension of a generic Objective-C class cannot access the class's generic parameters}}
static func usesGenericParamF(_ x: Int) {
_ = x is T // expected-note{{used here}}
}
func checkThatMethodsAreObjC() {
_ = #selector(GenericClass.doesntUseGenericParam)
_ = #selector(GenericClass.doesntUseGenericParam2)
_ = #selector(GenericClass.doesntUseGenericParam3)
_ = #selector(GenericClass.doesntUseGenericParam4)
_ = #selector(GenericClass.doesntUseGenericParam5)
}
}
func swiftFunction<T: Animal>(x: T) {}
extension AnimalContainer {
func doesntUseGenericParam1(_ x: T, _ y: T.Type) {
_ = #selector(x.another)
_ = #selector(y.create)
}
func doesntUseGenericParam2(_ x: T, _ y: T.Type) {
let a = x.another()
_ = a.another()
_ = x.another().another()
_ = x.dynamicType.create().another()
_ = x.dynamicType.init(noise: x).another()
_ = y.create().another()
_ = y.init(noise: x).another()
_ = y.init(noise: x.another()).another()
x.eat(a)
}
func doesntUseGenericParam3(_ x: T, _ y: T.Type) {
let sup: Animal = x
sup.eat(x)
_ = x.buddy
_ = x[0]
x[0] = x
}
func doesntUseGenericParam4(_ x: T, _ y: T.Type) {
_ = x.dynamicType.apexPredator.another()
x.dynamicType.apexPredator = x
_ = y.apexPredator.another()
y.apexPredator = x
}
// expected-error@+1{{extension of a generic Objective-C class cannot access the class's generic parameters}}
func usesGenericParamA(_ x: T) {
_ = T(noise: x) // expected-note{{used here}}
}
// expected-error@+1{{extension of a generic Objective-C class cannot access the class's generic parameters}}
func usesGenericParamB() {
_ = T.create() // expected-note{{used here}}
}
// expected-error@+1{{extension of a generic Objective-C class cannot access the class's generic parameters}}
func usesGenericParamC() {
_ = T.apexPredator // expected-note{{used here}}
}
// expected-error@+1{{extension of a generic Objective-C class cannot access the class's generic parameters}}
func usesGenericParamD(_ x: T) {
T.apexPredator = x // expected-note{{used here}}
}
func checkThatMethodsAreObjC() {
_ = #selector(AnimalContainer.doesntUseGenericParam1)
_ = #selector(AnimalContainer.doesntUseGenericParam2)
_ = #selector(AnimalContainer.doesntUseGenericParam3)
_ = #selector(AnimalContainer.doesntUseGenericParam4)
}
// rdar://problem/26283886
func funcWithWrongArgType(x: NSObject) {}
func crashWithInvalidSubscript(x: NSArray) {
_ = funcWithWrongArgType(x: x[12])
// expected-error@-1{{'AnyObject' is not convertible to 'NSObject'; did you mean to use 'as!' to force downcast?}}
}
}
extension PettableContainer {
func doesntUseGenericParam(_ x: T, _ y: T.Type) {
_ = x.dynamicType.init(fur: x).other()
_ = x.dynamicType.adopt().other()
_ = y.init(fur: x).other()
_ = y.adopt().other()
x.pet()
x.pet(with: x)
}
// expected-error@+1{{extension of a generic Objective-C class cannot access the class's generic parameters}}
func usesGenericParamA(_ x: T) {
_ = T(fur: x) // expected-note{{used here}}
}
// expected-error@+1{{extension of a generic Objective-C class cannot access the class's generic parameters}}
func usesGenericParamB(_ x: T) {
_ = T.adopt() // expected-note{{used here}}
}
// expected-error@+1{{extension of a generic Objective-C class cannot access the class's generic parameters}}
func usesGenericParamC(_ x: T) {
_ = T.needingMostPets // expected-note{{used here}}
}
// expected-error@+1{{extension of a generic Objective-C class cannot access the class's generic parameters}}
func usesGenericParamD(_ x: T) {
T.needingMostPets = x // expected-note{{used here}}
}
func checkThatMethodsAreObjC() {
_ = #selector(PettableContainer.doesntUseGenericParam)
}
}
// expected-error@+1{{inheritance from a generic Objective-C class 'GenericClass' must bind type parameters of 'GenericClass' to specific concrete types}}
class SwiftGenericSubclassA<X: AnyObject>: GenericClass<X> {}
// expected-error@+1{{inheritance from a generic Objective-C class 'GenericClass' must bind type parameters of 'GenericClass' to specific concrete types}}
class SwiftGenericSubclassB<X: AnyObject>: GenericClass<GenericClass<X>> {}
// expected-error@+1{{inheritance from a generic Objective-C class 'GenericClass' must bind type parameters of 'GenericClass' to specific concrete types}}
class SwiftGenericSubclassC<X: NSCopying>: GenericClass<X> {}
class SwiftConcreteSubclassA: GenericClass<AnyObject> {
override init(thing: AnyObject) { }
override func thing() -> AnyObject? { }
override func count() -> Int32 { }
override class func classThing() -> AnyObject? { }
override func arrayOfThings() -> [AnyObject] {}
}
class SwiftConcreteSubclassB: GenericClass<NSString> {
override init(thing: NSString) { }
override func thing() -> NSString? { }
override func count() -> Int32 { }
override class func classThing() -> NSString? { }
override func arrayOfThings() -> [NSString] {}
}
class SwiftConcreteSubclassC<T>: GenericClass<NSString> {
override init(thing: NSString) { }
override func thing() -> NSString? { }
override func count() -> Int32 { }
override class func classThing() -> NSString? { }
override func arrayOfThings() -> [NSString] {}
}
// FIXME: Some generic ObjC APIs rely on covariance. We don't handle this well
// in Swift yet, but ensure we don't emit spurious warnings when
// `as!` is used to force types to line up.
func foo(x: GenericClass<NSMutableString>) {
let x2 = x as! GenericClass<NSString>
takeGenericClass(x2)
takeGenericClass(unsafeBitCast(x, to: GenericClass<NSString>.self))
}