Skip to content

Commit c7088ff

Browse files
committed
[CSDiagnostics] Small cleanup in tryProtocolConformanceFixIt()
1 parent 5a1f06a commit c7088ff

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2457,6 +2457,7 @@ bool ContextualFailure::tryProtocolConformanceFixIt(
24572457
layout = unwrappedToType->getExistentialLayout();
24582458
requiresClass = layout.requiresClass();
24592459
}
2460+
24602461
if (requiresClass && !FromType->is<ClassType>()) {
24612462
return false;
24622463
}
@@ -2470,8 +2471,7 @@ bool ContextualFailure::tryProtocolConformanceFixIt(
24702471

24712472
diagnostic.flush();
24722473

2473-
// Let's build a list of protocols that the contextual type does not
2474-
// conform to.
2474+
// Let's build a list of protocols that the context does not conform to.
24752475
SmallVector<std::string, 8> missingProtoTypeStrings;
24762476
for (auto protocol : layout.getProtocols()) {
24772477
if (!getTypeChecker().conformsToProtocol(
@@ -2483,19 +2483,22 @@ bool ContextualFailure::tryProtocolConformanceFixIt(
24832483

24842484
// If we have a protocol composition type and we don't conform to all
24852485
// the protocols of the composition, then store the composition directly.
2486+
// This is because we need to append 'Foo & Bar' instead of 'Foo, Bar' in
2487+
// order to match the written type.
24862488
if (auto compositionTy = unwrappedToType->getAs<ProtocolCompositionType>()) {
24872489
if (compositionTy->getMembers().size() == missingProtoTypeStrings.size()) {
24882490
missingProtoTypeStrings = {compositionTy->getString()};
24892491
}
24902492
}
24912493

2494+
assert(!missingProtoTypeStrings.empty() &&
2495+
"type already conforms to all the protocols?");
2496+
24922497
// If we didn't have a protocol composition type, it means we only have a
24932498
// single protocol, so just use it directly. Otherwise, construct a comma
24942499
// separated list of missing types.
24952500
std::string protoString;
2496-
if (missingProtoTypeStrings.empty()) {
2497-
protoString = unwrappedToType->getString();
2498-
} else if (missingProtoTypeStrings.size() == 1) {
2501+
if (missingProtoTypeStrings.size() == 1) {
24992502
protoString = missingProtoTypeStrings.front();
25002503
} else {
25012504
protoString = llvm::join(missingProtoTypeStrings, ", ");

0 commit comments

Comments
 (0)