Skip to content

[Runtime] Handle dynamic casting to NSObject via error bridging. #28353

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 2 commits into from
Nov 19, 2019
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
8 changes: 5 additions & 3 deletions stdlib/public/runtime/Casting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "llvm/Support/Compiler.h"
#if SWIFT_OBJC_INTEROP
#include "swift/Runtime/ObjCBridge.h"
#include "SwiftObject.h"
#include "SwiftValue.h"
#endif

Expand Down Expand Up @@ -2330,9 +2331,10 @@ static bool swift_dynamicCastImpl(OpaqueValue *dest, OpaqueValue *src,
case MetadataKind::Class:
case MetadataKind::ObjCClassWrapper:
#if SWIFT_OBJC_INTEROP
// If the destination type is an NSError, and the source type is an
// Error, then the cast can succeed by NSError bridging.
if (targetType == getNSErrorMetadata()) {
// If the destination type is an NSError or NSObject, and the source type
// is an Error, then the cast can succeed by NSError bridging.
if (targetType == getNSErrorMetadata() ||
targetType == getNSObjectMetadata()) {
// Don't rebridge if the source is already some kind of NSError.
if (srcType->isAnyClass()
&& swift_dynamicCastObjCClass(*reinterpret_cast<id*>(src),
Expand Down
9 changes: 9 additions & 0 deletions stdlib/public/runtime/SwiftObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@


#if SWIFT_OBJC_INTEROP
#if __OBJC__

// Source code: "SwiftObject"
// Real class name: mangled "Swift._SwiftObject"
Expand Down Expand Up @@ -83,5 +84,13 @@ id getDescription(OpaqueValue *value, const Metadata *type);
}

#endif
#endif

namespace swift {

/// Get the NSObject metadata.
const Metadata *getNSObjectMetadata();

}

#endif
5 changes: 5 additions & 0 deletions stdlib/public/runtime/SwiftObject.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1552,6 +1552,11 @@ void swift_objc_swift3ImplicitObjCEntrypoint(id self, SEL selector,
free(nullTerminatedFilename);
}

const Metadata *swift::getNSObjectMetadata() {
return SWIFT_LAZY_CONSTANT(
swift_getObjCClassMetadata((const ClassMetadata *)[NSObject class]));
}

#endif

const ClassMetadata *swift::getRootSuperclass() {
Expand Down
25 changes: 25 additions & 0 deletions test/stdlib/ErrorBridged.swift
Original file line number Diff line number Diff line change
Expand Up @@ -767,4 +767,29 @@ ErrorBridgingTests.test("@objc error domains for nested types") {
String(reflecting: NonPrintAsObjCError.self))
}

@inline(never)
@_optimize(none)
func anyToAny<T, U>(_ a: T, _ : U.Type) -> U {
return a as! U
}

ErrorBridgingTests.test("error-to-NSObject casts") {
let error = MyCustomizedError(code: 12345)

// Unconditional cast
let nsErrorAsObject1 = unconditionalCast(error, to: NSObject.self)
let nsError1 = unconditionalCast(nsErrorAsObject1, to: NSError.self)
expectEqual("custom", nsError1.domain)
expectEqual(12345, nsError1.code)

// Conditional cast
let nsErrorAsObject2 = conditionalCast(error, to: NSObject.self)!
let nsError2 = unconditionalCast(nsErrorAsObject2, to: NSError.self)
expectEqual("custom", nsError2.domain)
expectEqual(12345, nsError2.code)

// "is" check
expectTrue(error is NSObject)
}

runAllTests()