stdlib/Dictionary: add tests for trapping during bridging a Dictionary

whose KeyType is not NSCopyable

Swift SVN r18259
This commit is contained in:
Dmitri Hrybenko
2014-05-17 17:24:58 +00:00
parent 6808737952
commit 5c453ce2bc
2 changed files with 54 additions and 3 deletions

View File

@@ -696,7 +696,11 @@ func _convertDictionaryToNSDictionary<KeyType, ValueType>(
// Avoid calling the runtime.
bridgedValue = _reinterpretCastToAnyObject(value)
} else {
bridgedValue = bridgeToObjectiveC(value)!
if let theBridgedValue: AnyObject = bridgeToObjectiveC(value) {
bridgedValue = theBridgedValue
} else {
_preconditionFailure("Dictionary to NSDictionary bridging: value failed to bridge")
}
}
// NOTE: the just-bridged key is copied here. It would be nice to avoid
@@ -707,7 +711,12 @@ func _convertDictionaryToNSDictionary<KeyType, ValueType>(
if let nsCopyingKey = bridgedKey as NSCopying {
result[nsCopyingKey] = bridgedValue
} else {
_fatalError("key bridged to an object that does not conform to NSCopying")
// This check is considered not comprehensive because on a different
// code path -- when KeyType bridges verbatim -- we are not doing it
// eagerly. Instead, the message send will fail at runtime when
// NSMutableDictionary attempts to copy the key that does not conform
// to NSCopying.
_debugPreconditionFailure("key bridged to an object that does not conform to NSCopying")
}
}
return reinterpretCast(result)

View File

@@ -12,9 +12,15 @@
// RUN: %target-run %t/a.out RemoveInvalidIndex2 2>&1 | FileCheck %s -check-prefix=CHECK
// RUN: %target-run %t/a.out RemoveInvalidIndex3 2>&1 | FileCheck %s -check-prefix=CHECK
// RUN: %target-run %t/a.out RemoveInvalidIndex4 2>&1 | FileCheck %s -check-prefix=CHECK
// RUN: %target-run %t/a.out BridgedKeyIsNotNSCopyable1 2>&1 | FileCheck %s -check-prefix=CHECK-UNRECOGNIZED-SELECTOR
// RUN: %target-run %t/a.out BridgedKeyIsNotNSCopyable2 2>&1 | FileCheck %s -check-prefix=CHECK
// CHECK: OK
// CHECK: CRASHED: SIG{{ILL|TRAP}}
// CHECK: CRASHED: SIG{{ILL|TRAP|ABRT}}
// CHECK-UNRECOGNIZED-SELECTOR: OK
// CHECK-UNRECOGNIZED-SELECTOR: unrecognized selector sent to instance
// CHECK-UNRECOGNIZED-SELECTOR: CRASHED: SIGABRT
import Foundation
@@ -123,6 +129,42 @@ if arg == "RemoveInvalidIndex4" {
d.removeAtIndex(index)
}
class TestObjCKeyTy : NSObject {
init(_ value: Int) {
self.value = value
}
override func isEqual(object: AnyObject!) -> Bool {
if let other: AnyObject = object {
if let otherObjcKey = other as TestObjCKeyTy {
return self.value == otherObjcKey.value
}
}
return false
}
override var hash : Int {
return value
}
var value: Int
}
if arg == "BridgedKeyIsNotNSCopyable1" {
// This Dictionary is bridged in O(1).
var d = [ TestObjCKeyTy(10): NSObject() ]
var nsd: NSDictionary = d
println("OK")
nsd.mutableCopy()
}
if arg == "BridgedKeyIsNotNSCopyable2" {
// This Dictionary is bridged in O(N).
var d = [ TestObjCKeyTy(10): 10 ]
println("OK")
var nsd: NSDictionary = d
}
println("BUSTED: should have crashed already")
exit(1)