@@ -147,6 +147,142 @@ DictionaryTraps.test("BridgedKeyIsNotNSCopyable2")
147147 nsd. mutableCopy ( )
148148}
149149
150+ DictionaryTraps . test ( " ForcedNonverbatimBridge.StringKey " )
151+ . skip ( . custom(
152+ { _isFastAssertConfiguration ( ) } ,
153+ reason: " this trap is not guaranteed to happen in -Ounchecked " ) )
154+ . crashOutputMatches ( " Could not cast value of type " )
155+ . code {
156+ let d1 : NSDictionary = [
157+ " Gordon " as NSString : NSObject ( ) ,
158+ " William " as NSString : NSObject ( ) ,
159+ " Katherine " as NSString : NSObject ( ) ,
160+ " Lynn " as NSString : NSObject ( ) ,
161+ " Brian " as NSString : NSObject ( ) ,
162+ 1756 as NSNumber : NSObject ( ) ]
163+
164+ expectCrashLater ( )
165+ _ = d1 as! Dictionary < String , Any >
166+ }
167+
168+ DictionaryTraps . test ( " ForcedNonverbatimBridge.IntKey " )
169+ . skip ( . custom(
170+ { _isFastAssertConfiguration ( ) } ,
171+ reason: " this trap is not guaranteed to happen in -Ounchecked " ) )
172+ . crashOutputMatches ( " Could not cast value of type " )
173+ . code {
174+
175+ let d1 : NSDictionary = [
176+ 4 as NSNumber : NSObject ( ) ,
177+ 8 as NSNumber : NSObject ( ) ,
178+ 15 as NSNumber : NSObject ( ) ,
179+ 16 as NSNumber : NSObject ( ) ,
180+ 23 as NSNumber : NSObject ( ) ,
181+ 42 as NSNumber : NSObject ( ) ,
182+ " John " as NSString : NSObject ( ) ]
183+
184+ expectCrashLater ( )
185+ _ = d1 as! Dictionary < Int , Any >
186+ }
187+
188+ DictionaryTraps . test ( " ForcedNonverbatimBridge.Value " )
189+ . skip ( . custom(
190+ { _isFastAssertConfiguration ( ) } ,
191+ reason: " this trap is not guaranteed to happen in -Ounchecked " ) )
192+ . crashOutputMatches ( " Could not cast value of type " )
193+ . code {
194+
195+ let d1 : NSDictionary = [
196+ 4 as NSNumber : " Jack " as NSString ,
197+ 8 as NSNumber : " Kate " as NSString ,
198+ 15 as NSNumber : " Hurley " as NSString ,
199+ 16 as NSNumber : " Sawyer " as NSString ,
200+ 23 as NSNumber : " John " as NSString ,
201+ 42 as NSNumber : NSObject ( ) ]
202+
203+ expectCrashLater ( )
204+ _ = d1 as! Dictionary < NSObject , String >
205+ }
206+
207+
208+ DictionaryTraps . test ( " ForcedVerbatimBridge.StringKey " )
209+ . skip ( . custom(
210+ { _isFastAssertConfiguration ( ) } ,
211+ reason: " this trap is not guaranteed to happen in -Ounchecked " ) )
212+ . crashOutputMatches ( " Could not cast value of type " )
213+ . code {
214+ let d1 : NSDictionary = [
215+ " Gordon " as NSString : NSObject ( ) ,
216+ " William " as NSString : NSObject ( ) ,
217+ " Katherine " as NSString : NSObject ( ) ,
218+ " Lynn " as NSString : NSObject ( ) ,
219+ " Brian " as NSString : NSObject ( ) ,
220+ 1756 as NSNumber : NSObject ( ) ]
221+
222+ // With the ObjC runtime, the verbatim downcast is O(1); it performs no
223+ // runtime checks.
224+ let d2 = d1 as! Dictionary < NSString , NSObject >
225+ // Element access goes through the bridged path and performs forced downcasts.
226+ // This is where the odd numeric value is caught.
227+ expectCrashLater ( )
228+ for (key, value) in d2 {
229+ _ = ( key, value)
230+ }
231+ }
232+
233+ DictionaryTraps . test ( " ForcedVerbatimBridge.IntKey " )
234+ . skip ( . custom(
235+ { _isFastAssertConfiguration ( ) } ,
236+ reason: " this trap is not guaranteed to happen in -Ounchecked " ) )
237+ . crashOutputMatches ( " Could not cast value of type " )
238+ . code {
239+
240+ let d1 : NSDictionary = [
241+ 4 as NSNumber : NSObject ( ) ,
242+ 8 as NSNumber : NSObject ( ) ,
243+ 15 as NSNumber : NSObject ( ) ,
244+ 16 as NSNumber : NSObject ( ) ,
245+ 23 as NSNumber : NSObject ( ) ,
246+ 42 as NSNumber : NSObject ( ) ,
247+ " John " as NSString : NSObject ( ) ]
248+
249+ // With the ObjC runtime, the verbatim downcast is O(1); it performs no
250+ // runtime checks.
251+ let d2 = d1 as! Dictionary < NSNumber , NSObject >
252+ // Element access goes through the bridged path and performs forced downcasts.
253+ // This is where the odd numeric value is caught.
254+ expectCrashLater ( )
255+ for (key, value) in d2 {
256+ _ = ( key, value)
257+ }
258+ }
259+
260+ DictionaryTraps . test ( " ForcedVerbatimBridge.Value " )
261+ . skip ( . custom(
262+ { _isFastAssertConfiguration ( ) } ,
263+ reason: " this trap is not guaranteed to happen in -Ounchecked " ) )
264+ . crashOutputMatches ( " Could not cast value of type " )
265+ . code {
266+
267+ let d1 : NSDictionary = [
268+ 4 as NSNumber : " Jack " as NSString ,
269+ 8 as NSNumber : " Kate " as NSString ,
270+ 15 as NSNumber : " Hurley " as NSString ,
271+ 16 as NSNumber : " Sawyer " as NSString ,
272+ 23 as NSNumber : " John " as NSString ,
273+ 42 as NSNumber : NSObject ( ) ]
274+
275+ // With the ObjC runtime, the verbatim downcast is O(1); it performs no
276+ // runtime checks.
277+ let d2 = d1 as! Dictionary < NSObject , NSString >
278+ // Element access goes through the bridged path and performs forced downcasts.
279+ // This is where the odd numeric value is caught.
280+ expectCrashLater ( )
281+ for (key, value) in d2 {
282+ _ = ( key, value)
283+ }
284+ }
285+
150286DictionaryTraps . test ( " Downcast1 " ) {
151287 let d : Dictionary < NSObject , NSObject > = [ TestObjCKeyTy ( 10 ) : NSObject ( ) ,
152288 NSObject ( ) : NSObject ( ) ]
0 commit comments