Skip to content

Runtime: Let structural types be cast out of boxed AnyObjects too. #3829

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
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
20 changes: 9 additions & 11 deletions stdlib/public/runtime/Casting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2281,6 +2281,14 @@ bool swift::swift_dynamicCast(OpaqueValue *dest,
if (!srcType)
return unwrapResult.success;

#if SWIFT_OBJC_INTEROP
// A class or AnyObject reference may point at a boxed SwiftValue.
if (tryDynamicCastBoxedSwiftValue(dest, src, srcType,
targetType, flags)) {
return true;
}
#endif

switch (targetType->getKind()) {
// Handle wrapping an Optional target.
case MetadataKind::Optional: {
Expand Down Expand Up @@ -2411,17 +2419,6 @@ bool swift::swift_dynamicCast(OpaqueValue *dest,
if (tryDynamicCastNSErrorToValue(dest, src, srcType, targetType, flags)) {
return true;
}
#endif
SWIFT_FALLTHROUGH;
}

case MetadataKind::Existential: {
#if SWIFT_OBJC_INTEROP
// A class or AnyObject reference may point at a boxed SwiftValue.
if (tryDynamicCastBoxedSwiftValue(dest, src, srcType,
targetType, flags)) {
return true;
}
#endif
break;
}
Expand All @@ -2436,6 +2433,7 @@ bool swift::swift_dynamicCast(OpaqueValue *dest,
}
break;

case MetadataKind::Existential:
case MetadataKind::ExistentialMetatype:
case MetadataKind::Enum:
case MetadataKind::Optional:
Expand Down
33 changes: 29 additions & 4 deletions test/1_stdlib/BridgeIdAsAny.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,41 @@ func boxedTypeRoundTripsThroughDynamicCasting(original: KnownUnbridged,
expectEqual(bridged as? String, nil)
}

func tupleCanBeDynamicallyCast(original: (Int, String),
bridged: AnyObject) {
expectTrue(original == bridged as! (Int, String))
}
func metatypeCanBeDynamicallyCast(original: Int.Type,
bridged: AnyObject) {
expectTrue(original == bridged as! Int.Type)
expectTrue(original == bridged as! Any.Type)
}

func guineaPigFunction() -> Int {
return 1738
}

func functionCanBeDynamicallyCast(original: () -> Int,
bridged: AnyObject) {
expectEqual(original(), (bridged as! () -> Int)())
expectEqual(original(), try! (bridged as! () throws -> Int)())
}

// We want to exhaustively check all paths through the bridging and dynamic
// casting infrastructure, so expand out test cases that wrap the different
// interesting bridging cases in different kinds of existential container.
%{
testCases = [
("classes", "LifetimeTracked(0)", "bridgedObjectPreservesIdentity"),
("strings", '"vitameatavegamin"', "stringBridgesToEqualNSString"),
("unbridged type", "KnownUnbridged()", "boxedTypeRoundTripsThroughDynamicCasting"),
("classes", "LifetimeTracked(0)", "bridgedObjectPreservesIdentity", True),
("strings", '"vitameatavegamin"', "stringBridgesToEqualNSString", True),
("unbridged type", "KnownUnbridged()", "boxedTypeRoundTripsThroughDynamicCasting", True),
("tuple", '(1, "2")', "tupleCanBeDynamicallyCast", False),
("metatype", 'Int.self', "metatypeCanBeDynamicallyCast", False),
("function", 'guineaPigFunction', "functionCanBeDynamicallyCast", False),
]
}%

% for testName, valueExpr, testFunc in testCases:
% for testName, valueExpr, testFunc, conformsToError in testCases:
BridgeAnything.test("${testName}") {
do {
let x = ${valueExpr}
Expand All @@ -120,13 +143,15 @@ BridgeAnything.test("${testName}") {
${testFunc}(original: x, bridged: _bridgeAnythingToObjectiveC(xInAnyInAny))
${testFunc}(original: x, bridged: _bridgeAnythingNonVerbatimToObjectiveC(xInAnyInAny))

% if conformsToError:
let xInError: Error = x
${testFunc}(original: x, bridged: _bridgeAnythingToObjectiveC(xInError))
${testFunc}(original: x, bridged: _bridgeAnythingNonVerbatimToObjectiveC(xInError))

let xInErrorInAny = wantonlyWrapInAny(xInError)
${testFunc}(original: x, bridged: _bridgeAnythingToObjectiveC(xInErrorInAny))
${testFunc}(original: x, bridged: _bridgeAnythingNonVerbatimToObjectiveC(xInErrorInAny))
% end
}
}
% end
Expand Down