<div dir="ltr">Hi all,<div><br></div><div><a href="https://github.com/apple/swift/blob/3d2b5bcc5350e1dea2ed8a0a95cd12ff5c760f24/stdlib/public/runtime/Casting.cpp#L508">swift_dynamicCastClassUnconditional</a> and <a href="https://github.com/apple/swift/blob/2daa1400cf79a2965eb07034b48ef7fae02459fd/stdlib/public/runtime/SwiftObject.mm#L1155">swift_dynamicCastObjCClassUnconditional</a> methods behave differently while verifying casting.</div><div><br></div><div><a href="https://github.com/apple/swift/blob/2daa1400cf79a2965eb07034b48ef7fae02459fd/stdlib/public/runtime/SwiftObject.mm#L1155">swift_dynamicCastObjCClassUnconditional</a> method calls <i>-isKindOfClass:</i> method before falling back to <i>object_getClass</i> function.<br></div><div><br></div><div><a href="https://github.com/apple/swift/blob/3d2b5bcc5350e1dea2ed8a0a95cd12ff5c760f24/stdlib/public/runtime/Casting.cpp#L508">swift_dynamicCastClassUnconditional</a> method calls <i>swift_dynamicCastClass</i> method which calls <i><a href="https://github.com/apple/swift/blob/82509cbd7451e72fb99d22556ad259ceb335cb1f/stdlib/public/runtime/Private.h#L80">_swift_getClassOfAllocated</a></i> method which calls directly <i>object_getClass</i> function.<br></div><div><br></div><div>This causes problems if underlying object is an NSProxy subclass.</div><div><br></div><div>NSProxy class does not implement <i>-isKindOfClass:</i> method, so calls to this method are forwarded to the real object through <i>-forwardInvocation:</i> method, which causes <i>-isKindOfClass:</i> method to return answer according to the real object&#39;s class.</div><div><br></div><div>However, <i>object_getClass</i> function directly accesses to the metadata of the given object, so it returns NSProxy subclass.</div><div><br></div><div>I think this is a conflicting behavior, and I think <a href="https://github.com/apple/swift/blob/3d2b5bcc5350e1dea2ed8a0a95cd12ff5c760f24/stdlib/public/runtime/Casting.cpp#L508" style="outline:transparent solid 1px">swift_dynamicCastClassUnconditional</a> method should also verify first using <i>-isKindOfClass:</i> method, in order to provide consistency.</div><div><br></div><div>What do you think?</div><div><br></div><div>Best</div></div>