Skip to content

[Runtime] Check for @objc existentials conforming to @objc protocols. #20217

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
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: 18 additions & 2 deletions stdlib/public/runtime/Casting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,24 @@ bool swift::_conformsToProtocol(const OpaqueValue *value,
#endif


case MetadataKind::Existential: // FIXME
case MetadataKind::ExistentialMetatype: // FIXME
case MetadataKind::Existential: {
#if SWIFT_OBJC_INTEROP
// If all protocols are @objc and at least one of them conforms to the
// protocol, succeed.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. What about non-@objc protocols that inherit from @objc protocols?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only @objc existentials can conform to protocols. A non-@objc protocol does not, even if it inherits from an @objc protocol. For example, the type checker rightly rejects this:

@objc protocol P { }
protocol Q: P { }
struct X<T:P> { }
typealias Y = X<Q>

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, right, we aren't unboxing anything in this cast.

auto existential = cast<ExistentialTypeMetadata>(type);
if (!existential->isObjC())
return false;
for (auto existentialProto : existential->getProtocols()) {
if (protocol_conformsToProtocol(existentialProto.getObjCProtocol(),
protocol.getObjCProtocol()))
return true;
}
#endif

return false;
}

case MetadataKind::ExistentialMetatype:
default:
return false;
}
Expand Down
7 changes: 7 additions & 0 deletions test/Runtime/demangleToMetadataObjC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ let DemangleToMetadataTests = TestSuite("DemangleToMetadataObjC")
@objc enum E: Int { case a }
@objc protocol P1 { }
protocol P2 { }
@objc protocol P3: P1 { }

DemangleToMetadataTests.test("@objc classes") {
expectEqual(type(of: C()), _typeByMangledName("4main1CC")!)
Expand Down Expand Up @@ -96,5 +97,11 @@ DemangleToMetadataTests.test("runtime conformance lookup via foreign superclasse
_typeByMangledName("ShySo18CFMutableStringRefaG")!)
}

class F<T: P1> { }

DemangleToMetadataTests.test("runtime conformance check for @objc protocol inheritance") {
expectEqual(F<P3>.self, _typeByMangledName("4main1FCyAA2P3PG")!)
}

runAllTests()