[Runtime] Use _conformsToProtocols instead of manually checking protocol conformance.

This is cleaner and it fixes a bunch of cases the old code didn't handle: @objc protocols, class bounds, and superclass constraints.

rdar://problem/56044443
This commit is contained in:
Mike Ash
2019-10-10 14:14:41 -04:00
parent d56173eb14
commit 3ff277d31d
2 changed files with 70 additions and 22 deletions

View File

@@ -403,7 +403,7 @@ static bool _conformsToProtocols(const OpaqueValue *value,
for (auto protocol : existentialType->getProtocols()) {
if (!_conformsToProtocol(value, type, protocol, conformances))
return false;
if (protocol.needsWitnessTable()) {
if (conformances != nullptr && protocol.needsWitnessTable()) {
assert(*conformances != nullptr);
++conformances;
}
@@ -1117,9 +1117,8 @@ swift_dynamicCastMetatypeImpl(const Metadata *sourceType,
case MetadataKind::Existential: {
auto targetTypeAsExistential = static_cast<const ExistentialTypeMetadata *>(targetType);
for (auto protocol : targetTypeAsExistential->getProtocols())
if (!swift_conformsToProtocol(sourceType, protocol.getSwiftProtocol()))
return nullptr;
if (!_conformsToProtocols(nullptr, sourceType, targetTypeAsExistential, nullptr))
return nullptr;
return origSourceType;
}