Skip to content

Commit 5a1f06a

Browse files
committed
[CSDiagnostics] Use the existential layout to get the protocols
1 parent 666c000 commit 5a1f06a

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2452,8 +2452,10 @@ bool ContextualFailure::tryProtocolConformanceFixIt(
24522452
// If the protocol requires a class & we don't have one (maybe the context
24532453
// is a struct), then bail out instead of offering a broken fix-it later on.
24542454
auto requiresClass = false;
2455+
ExistentialLayout layout;
24552456
if (unwrappedToType->isExistentialType()) {
2456-
requiresClass = unwrappedToType->getExistentialLayout().requiresClass();
2457+
layout = unwrappedToType->getExistentialLayout();
2458+
requiresClass = layout.requiresClass();
24572459
}
24582460
if (requiresClass && !FromType->is<ClassType>()) {
24592461
return false;
@@ -2469,23 +2471,20 @@ bool ContextualFailure::tryProtocolConformanceFixIt(
24692471
diagnostic.flush();
24702472

24712473
// Let's build a list of protocols that the contextual type does not
2472-
// conform to. We will start by first checking if we have a protocol
2473-
// composition type and add all the individual types that the context
2474-
// does not conform to.
2474+
// conform to.
24752475
SmallVector<std::string, 8> missingProtoTypeStrings;
2476-
if (auto compositionTy = unwrappedToType->getAs<ProtocolCompositionType>()) {
2477-
for (auto memberTy : compositionTy->getMembers()) {
2478-
auto protocol = memberTy->getAnyNominal()->getSelfProtocolDecl();
2479-
if (!getTypeChecker().conformsToProtocol(
2480-
FromType, protocol, getDC(),
2481-
ConformanceCheckFlags::InExpression)) {
2482-
missingProtoTypeStrings.push_back(memberTy->getString());
2483-
}
2476+
for (auto protocol : layout.getProtocols()) {
2477+
if (!getTypeChecker().conformsToProtocol(
2478+
FromType, protocol->getDecl(), getDC(),
2479+
ConformanceCheckFlags::InExpression)) {
2480+
missingProtoTypeStrings.push_back(protocol->getString());
24842481
}
2485-
// If we don't conform to all of the protocols in the composition, then
2486-
// store the composition type only. This is because we need to append
2487-
// 'Foo & Bar' instead of 'Foo, Bar' in order to match the written type.
2488-
if (missingProtoTypeStrings.size() == compositionTy->getMembers().size()) {
2482+
}
2483+
2484+
// If we have a protocol composition type and we don't conform to all
2485+
// the protocols of the composition, then store the composition directly.
2486+
if (auto compositionTy = unwrappedToType->getAs<ProtocolCompositionType>()) {
2487+
if (compositionTy->getMembers().size() == missingProtoTypeStrings.size()) {
24892488
missingProtoTypeStrings = {compositionTy->getString()};
24902489
}
24912490
}

0 commit comments

Comments
 (0)