@@ -795,4 +795,90 @@ CastsTests.test("AnyObject.Type -> AnyObject") {
795
795
}
796
796
#endif
797
797
798
+ protocol Fruit { }
799
+ CastsTests . test ( " Generic type validation [SR-13812] " ) {
800
+ func check< A, B> ( a: A . Type , b: B . Type ) -> Bool {
801
+ return ( a is B . Type)
802
+ }
803
+ struct Apple : Fruit { }
804
+ expectFalse ( check ( a: Apple . self, b: Fruit . self) )
805
+ expectFalse ( Apple . self is Fruit . Protocol )
806
+ expectTrue ( Apple . self is Fruit . Type )
807
+ }
808
+
809
+ CastsTests . test ( " Cast failure for Any! holding Error struct [SR-8964] " ) {
810
+ struct MyError : Error { }
811
+ let a : Any ! = MyError ( )
812
+ let b : Any = a
813
+ expectTrue ( b is Error )
814
+ }
815
+
816
+ CastsTests . test ( " Cannot cast from Any? to Existential [SR-1999] " ) {
817
+ let a = Float ( 1 ) as Any as? Float
818
+ expectNotNil ( a)
819
+
820
+ let b = Float ( 1 ) as Any as? CustomStringConvertible
821
+ expectNotNil ( b)
822
+
823
+ let c = Optional . some ( Float ( 1 ) ) as Any as? Float
824
+ expectNotNil ( c)
825
+
826
+ let d = Optional . some ( Float ( 1 ) ) as Any as? CustomStringConvertible
827
+ expectNotNil ( d)
828
+ }
829
+
830
+ protocol A { }
831
+ CastsTests . test ( " Failing cast from Any to Optional<Protocol> [SR-6279] " ) {
832
+ struct B : A { }
833
+
834
+ // If we have an optional instance, stored as an `Any`
835
+ let b : A ? = B ( )
836
+ let c = b as Any
837
+
838
+ // This fails to cast, should succeed.
839
+ let d = c as? A
840
+ expectNotNil ( d)
841
+
842
+ // There is a workaround, but not ideal.
843
+ func cast< T, U> ( _ t: T , to: U . Type ) -> U ? {
844
+ return t as? U
845
+ }
846
+ let f = cast ( c, to: Any ? . self) as? A
847
+ expectNotNil ( f)
848
+ }
849
+
850
+ protocol SuperProtocol { }
851
+ CastsTests . test ( " Casting Objects retained from KeyPaths to Protocols is not working properly " ) {
852
+ // This is the simplified reproduction from rdar://59844232 which doesn't
853
+ // actually use KeyPaths
854
+ class SubClass : SuperProtocol { }
855
+ let value = SubClass ( ) as Any ? as Any
856
+
857
+ expectNotNil ( value as? SubClass )
858
+ expectNotNil ( value as? SuperProtocol )
859
+ }
860
+
861
+ #if _runtime(_ObjC)
862
+ // Known to still be broken, but we can document the issue here
863
+ public protocol SomeProtocol { }
864
+ extension NSString : SomeProtocol { }
865
+ CastsTests . test ( " NSDictionary -> Dictionary casting [SR-12025] " ) {
866
+ // Create NSDictionary with one entry
867
+ var a = NSMutableDictionary ( )
868
+ a [ NSString ( " key " ) ] = NSString ( " value " )
869
+
870
+ let v = NSString ( " value " )
871
+ let v2 = v as? SomeProtocol
872
+ expectNotNil ( v2)
873
+
874
+ // Test casting of the dictionary
875
+ let b = a as? [ String : SomeProtocol ]
876
+ expectFailure { expectNotNil ( b) } // Expect non-nil, but see nil
877
+ let c = a as? [ String : Any ]
878
+ expectNotNil ( c) // Non-nil (as expected)
879
+ let d = c as? [ String : SomeProtocol ]
880
+ expectNotNil ( d) // Non-nil (as expected)
881
+ }
882
+ #endif
883
+
798
884
runAllTests ( )
0 commit comments