-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathobjc_parse.swift
461 lines (363 loc) · 14.8 KB
/
objc_parse.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
// RUN: %swift %clang-importer-sdk -emit-sil -I %S/Inputs/custom-modules -target x86_64-apple-macosx10.9 %s -verify
import AppKit
import AVFoundation
import objc_ext
import TestProtocols
import ObjCParseExtras
import ObjCParseExtrasToo
func testAnyObject(obj: AnyObject) {
var optStr = obj.nsstringProperty
}
// Construction
func construction() {
var b = B()
}
// Subtyping
func treatBAsA(b: B) -> A {
return b
}
// Instance method invocation
func instanceMethods(b: B) {
var i = b.method(1, withFloat:2.5)
i = i + b.method(1, withDouble:2.5)
// BOOL
b.setEnabled(true)
// SEL
b.performSelector("isEqual:", withObject:b) // expected-error {{'performSelector(_:withObject:)' is unavailable: 'performSelector' methods are unavailable}}
// Renaming of redundant parameters.
b.performAdd(1, withValue:2, withValue2:3, withValue:4) // expected-error{{argument 'withValue' must precede argument 'withValue2'}}
b.performAdd(1, withValue:2, withValue:4, withValue2: 3)
b.performAdd(1, 2, 3, 4) // expected-error{{missing argument labels 'withValue:withValue:withValue2:' in call}}
// Both class and instance methods exist.
b.description
b.instanceTakesObjectClassTakesFloat(b)
b.instanceTakesObjectClassTakesFloat(2.0) // expected-error{{cannot invoke 'instanceTakesObjectClassTakesFloat' with an argument list of type '(Double)'}}
// Instance methods with keyword components
var obj = NSObject()
var prot = NSObjectProtocol.self
b.`protocol`(prot, hasThing:obj)
b.doThing(obj, `protocol`: prot)
}
func testNSArrayMethods(a: NSArray, b: B) {
a.makeObjectsPerformSelector("isEqual") // expected-error {{'performSelector' methods are unavailable}}
a.makeObjectsPerformSelector("isEqual", withObject:b) // expected-error {{'performSelector' methods are unavailable}}
}
// Class method invocation
func classMethods(b: B, other: NSObject) {
var i = B.classMethod()
i += B.classMethod(1)
i += B.classMethod(1, withInt:2)
i += b.classMethod() // expected-error{{'B' does not have a member named 'classMethod'}}
// Both class and instance methods exist.
B.description()
B.instanceTakesObjectClassTakesFloat(2.0)
B.instanceTakesObjectClassTakesFloat(other) // expected-error{{cannot invoke 'instanceTakesObjectClassTakesFloat' with an argument list of type '(NSObject)'}}
// Call an instance method of NSObject.
var c: AnyClass = B.myClass() // no-warning
c = b.myClass() // no-warning
}
// Instance method invocation on extensions
func instanceMethodsInExtensions(b: B) {
b.method(1, onCat1:2.5)
b.method(1, onExtA:2.5)
b.method(1, onExtB:2.5)
b.method(1, separateExtMethod:3.5)
let m1 = b.method:onCat1:
m1(1, onCat1: 2.5)
let m2 = b.method:onExtA:
m2(1, onExtA: 2.5)
let m3 = b.method:onExtB:
m3(1, onExtB: 2.5)
let m4 = b.method:separateExtMethod:
m4(1, separateExtMethod: 2.5)
}
func dynamicLookupMethod(b: AnyObject) {
// FIXME: Syntax will be replaced.
if let m5 = b.method:separateExtMethod: {
m5(1, separateExtMethod: 2.5)
}
}
// Properties
func properties(b: B) {
var i = b.counter
b.counter = i + 1
i = i + b.readCounter
b.readCounter = i + 1 // expected-error{{cannot assign to 'readCounter' in 'b'}}
b.setCounter(5) // expected-error{{'B' does not have a member named 'setCounter'}}
// Informal properties in Objective-C map to methods, not variables.
b.informalProp()
// An informal property cannot be made formal in a subclass. The
// formal property is simply ignored.
b.informalMadeFormal()
b.informalMadeFormal = i // expected-error{{cannot assign to 'informalMadeFormal' in 'b'}}
b.setInformalMadeFormal(5)
b.overriddenProp = 17
// Dynamic properties.
var obj : AnyObject = b
var optStr = obj.nsstringProperty
if optStr != nil {
var s : String = optStr!
}
// Properties that are Swift keywords
var prot = b.`protocol`
}
// Construction.
func newConstruction(a: A, aproxy: AProxy) {
var b : B = B()
b = B(int: 17)
b = B(int:17)
b = B(double:17.5, 3.14159)
b = B(BBB:b)
b = B(forWorldDomination:())
b = B(int: 17, andDouble : 3.14159)
b = B.newWithA(a)
B.alloc()._initFoo()
b.notAnInit()
// init methods are not imported by themselves.
b.initWithInt(17) // expected-error{{'B' does not have a member named 'initWithInt'}}
// init methods on non-NSObject-rooted classes
AProxy(int: 5)
}
// Indexed subscripting
func indexedSubscripting(b: B, idx: Int, a: A) {
b[idx] = a
var a2 = b[idx] as! A
}
// Keyed subscripting
func keyedSubscripting(b: B, idx: A, a: A) {
b[a] = a
var a2 = b[a] as! A
let dict = NSMutableDictionary()
dict[NSString()] = a
let value = dict[NSString()] // expected-warning {{constant 'value' inferred to have type 'AnyObject?', which may be unexpected}} expected-note {{add an explicit type annotation to silence this warning}}
dict[nil] = a // expected-error {{cannot assign a value of type 'A' to a value of type 'AnyObject?'}}
let _ = dict[nil] // expected-error {{type 'NSCopying' does not conform to protocol 'NilLiteralConvertible'}}
}
// Typed indexed subscripting
func checkHive(hive: Hive, b: B) {
var b2 = hive.bees[5] as! B
b2.method(1, withFloat:1.5)
}
// Protocols
func testProtocols(b: B, bp: BProto) {
var bp2 : BProto = b
var b2 : B = bp // expected-error{{'BProto' is not convertible to 'B'}}
bp.method(1, withFloat:2.5)
bp.method(1, withDouble:2.5) // expected-error{{incorrect argument label in call (have '_:withDouble:', expected '_:withFloat:')}}
bp2 = b.getAsProto()
var c1 : Cat1Proto = b
var bcat1 = b.getAsProtoWithCat()
c1 = bcat1
bcat1 = c1 // expected-error{{cannot assign a value of type 'Cat1Proto' to a value of type 'protocol<BProto, Cat1Proto>!'}}
}
// Methods only defined in a protocol
func testProtocolMethods(b: B, p2m: P2.Type) {
b.otherMethod(1, withFloat:3.14159)
b.p2Method()
b.initViaP2(3.14159, second:3.14159) // expected-error{{'B' does not have a member named 'initViaP2'}}
// Imported constructor.
var b2 = B(viaP2: 3.14159, second:3.14159)
p2m(viaP2:3.14159, second: 3.14159)
}
func testId(x: AnyObject) {
x.performSelector!("foo:", withObject: x) // expected-error{{'performSelector(_:withObject:)' is unavailable: 'performSelector' methods are unavailable}}
x.performAdd(1, withValue: 2, withValue: 3, withValue2: 4)
x.performAdd!(1, withValue: 2, withValue: 3, withValue2: 4)
x.performAdd?(1, withValue: 2, withValue: 3, withValue2: 4)
}
class MySubclass : B {
// Override a regular method.
override func anotherMethodOnB() {}
// Override a category method
override func anotherCategoryMethod() {}
}
func getDescription(array: NSArray) {
array.description
}
// Method overriding with unfortunate ordering.
func overridingTest(srs: SuperRefsSub) {
var rs : RefedSub
rs.overridden()
}
func almostSubscriptableValueMismatch(as1: AlmostSubscriptable, a: A) {
// FIXME: Crummy diagnostic.
as1[a] // expected-error{{'AlmostSubscriptable' does not have a member named 'subscript'}}
}
func almostSubscriptableKeyMismatch(bc: BadCollection, key: NSString) {
// FIXME: We end up importing this as read-only due to the mismatch between
// getter/setter element types.
var v : AnyObject = bc[key]
}
func almostSubscriptableKeyMismatchInherited(bc: BadCollectionChild,
key: String) {
var value : AnyObject = bc[key] // no-warning, inherited from parent
bc[key] = value // expected-error{{cannot assign to the result of this expression}}
}
func almostSubscriptableKeyMismatchInherited(roc: ReadOnlyCollectionChild,
key: String) {
var value : AnyObject = roc[key] // no-warning, inherited from parent
roc[key] = value // expected-error{{cannot assign to the result of this expression}}
}
// Use of 'Class' via dynamic lookup.
func classAnyObject(obj: NSObject) {
obj.myClass().description!()
}
// Protocol conformances
class Wobbler : NSWobbling { // expected-note{{candidate has non-matching type '()'}}
func wobble() { }
func returnMyself() -> Self { return self }
}
extension Wobbler : NSMaybeInitWobble { // expected-error{{type 'Wobbler' does not conform to protocol 'NSMaybeInitWobble'}}
}
func optionalMemberAccess(w: NSWobbling) {
w.wobble()
w.wibble() // expected-error{{value of optional type '(() -> Void)?' not unwrapped; did you mean to use '!' or '?'?}}
var x: AnyObject = w[5] // expected-error{{value of optional type 'AnyObject!?' not unwrapped; did you mean to use '!' or '?'?}}
}
func protocolInheritance(s: NSString) {
var coding: NSCoding = s
}
func ivars(hive: Hive) {
var d = hive.bees.description
hive.queen.description() // expected-error{{'Hive' does not have a member named 'queen'}}
}
class NSObjectable : NSObjectProtocol {
@objc var description : String { return "" }
}
// Properties with custom accessors
func customAccessors(hive: Hive, bee: B) {
println(hive.makingHoney)
println(hive.isMakingHoney()) // expected-error{{'Hive' does not have a member named 'isMakingHoney'}}
hive.setMakingHoney(true) // expected-error{{'Hive' does not have a member named 'setMakingHoney'}}
hive.guard.description // okay
hive.guard.description! // no-warning
hive.guard = bee // no-warning
}
// instancetype/Dynamic Self invocation.
func testDynamicSelf(queen: B, wobbler: NSWobbling) {
var hive = Hive()
// Factory method with instancetype result.
var hive1 = Hive(queen: queen)
hive1 = hive
hive = hive1
// Instance method with instancetype result.
var hive2 = hive.visit()
hive2 = hive
hive = hive2
// Instance method on a protocol with instancetype result.
var wobbler2 = wobbler.returnMyself()
var wobbler: NSWobbling = wobbler2
wobbler2 = wobbler
// Instance method on a base class with instancetype result, called on the
// class itself.
// FIXME: This should be accepted.
// FIXME: The error is lousy, too.
let baseClass: ObjCParseExtras.Base.Type = ObjCParseExtras.Base.returnMyself() // expected-error{{missing argument for parameter #1 in call}}
}
func testRepeatedProtocolAdoption(w: NSWindow) {
w.description
}
class ProtocolAdopter1 : FooProto {
var bar: CInt // no-warning
init() { bar = 5 }
}
class ProtocolAdopter2 : FooProto {
var bar: CInt {
get { return 42 }
set { /* do nothing! */ }
}
}
class ProtocolAdopterBad1 : FooProto { // expected-error{{type 'ProtocolAdopterBad1' does not conform to protocol 'FooProto'}}
var bar: Int = 0 // expected-note{{candidate has non-matching type 'Int'}}
}
class ProtocolAdopterBad2 : FooProto { // expected-error{{type 'ProtocolAdopterBad2' does not conform to protocol 'FooProto'}}
let bar: CInt = 0 // expected-note{{candidate is not settable, but protocol requires it}}
}
class ProtocolAdopterBad3 : FooProto { // expected-error{{type 'ProtocolAdopterBad3' does not conform to protocol 'FooProto'}}
var bar: CInt { // expected-note{{candidate is not settable, but protocol requires it}}
return 42
}
}
@objc protocol RefinedFooProtocol : FooProto {}
func testPreferClassMethodToCurriedInstanceMethod(obj: NSObject) {
// FIXME: We shouldn't need the ": Bool" type annotation here.
// <rdar://problem/18006008>
let result: Bool = NSObject.isEqual(obj)
let curried = NSObject.isEqual(obj) as (NSObject!) -> Bool // no-warning
}
func testPropertyAndMethodCollision(obj: PropertyAndMethodCollision,
rev: PropertyAndMethodReverseCollision) {
obj.object = nil
obj.object(obj, doSomething:"action")
rev.object = nil
rev.object(rev, doSomething:"action")
var value: AnyObject = obj.protoProp()
value = obj.protoPropRO()
}
func testSubscriptAndPropertyRedeclaration(obj: SubscriptAndProperty) {
let _ = obj.x
obj.x = 5
obj.objectAtIndexedSubscript(5) // expected-error{{'objectAtIndexedSubscript' is unavailable: use subscripting}}
obj.setX(5) // expected-error{{'SubscriptAndProperty' does not have a member named 'setX'}}
let _ = obj[0]
obj[1] = obj
obj.setObject(obj, atIndexedSubscript: 2) // expected-error{{'setObject(_:atIndexedSubscript:)' is unavailable: use subscripting}}
}
func testSubscriptAndPropertyWithProtocols(obj: SubscriptAndPropertyWithProto) {
let _ = obj.x
obj.x = 5
obj.setX(5) // expected-error{{'SubscriptAndPropertyWithProto' does not have a member named 'setX'}}
let _ = obj[0]
obj[1] = obj
obj.setObject(obj, atIndexedSubscript: 2) // expected-error{{'setObject(_:atIndexedSubscript:)' is unavailable: use subscripting}}
}
func testProtocolMappingSameModule(obj: AVVideoCompositionInstruction, p: AVVideoCompositionInstructionProtocol) {
println(p.enablePostProcessing)
println(obj.enablePostProcessing)
let _ = obj.backgroundColor
}
func testProtocolMappingDifferentModules(obj: ObjCParseExtrasToo.ProtoOrClass, p: ObjCParseExtras.ProtoOrClass) {
println(p.thisIsTheProto)
println(obj.thisClassHasAnAwfulName)
let _: ProtoOrClass? // expected-error{{'ProtoOrClass' is ambiguous for type lookup in this context}}
let _ = ObjCParseExtrasToo.ClassInHelper() // expected-error{{'ClassInHelper' cannot be constructed because it has no accessible initializers}}
let _ = ObjCParseExtrasToo.ProtoInHelper()
let _ = ObjCParseExtrasTooHelper.ClassInHelper()
let _ = ObjCParseExtrasTooHelper.ProtoInHelper() // expected-error{{'ProtoInHelper' cannot be constructed because it has no accessible initializers}}
}
func testProtocolClassShadowing(obj: ClassInHelper, p: ProtoInHelper) {
let _: ObjCParseExtrasToo.ClassInHelper = obj
let _: ObjCParseExtrasToo.ProtoInHelper = p
}
func testNSExtensionContext(url: NSURL, extensionContext: NSExtensionContext) {
extensionContext.openURL(url) { success in return }
}
func testDealloc(obj: NSObject) {
// dealloc is subsumed by deinit.
// FIXME: Special-case diagnostic in the type checker?
obj.dealloc() // expected-error{{'NSObject' does not have a member named 'dealloc'}}
}
func testConstantGlobals() {
println(MAX)
println(SomeImageName)
println(SomeNumber.description)
MAX = 5 // expected-error{{cannot assign to 'let' value 'MAX'}}
SomeImageName = "abc" // expected-error{{cannot assign to 'let' value 'SomeImageName'}}
SomeNumber = nil // expected-error{{cannot assign to 'let' value 'SomeNumber'}}
}
func testWeakVariable() {
let _: AnyObject = globalWeakVar
}
class IncompleteProtocolAdopter : Incomplete, IncompleteOptional { // expected-error {{type 'IncompleteProtocolAdopter' cannot conform to protocol 'Incomplete' because it has requirements that cannot be satisfied}}
func getObject() -> AnyObject { return self }
}
func testNullarySelectorPieces(obj: AnyObject) {
obj.foo(1, bar: 2, 3) // no-warning
obj.foo(1, 2, bar: 3) // expected-error{{'AnyObject' does not have a member named 'foo(_:_:bar:)'}}
}
func testFactoryMethodAvailability() {
let _ = DeprecatedFactoryMethod() // expected-error{{'init()' is unavailable: use something newer}}
}
func testRepeatedMembers(obj: RepeatedMembers) {
obj.repeatedMethod()
}