Skip to content

Commit 233a2ba

Browse files
committed
Runtime: Fix _SwiftNativeNSError's copyWithZone:.
The default implementation from NSObject doesn't know how to copy the Swift error payload, and the object's immutable anyway, so we just need to retain and return the object we already have.
1 parent d5d196a commit 233a2ba

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

stdlib/public/runtime/ErrorObject.mm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ - (NSDictionary*)userInfo {
8989
}
9090
}
9191

92+
- (id)copyWithZone:(NSZone *)zone {
93+
(void)zone;
94+
// _SwiftNativeNSError is immutable, so we can return the same instance back.
95+
return [self retain];
96+
}
97+
9298
@end
9399

94100
Class swift::getNSErrorClass() {

test/1_stdlib/ErrorProtocolBridging.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ ErrorProtocolBridgingTests.test("NSError") {
6060
expectEqual(NoisyErrorDeathCount, NoisyErrorLifeCount)
6161
}
6262

63+
ErrorProtocolBridgingTests.test("NSCopying") {
64+
autoreleasepool {
65+
let orig = EnumError.ReallyBadError as NSError
66+
let copy = orig.copy() as! NSError
67+
expectEqual(orig, copy)
68+
}
69+
}
70+
6371
ErrorProtocolBridgingTests.test("NSError-to-enum bridging") {
6472
NoisyErrorLifeCount = 0
6573
NoisyErrorDeathCount = 0

0 commit comments

Comments
 (0)