Skip to content

[Runtime] Error bridged to NSError should return its description rather than NSError description #29224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions stdlib/public/runtime/ErrorObject.mm
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,20 @@
#include "swift/Runtime/Config.h"

#if SWIFT_OBJC_INTEROP
#include "ErrorObject.h"
#include "Private.h"
#include "SwiftObject.h"
#include "swift/Basic/Lazy.h"
#include "swift/Demangling/ManglingMacros.h"
#include "swift/Runtime/Casting.h"
#include "swift/Runtime/Debug.h"
#include "swift/Runtime/ObjCBridge.h"
#include "swift/Basic/Lazy.h"
#include "swift/Demangling/ManglingMacros.h"
#include "ErrorObject.h"
#include "Private.h"
#include <Foundation/Foundation.h>
#include <dlfcn.h>
#include <objc/NSObject.h>
#include <objc/runtime.h>
#include <objc/message.h>
#include <objc/objc.h>
#include <Foundation/Foundation.h>
#include <objc/runtime.h>

using namespace swift;
using namespace swift::hashable_support;
Expand Down Expand Up @@ -99,6 +100,12 @@ - (void)dealloc {
return cf_const_cast<id>(domain);
}

- (id /* NSString */)description {
auto error = (const SwiftError *)self;
return getDescription(const_cast<OpaqueValue *>(error->getValue()),
error->type);
}

- (NSInteger)code {
auto error = (const SwiftError*)self;
return error->code.load(SWIFT_MEMORY_ORDER_CONSUME);
Expand Down
18 changes: 18 additions & 0 deletions test/stdlib/ErrorBridged.swift
Original file line number Diff line number Diff line change
Expand Up @@ -836,4 +836,22 @@ ErrorBridgingTests.test("SR-9207 crash in failed cast to NSError") {
}
}

// SR-7652

enum SwiftError: Error, CustomStringConvertible {
case something
var description: String { return "Something" }
}

ErrorBridgingTests.test("Swift Error bridged to NSError description") {
func checkDescription() {
let bridgedError = SwiftError.something as NSError
expectEqual("Something", bridgedError.description)
}

if #available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *) {
checkDescription()
}
}

runAllTests()