Skip to content

Commit 3bdbbb5

Browse files
authored
Merge pull request swiftlang#68952 from tbkka/tbkka-rdar111422725-more-SwiftValue-casting
[Casting] Make more casts look inside `__SwiftValue`
2 parents 1a6d76e + 7af257a commit 3bdbbb5

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

stdlib/public/runtime/SwiftObject.mm

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,22 @@ static bool isObjCForUnownedReference(void *value) {
10931093
if (object == nullptr)
10941094
return nullptr;
10951095

1096+
if ([id_const_cast(object) isKindOfClass:[__SwiftValue class]]) {
1097+
// Source is a `__SwiftValue` container
1098+
// Unwrap, then use the most general casting machine to do the heavy lifting
1099+
auto typeValue = getValueFromSwiftValue(reinterpret_cast<__SwiftValue *>(object));
1100+
const void *result = nullptr;
1101+
if (swift_dynamicCast(reinterpret_cast<OpaqueValue *>(&result),
1102+
const_cast<OpaqueValue *>(typeValue.second),
1103+
typeValue.first,
1104+
targetType,
1105+
DynamicCastFlags::TakeOnSuccess)) {
1106+
return result;
1107+
} else {
1108+
return nullptr;
1109+
}
1110+
}
1111+
10961112
if ([id_const_cast(object) isKindOfClass:class_const_cast(targetType)]) {
10971113
return object;
10981114
}

test/Casting/Casts.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,4 +1064,21 @@ CastsTests.test("Don't put AnyHashable inside AnyObject") {
10641064
expectTrue(a === d)
10651065
}
10661066

1067+
#if _runtime(_ObjC)
1068+
CastsTests.test("__SwiftValue should not be obvious to `is`") {
1069+
struct S {}
1070+
let s = S() as AnyObject
1071+
expectFalse(s is NSObject)
1072+
}
1073+
#endif
1074+
1075+
CastsTests.test("type(of:) should look through __SwiftValue")
1076+
.xfail(.always("Known to be broken"))
1077+
.code {
1078+
struct S {}
1079+
let s = S() as AnyObject
1080+
let t = "\(type(of: s))"
1081+
expectEqual(t, "S") // Fails: currently says `__SwiftValue`
1082+
}
1083+
10671084
runAllTests()

0 commit comments

Comments
 (0)