mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
stdlib/Dictionary: add tests for trapping during bridging a Dictionary
whose KeyType is not NSCopyable Swift SVN r18259
This commit is contained in:
@@ -696,7 +696,11 @@ func _convertDictionaryToNSDictionary<KeyType, ValueType>(
|
|||||||
// Avoid calling the runtime.
|
// Avoid calling the runtime.
|
||||||
bridgedValue = _reinterpretCastToAnyObject(value)
|
bridgedValue = _reinterpretCastToAnyObject(value)
|
||||||
} else {
|
} 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
|
// 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 {
|
if let nsCopyingKey = bridgedKey as NSCopying {
|
||||||
result[nsCopyingKey] = bridgedValue
|
result[nsCopyingKey] = bridgedValue
|
||||||
} else {
|
} 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)
|
return reinterpretCast(result)
|
||||||
|
|||||||
@@ -12,9 +12,15 @@
|
|||||||
// RUN: %target-run %t/a.out RemoveInvalidIndex2 2>&1 | FileCheck %s -check-prefix=CHECK
|
// 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 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 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: 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
|
import Foundation
|
||||||
|
|
||||||
@@ -123,6 +129,42 @@ if arg == "RemoveInvalidIndex4" {
|
|||||||
d.removeAtIndex(index)
|
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")
|
println("BUSTED: should have crashed already")
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user