@@ -41,7 +41,8 @@ private func runtimeCast<T,U>(_ from: T, to: U.Type) -> U? {
41
41
42
42
let CastsTests = TestSuite ( " Casts " )
43
43
44
- // Test for SR-426: missing release for some types after failed conversion
44
+ // https://github.com/apple/swift/issues/43043
45
+ // Missing release for some types after failed conversion
45
46
CastsTests . test ( " No leak for failed tuple casts " ) {
46
47
let t : Any = ( 1 , LifetimeTracked ( 0 ) )
47
48
expectFalse ( t is Any . Type )
@@ -50,10 +51,10 @@ CastsTests.test("No leak for failed tuple casts") {
50
51
protocol P { }
51
52
class ErrClass : Error { }
52
53
54
+ // https://github.com/apple/swift/issues/43009
53
55
CastsTests . test ( " No overrelease of existential boxes in failed casts " ) {
54
- // Test for crash from SR-392
55
- // We fail casts of an existential box repeatedly
56
- // to ensure it does not get over-released.
56
+ // We fail casts of an existential box repeatedly to ensure it does not get
57
+ // over-released.
57
58
func bar< T> ( _ t: T ) {
58
59
for _ in 0 ..< 10 {
59
60
if case let a as P = t {
@@ -68,8 +69,10 @@ CastsTests.test("No overrelease of existential boxes in failed casts") {
68
69
69
70
extension Int : P { }
70
71
71
- // Test for SR-7664: Inconsistent optional casting behaviour with generics
72
- // Runtime failed to unwrap multiple levels of Optional when casting.
72
+ /// https://github.com/apple/swift/issues/50204
73
+ /// Inconsistent optional casting behaviour with generics
74
+ ///
75
+ /// Runtime failed to unwrap multiple levels of `Optional` when casting.
73
76
CastsTests . test ( " Multi-level optionals can be casted " ) {
74
77
func testSuccess< From, To> ( _ x: From , from: From . Type , to: To . Type ) {
75
78
expectNotNil ( x as? To )
@@ -88,7 +91,8 @@ CastsTests.test("Multi-level optionals can be casted") {
88
91
testFailure ( 42 , from: Int??? . self , to: String . self)
89
92
}
90
93
91
- // Test for SR-9837: Optional<T>.none not casting to Optional<U>.none in generic context
94
+ /// https://github.com/apple/swift/issues/52251
95
+ /// `Optional<T>.none` not casting to `Optional<U>.none` in generic context
92
96
CastsTests . test ( " Optional<T>.none can be casted to Optional<U>.none in generic context " ) {
93
97
func test< T> ( _ type: T . Type ) -> T ? {
94
98
return Any ? . none as? T
@@ -98,10 +102,11 @@ CastsTests.test("Optional<T>.none can be casted to Optional<U>.none in generic c
98
102
expectEqual ( type ( of: test ( Bool ? . self) ) , Bool?? . self )
99
103
}
100
104
101
- // Test for SR-3871: Cannot cast from ObjC existential without going through AnyObject
105
+ /// https://github.com/apple/swift/issues/46456
106
+ /// Failure to cast from ObjC existential without going through `AnyObject`
102
107
#if _runtime(_ObjC)
103
108
protocol P2 { }
104
- CastsTests . test ( " Cast from ObjC existential to Protocol (SR-3871) " ) {
109
+ CastsTests . test ( " Cast from ObjC existential to Protocol " ) {
105
110
if #available( SwiftStdlib 5 . 3 , * ) {
106
111
struct S : P2 { }
107
112
@@ -111,7 +116,8 @@ CastsTests.test("Cast from ObjC existential to Protocol (SR-3871)") {
111
116
}
112
117
let a = ObjCWrapper ( ) . any
113
118
expectTrue ( a is P2 )
114
- // In SR-3871, the following cast failed (everything else here succeeded)
119
+ // The following cast failed in the above issue (everything else here
120
+ // succeeded).
115
121
expectNotNil ( a as? P2 )
116
122
expectNotNil ( a as? S )
117
123
let b = a as AnyObject
@@ -139,6 +145,26 @@ CastsTests.test("Cast from Swift existential to Protocol") {
139
145
expectNotNil ( b as? S )
140
146
}
141
147
148
+ /// Another test for https://github.com/apple/swift/issues/46456
149
+ /// User type in a `_SwiftValue` in an `Optional<Any>` not casting to a
150
+ /// protocol
151
+ ///
152
+ /// Note: This uses the (misnamed) `_bridgeAnythingToObjectiveC` so it can
153
+ /// test these paths on Linux as well.
154
+ protocol P4 { }
155
+ CastsTests . test ( " struct -> Obj-C -> Protocol " ) {
156
+ struct S : P4 {
157
+ let value : Int
158
+ let tracker = LifetimeTracked ( 13 )
159
+ }
160
+
161
+ let a : P4 = S ( value: 13 )
162
+
163
+ let b = _bridgeAnythingToObjectiveC ( a)
164
+ let d = b as? Any
165
+ let e = d as? P4
166
+ expectNotNil ( e)
167
+ }
142
168
143
169
#if _runtime(_ObjC)
144
170
extension CFBitVector : P {
@@ -160,7 +186,8 @@ func isP<T>(_ t: T) -> Bool {
160
186
return t is P
161
187
}
162
188
163
- CastsTests . test ( " Dynamic casts of CF types to protocol existentials (SR-2289) " )
189
+ // https://github.com/apple/swift/issues/44896
190
+ CastsTests . test ( " Dynamic casts of CF types to protocol existentials " )
164
191
. skip ( . custom( {
165
192
!_isDebugAssertConfiguration( )
166
193
} ,
@@ -174,37 +201,18 @@ CastsTests.test("Dynamic casts of CF types to protocol existentials (SR-2289)")
174
201
}
175
202
#endif
176
203
177
- // Another test for SR-3871, SR-5590, SR-6309, SR-8651:
178
- // user type in a _SwiftValue in an Optional<Any> can't be cast to a protocol.
179
- // Note: This uses the (misnamed) _bridgeAnythingToObjectiveC so it can
180
- // test these paths on Linux as well.
181
- protocol P6309 { }
182
- CastsTests . test ( " Casting struct -> Obj-C -> Protocol fails (SR-3871, SR-5590, SR-6309, SR-8651) " ) {
183
- struct S : P6309 {
184
- let value : Int
185
- let tracker = LifetimeTracked ( 13 )
186
- }
187
-
188
- let a : P6309 = S ( value: 13 )
189
-
190
- let b = _bridgeAnythingToObjectiveC ( a)
191
- let d = b as? Any
192
- let e = d as? P6309
193
- expectNotNil ( e)
194
- }
195
-
196
-
197
- protocol P4552 { }
204
+ // https://github.com/apple/swift/issues/47129
205
+ protocol P_47129 { }
198
206
if #available( SwiftStdlib 5 . 5 , * ) {
199
- CastsTests . test ( " Casting Any(Optional(T)) -> Protocol fails (SR-4552) " ) {
200
- struct S : P4552 {
207
+ CastsTests . test ( " Any(Optional(T)) -> Protocol " ) {
208
+ struct S : P_47129 {
201
209
let tracker = LifetimeTracked ( 13 )
202
210
}
203
211
204
212
let a = S ( )
205
213
let b : S ? = a
206
214
let c = b as? Any
207
- let d = c as? P4552
215
+ let d = c as? P_47129
208
216
expectNotNil ( d)
209
217
}
210
218
}
@@ -264,8 +272,9 @@ CastsTests.test("Store Swift metatype in ObjC property and cast back to Any.Type
264
272
}
265
273
#endif
266
274
267
- // rdar://37793004 ([dynamic casting] [SR-7049]: Enums don't cast back from AnyHashable)
268
- CastsTests . test ( " Enums don't cast back from AnyHashable (SR-7049) " ) {
275
+ /// rdar://37793004
276
+ /// https://github.com/apple/swift/issues/49597
277
+ CastsTests . test ( " Cast enum back from AnyHashable " ) {
269
278
enum E {
270
279
case a
271
280
}
@@ -282,8 +291,10 @@ CastsTests.test("Enums don't cast back from AnyHashable (SR-7049)") {
282
291
expectEqual ( ( ea as? E ) , E . a)
283
292
}
284
293
294
+ /// rdar://39415812
295
+ /// https://github.com/apple/swift/issues/49975
296
+ /// Failure to see through boxed `_SwiftValue` when casting from `@objc` Type
285
297
#if _runtime(_ObjC)
286
- //rdar://39415812 ([dynamic casting] [SR-7432]: Can't see through boxed _SwiftValue when casting from @objc Type)
287
298
@objc ( Exporter)
288
299
protocol Exporter : NSObjectProtocol {
289
300
var type : Any { get }
@@ -318,9 +329,12 @@ CastsTests.test("Conditional NSNumber -> Bool casts") {
318
329
}
319
330
#endif
320
331
321
- // rdar://45217461 ([dynamic casting] [SR-8964]: Type check operator (is) fails for Any! variable holding an Error (struct) value)
332
+ /// rdar://45217461
333
+ /// https://github.com/apple/swift/issues/51469
334
+ /// Type check operator (`is`) fails for `Any!` variable holding an `Error`
335
+ /// (struct) value
322
336
if #available( SwiftStdlib 5 . 5 , * ) {
323
- CastsTests . test ( " Casts from Any(struct) to Error (SR-8964) " ) {
337
+ CastsTests . test ( " Casts from Any(struct) to Error " ) {
324
338
struct MyError : Error { }
325
339
326
340
let a : Any ! = MyError ( )
@@ -329,6 +343,13 @@ CastsTests.test("Casts from Any(struct) to Error (SR-8964)") {
329
343
}
330
344
}
331
345
346
+ CastsTests . test ( " Cast failure for Any! holding Error struct " ) {
347
+ struct MyError : Error { }
348
+ let a : Any ! = MyError ( )
349
+ let b : Any = a
350
+ expectTrue ( b is Error )
351
+ }
352
+
332
353
#if _runtime(_ObjC)
333
354
// rdar://15494623 (Handle dynamic cast to archetype bound to ObjC existential)
334
355
CastsTests . test ( " Dynamic cast to ObjC protocol " ) {
@@ -342,9 +363,9 @@ CastsTests.test("Dynamic cast to ObjC protocol") {
342
363
}
343
364
#endif
344
365
345
- // SR-6126
366
+ // https://github.com/apple/swift/issues/48681
346
367
if #available( macOS 11 . 3 , iOS 14 . 5 , tvOS 14 . 5 , watchOS 7 . 4 , * ) {
347
- CastsTests . test ( " Nil handling for Optionals and Arrays (SR-6126) " ) {
368
+ CastsTests . test ( " Nil handling for Optionals and Arrays " ) {
348
369
func check( _ arg: Int ? ? ) -> String {
349
370
switch arg {
350
371
case . none:
@@ -471,39 +492,56 @@ CastsTests.test("String/NSString extension compat") {
471
492
}
472
493
#endif
473
494
474
- protocol P1999 { }
495
+ // https://github.com/apple/swift/issues/44608
496
+ protocol P_44608 { }
475
497
if #available( SwiftStdlib 5 . 5 , * ) {
476
- CastsTests . test ( " Cast Any(Optional(class)) to Protocol type (SR-1999) " ) {
477
- class Foo : P1999 { }
498
+ CastsTests . test ( " Cast Any(Optional(class)) to Protocol type " ) {
499
+ class Foo : P_44608 { }
478
500
479
501
let optionalFoo : Foo ? = Foo ( )
480
502
let anyValue : Any = optionalFoo
481
503
482
504
let foo1 = anyValue as? Foo
483
505
expectNotNil ( foo1)
484
506
485
- let foo2 = anyValue as? P1999
507
+ let foo2 = anyValue as? P_44608
486
508
expectNotNil ( foo2)
487
509
488
510
let foo3 = runtimeCast ( anyValue, to: Foo . self)
489
511
expectNotNil ( foo3)
490
512
491
- let foo4 = runtimeCast ( anyValue, to: P1999 . self)
513
+ let foo4 = runtimeCast ( anyValue, to: P_44608 . self)
492
514
expectNotNil ( foo4)
493
515
}
494
516
}
495
517
518
+ CastsTests . test ( " Cast from Any? to Existential " ) {
519
+ let a = Float ( 1 ) as Any as? Float
520
+ expectNotNil ( a)
521
+
522
+ let b = Float ( 1 ) as Any as? CustomStringConvertible
523
+ expectNotNil ( b)
524
+
525
+ let c = Optional . some ( Float ( 1 ) ) as Any as? Float
526
+ expectNotNil ( c)
527
+
528
+ let d = Optional . some ( Float ( 1 ) ) as Any as? CustomStringConvertible
529
+ expectNotNil ( d)
530
+ }
531
+
532
+ // https://github.com/apple/swift/issues/45505
496
533
#if _runtime(_ObjC)
497
- CastsTests . test ( " Dict value casting (SR-2911) " ) {
534
+ CastsTests . test ( " Dict value casting " ) {
498
535
var dict : [ AnyHashable : String ] = [ : ]
499
536
dict [ " Key " ] = " Value "
500
537
expectNotNil ( dict [ " Key " ] as? NSString )
501
538
expectNotNil ( runtimeCast ( dict [ " Key " ] , to: NSString . self) )
502
539
}
503
540
#endif
504
541
542
+ // https://github.com/apple/swift-corelibs-foundation/issues/3279
505
543
#if _runtime(_ObjC)
506
- CastsTests . test ( " String coercions should work on Linux (SR-12020) " ) {
544
+ CastsTests . test ( " String coercions should work on Linux " ) {
507
545
let a = " abc " as Substring as NSString
508
546
let b = " abc " as NSString
509
547
expectEqual ( a, b)
@@ -535,9 +573,10 @@ CastsTests.test("AnyHashable(Class) -> Obj-C -> Class") {
535
573
expectNotNil ( e)
536
574
}
537
575
538
- #if _runtime(_ObjC)
539
576
// rdar://58999120
540
- CastsTests . test ( " Error -> NSError -> Protocol transitivity (SR-12095) " ) {
577
+ // https://github.com/apple/swift-corelibs-foundation/issues/3274
578
+ #if _runtime(_ObjC)
579
+ CastsTests . test ( " Error -> NSError -> Protocol transitivity " ) {
541
580
enum NonConformingError : Error {
542
581
case ok
543
582
}
@@ -802,8 +841,9 @@ CastsTests.test("AnyObject.Type -> AnyObject") {
802
841
}
803
842
#endif
804
843
844
+ // https://github.com/apple/swift/issues/56209
805
845
protocol Fruit { }
806
- CastsTests . test ( " Generic type validation [SR-13812] " ) {
846
+ CastsTests . test ( " Generic type validation " ) {
807
847
func check< A, B> ( a: A . Type , b: B . Type ) -> Bool {
808
848
return ( a is B . Type)
809
849
}
@@ -813,29 +853,9 @@ CastsTests.test("Generic type validation [SR-13812]") {
813
853
expectTrue ( Apple . self is Fruit . Type )
814
854
}
815
855
816
- CastsTests . test ( " Cast failure for Any! holding Error struct [SR-8964] " ) {
817
- struct MyError : Error { }
818
- let a : Any ! = MyError ( )
819
- let b : Any = a
820
- expectTrue ( b is Error )
821
- }
822
-
823
- CastsTests . test ( " Cannot cast from Any? to Existential [SR-1999] " ) {
824
- let a = Float ( 1 ) as Any as? Float
825
- expectNotNil ( a)
826
-
827
- let b = Float ( 1 ) as Any as? CustomStringConvertible
828
- expectNotNil ( b)
829
-
830
- let c = Optional . some ( Float ( 1 ) ) as Any as? Float
831
- expectNotNil ( c)
832
-
833
- let d = Optional . some ( Float ( 1 ) ) as Any as? CustomStringConvertible
834
- expectNotNil ( d)
835
- }
836
-
856
+ // https://github.com/apple/swift/issues/48829
837
857
protocol A { }
838
- CastsTests . test ( " Failing cast from Any to Optional<Protocol> [SR-6279] " ) {
858
+ CastsTests . test ( " Cast from Any to Optional<Protocol>" ) {
839
859
struct B : A { }
840
860
841
861
// If we have an optional instance, stored as an `Any`
@@ -865,11 +885,12 @@ CastsTests.test("Casting Objects retained from KeyPaths to Protocols is not work
865
885
expectNotNil ( value as? SuperProtocol )
866
886
}
867
887
888
+ // https://github.com/apple/swift/issues/54462
889
+ // FIXME: Known to still be broken, but we can document the issue here.
868
890
#if _runtime(_ObjC)
869
- // Known to still be broken, but we can document the issue here
870
891
public protocol SomeProtocol { }
871
892
extension NSString : SomeProtocol { }
872
- CastsTests . test ( " NSDictionary -> Dictionary casting [SR-12025] " ) {
893
+ CastsTests . test ( " NSDictionary -> Dictionary casting " ) {
873
894
// Create NSDictionary with one entry
874
895
var a = NSMutableDictionary ( )
875
896
a [ NSString ( " key " ) ] = NSString ( " value " )
@@ -897,7 +918,8 @@ CastsTests.test("Optional cast to AnyHashable") {
897
918
// We've deliberately tried to preserve that behavior in Swift 5.4
898
919
let d2 = d as [ AnyHashable : String ]
899
920
900
- // After SR-9047, all four of the following should work:
921
+ // After https://github.com/apple/swift/issues/51550 all four of the following
922
+ // should work:
901
923
let d3 = d2 [ " FooKey " as String ? as AnyHashable ]
902
924
expectNil ( d3)
903
925
let d4 = d2 [ " FooKey " as String ? ]
@@ -921,7 +943,8 @@ CastsTests.test("Optional cast to AnyHashable") {
921
943
// Direct casts that don't go through the runtime don't unwrap the optional
922
944
// This is inconsistent with the runtime cast behavior above. We should
923
945
// probably change the runtime behavior above to work the same as this,
924
- // but that should wait until SR-9047 lands.
946
+ // but that should wait until https://github.com/apple/swift/issues/51550
947
+ // lands.
925
948
let x : String = " Baz "
926
949
let xh = x as AnyHashable
927
950
let y : String ? = x
@@ -954,16 +977,17 @@ CastsTests.test("Recursive AnyHashable") {
954
977
expectEqual ( s. x, p)
955
978
}
956
979
957
- // SR-14635 (aka rdar://78224322)
980
+ // rdar://78224322
981
+ // https://github.com/apple/swift/issues/56987
958
982
#if _runtime(_ObjC)
959
983
CastsTests . test ( " Do not overuse __SwiftValue " )
960
- . skip ( . osxAny( " SR-14635 not yet fully enabled for Apple OSes" ) )
961
- . skip ( . iOSAny( " SR-14635 not yet fully enabled for Apple OSes" ) )
962
- . skip ( . iOSSimulatorAny( " SR-14635 not yet fully enabled for Apple OSes" ) )
963
- . skip ( . tvOSAny( " SR-14635 not yet fully enabled for Apple OSes" ) )
964
- . skip ( . tvOSSimulatorAny( " SR-14635 not yet fully enabled for Apple OSes" ) )
965
- . skip ( . watchOSAny( " SR-14635 not yet fully enabled for Apple OSes" ) )
966
- . skip ( . watchOSSimulatorAny( " SR-14635 not yet fully enabled for Apple OSes" ) )
984
+ . skip ( . osxAny( " Not yet fully enabled for Apple OSes" ) )
985
+ . skip ( . iOSAny( " Not yet fully enabled for Apple OSes" ) )
986
+ . skip ( . iOSSimulatorAny( " Not yet fully enabled for Apple OSes" ) )
987
+ . skip ( . tvOSAny( " Not yet fully enabled for Apple OSes" ) )
988
+ . skip ( . tvOSSimulatorAny( " Not yet fully enabled for Apple OSes" ) )
989
+ . skip ( . watchOSAny( " Not yet fully enabled for Apple OSes" ) )
990
+ . skip ( . watchOSSimulatorAny( " Not yet fully enabled for Apple OSes" ) )
967
991
. code {
968
992
struct Bar { }
969
993
// This used to succeed because of overeager __SwiftValue
0 commit comments