Skip to content

Commit f8f9489

Browse files
authored
Merge pull request swiftlang#35149 from tbkka/tbkka/sketch-in-hook-for-NSString-to-AnyHashable
2 parents fcc31e9 + e5c4e2d commit f8f9489

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

stdlib/public/runtime/DynamicCast.cpp

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -724,15 +724,33 @@ tryCastToAnyHashable(
724724
assert(cast<StructMetadata>(destType)->Description
725725
== &STRUCT_TYPE_DESCR_SYM(s11AnyHashable));
726726

727-
auto hashableConformance = reinterpret_cast<const HashableWitnessTable *>(
727+
switch (srcType->getKind()) {
728+
case MetadataKind::ForeignClass: // CF -> String
729+
case MetadataKind::ObjCClassWrapper: { // Obj-C -> String
730+
#if SWIFT_OBJC_INTEROP
731+
// TODO: Implement a fast path for NSString->AnyHashable casts.
732+
// These are incredibly common because an NSDictionary with
733+
// NSString keys is bridged by default to [AnyHashable:Any].
734+
// Until this is implemented, fall through to the default case
735+
SWIFT_FALLTHROUGH;
736+
#else
737+
// If no Obj-C interop, just fall through to the default case.
738+
SWIFT_FALLTHROUGH;
739+
#endif
740+
}
741+
default: {
742+
auto hashableConformance = reinterpret_cast<const HashableWitnessTable *>(
728743
swift_conformsToProtocol(srcType, &HashableProtocolDescriptor));
729-
if (hashableConformance) {
730-
_swift_convertToAnyHashableIndirect(srcValue, destLocation,
731-
srcType, hashableConformance);
732-
return DynamicCastResult::SuccessViaCopy;
733-
} else {
734-
return DynamicCastResult::Failure;
744+
if (hashableConformance) {
745+
_swift_convertToAnyHashableIndirect(srcValue, destLocation,
746+
srcType, hashableConformance);
747+
return DynamicCastResult::SuccessViaCopy;
748+
} else {
749+
return DynamicCastResult::Failure;
750+
}
735751
}
752+
}
753+
return DynamicCastResult::Failure;
736754
}
737755

738756
static DynamicCastResult

0 commit comments

Comments
 (0)