Skip to content

Revert "Obj-C class metatypes should never satisfy Obj-C existentials" #71065

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 1 commit into from
Jan 23, 2024
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
13 changes: 0 additions & 13 deletions stdlib/public/runtime/SwiftObject.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1353,15 +1353,6 @@ id swift_dynamicCastObjCProtocolUnconditional(id object,
Protocol * const *protocols,
const char *filename,
unsigned line, unsigned column) {
if (numProtocols == 0) {
return object;
}
if (object_isClass(object)) {
// ObjC classes never conform to protocols
Class sourceType = object_getClass(object);
swift_dynamicCastFailure(sourceType, class_getName(sourceType),
protocols[0], protocol_getName(protocols[0]));
}
for (size_t i = 0; i < numProtocols; ++i) {
if (![object conformsToProtocol:protocols[i]]) {
Class sourceType = object_getClass(object);
Expand All @@ -1383,10 +1374,6 @@ id swift_dynamicCastObjCProtocolConditional(id object,
return nil;
}
}
if (object_isClass(object)) {
// ObjC classes never conform to protocols
return nil;
}
for (size_t i = 0; i < numProtocols; ++i) {
if (![object conformsToProtocol:protocols[i]]) {
return nil;
Expand Down
19 changes: 0 additions & 19 deletions test/Casting/Casts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1081,23 +1081,4 @@ CastsTests.test("type(of:) should look through __SwiftValue")
expectEqual(t, "S") // Fails: currently says `__SwiftValue`
}

#if _runtime(_ObjC)
@objc protocol P106973771 {
func sayHello()
}
CastsTests.test("Class metatype values should not cast to Obj-C existentials") {
class C106973771: NSObject, P106973771 {
func sayHello() { print("Hello") }
}
// A class instance clearly conforms to the protocol
expectTrue(C106973771() is any P106973771)
// But the metatype definitely does not
expectFalse(C106973771.self is any P106973771)
// The cast should not succeed
expectNil(C106973771.self as? any P106973771)
// The following will crash if the cast succeeds
(C106973771.self as? any P106973771)?.sayHello()
}
#endif

runAllTests()