@@ -4714,4 +4714,116 @@ SetTestSuite.test("ForcedVerbatimBridge.Trap.NSNumber")
4714
4714
}
4715
4715
#endif
4716
4716
4717
+ #if _runtime(_ObjC)
4718
+ SetTestSuite . test ( " ForcedVerbatimDowncast.Trap.String " )
4719
+ . skip ( . custom(
4720
+ { _isFastAssertConfiguration ( ) } ,
4721
+ reason: " this trap is not guaranteed to happen in -Ounchecked " ) )
4722
+ . crashOutputMatches ( " Could not cast value of type " )
4723
+ . code {
4724
+ let s1 : Set < NSObject > = [
4725
+ " Gordon " as NSString ,
4726
+ " William " as NSString ,
4727
+ " Katherine " as NSString ,
4728
+ " Lynn " as NSString ,
4729
+ " Brian " as NSString ,
4730
+ 1756 as NSNumber ]
4731
+ // With the ObjC runtime, the verbatim downcast is O(1); it performs no
4732
+ // runtime checks.
4733
+ let s2 = s1 as! Set < NSString >
4734
+ // Element access goes through the bridged path and performs forced downcasts.
4735
+ // This is where the odd numeric value is caught.
4736
+ expectCrashLater ( )
4737
+ for string in s2 {
4738
+ _ = string
4739
+ }
4740
+ }
4741
+ #endif
4742
+
4743
+ #if _runtime(_ObjC)
4744
+ SetTestSuite . test ( " ForcedVerbatimDowncast.Trap.Int " )
4745
+ . skip ( . custom(
4746
+ { _isFastAssertConfiguration ( ) } ,
4747
+ reason: " this trap is not guaranteed to happen in -Ounchecked " ) )
4748
+ . crashOutputMatches ( " Could not cast value of type " )
4749
+ . code {
4750
+ let s1 : Set < NSObject > = [
4751
+ 4 as NSNumber ,
4752
+ 8 as NSNumber ,
4753
+ 15 as NSNumber ,
4754
+ 16 as NSNumber ,
4755
+ 23 as NSNumber ,
4756
+ 42 as NSNumber ,
4757
+ " John " as NSString ]
4758
+ // With the ObjC runtime, the verbatim downcast is O(1); it performs no
4759
+ // runtime checks.
4760
+ let s2 = s1 as! Set < NSNumber >
4761
+ expectCrashLater ( )
4762
+ // Element access goes through the bridged path and performs forced downcasts.
4763
+ // This is where the odd string value is caught.
4764
+ for string in s2 {
4765
+ _ = string
4766
+ }
4767
+ }
4768
+ #endif
4769
+
4770
+ #if _runtime(_ObjC)
4771
+ SetTestSuite . test ( " ForcedBridgingNonverbatimDowncast.Trap.String " )
4772
+ . skip ( . custom(
4773
+ { _isFastAssertConfiguration ( ) } ,
4774
+ reason: " this trap is not guaranteed to happen in -Ounchecked " ) )
4775
+ . crashOutputMatches ( " Could not cast value of type " )
4776
+ . code {
4777
+ let s1 : Set < NSObject > = [
4778
+ " Gordon " as NSString ,
4779
+ " William " as NSString ,
4780
+ " Katherine " as NSString ,
4781
+ " Lynn " as NSString ,
4782
+ " Brian " as NSString ,
4783
+ 1756 as NSNumber ]
4784
+ expectCrashLater ( )
4785
+ // Nonverbatim downcasts are greedy and they trap immediately.
4786
+ let s2 = s1 as! Set < String >
4787
+ _ = s2. contains ( " Gordon " )
4788
+ }
4789
+ #endif
4790
+
4791
+ #if _runtime(_ObjC)
4792
+ SetTestSuite . test ( " ForcedBridgingNonverbatimDowncast.Trap.Int " )
4793
+ . skip ( . custom(
4794
+ { _isFastAssertConfiguration ( ) } ,
4795
+ reason: " this trap is not guaranteed to happen in -Ounchecked " ) )
4796
+ . crashOutputMatches ( " Could not cast value of type " )
4797
+ . code {
4798
+ let s1 : Set < NSObject > = [
4799
+ 4 as NSNumber ,
4800
+ 8 as NSNumber ,
4801
+ 15 as NSNumber ,
4802
+ 16 as NSNumber ,
4803
+ 23 as NSNumber ,
4804
+ 42 as NSNumber ,
4805
+ " John " as NSString ]
4806
+ expectCrashLater ( )
4807
+ // Nonverbatim downcasts are greedy and they trap immediately.
4808
+ let s2 = s1 as! Set < Int >
4809
+ _ = s2. contains ( 23 )
4810
+ }
4811
+ #endif
4812
+
4813
+ #if _runtime(_ObjC)
4814
+ SetTestSuite . test ( " Upcast.StringEqualityMismatch " ) {
4815
+ // Upcasting from NSString to String keys changes their concept of equality,
4816
+ // resulting in two equal keys, one of which should be discarded by the
4817
+ // downcast. (Along with its associated value.)
4818
+ // rdar://problem/35995647
4819
+ let s : Set < NSString > = [
4820
+ " cafe \u{301} " ,
4821
+ " café "
4822
+ ]
4823
+ expectEqual ( s. count, 2 )
4824
+ let s2 = s as Set < String >
4825
+ expectEqual ( s2. count, 1 )
4826
+ }
4827
+ #endif
4828
+
4717
4829
runAllTests ( )
0 commit comments