Skip to content

Commit c02b95e

Browse files
authored
[Runtime] A Swift Error bridged to NSError should return its description, rather than NSError.description (#29224)
1 parent 1c74c7f commit c02b95e

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

stdlib/public/runtime/ErrorObject.mm

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,20 @@
2424
#include "swift/Runtime/Config.h"
2525

2626
#if SWIFT_OBJC_INTEROP
27+
#include "ErrorObject.h"
28+
#include "Private.h"
29+
#include "SwiftObject.h"
30+
#include "swift/Basic/Lazy.h"
31+
#include "swift/Demangling/ManglingMacros.h"
2732
#include "swift/Runtime/Casting.h"
2833
#include "swift/Runtime/Debug.h"
2934
#include "swift/Runtime/ObjCBridge.h"
30-
#include "swift/Basic/Lazy.h"
31-
#include "swift/Demangling/ManglingMacros.h"
32-
#include "ErrorObject.h"
33-
#include "Private.h"
35+
#include <Foundation/Foundation.h>
3436
#include <dlfcn.h>
3537
#include <objc/NSObject.h>
36-
#include <objc/runtime.h>
3738
#include <objc/message.h>
3839
#include <objc/objc.h>
39-
#include <Foundation/Foundation.h>
40+
#include <objc/runtime.h>
4041

4142
using namespace swift;
4243
using namespace swift::hashable_support;
@@ -99,6 +100,12 @@ - (void)dealloc {
99100
return cf_const_cast<id>(domain);
100101
}
101102

103+
- (id /* NSString */)description {
104+
auto error = (const SwiftError *)self;
105+
return getDescription(const_cast<OpaqueValue *>(error->getValue()),
106+
error->type);
107+
}
108+
102109
- (NSInteger)code {
103110
auto error = (const SwiftError*)self;
104111
return error->code.load(SWIFT_MEMORY_ORDER_CONSUME);

test/stdlib/ErrorBridged.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,4 +836,22 @@ ErrorBridgingTests.test("SR-9207 crash in failed cast to NSError") {
836836
}
837837
}
838838

839+
// SR-7652
840+
841+
enum SwiftError: Error, CustomStringConvertible {
842+
case something
843+
var description: String { return "Something" }
844+
}
845+
846+
ErrorBridgingTests.test("Swift Error bridged to NSError description") {
847+
func checkDescription() {
848+
let bridgedError = SwiftError.something as NSError
849+
expectEqual("Something", bridgedError.description)
850+
}
851+
852+
if #available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *) {
853+
checkDescription()
854+
}
855+
}
856+
839857
runAllTests()

0 commit comments

Comments
 (0)