@@ -2452,8 +2452,10 @@ bool ContextualFailure::tryProtocolConformanceFixIt(
2452
2452
// If the protocol requires a class & we don't have one (maybe the context
2453
2453
// is a struct), then bail out instead of offering a broken fix-it later on.
2454
2454
auto requiresClass = false ;
2455
+ ExistentialLayout layout;
2455
2456
if (unwrappedToType->isExistentialType ()) {
2456
- requiresClass = unwrappedToType->getExistentialLayout ().requiresClass ();
2457
+ layout = unwrappedToType->getExistentialLayout ();
2458
+ requiresClass = layout.requiresClass ();
2457
2459
}
2458
2460
if (requiresClass && !FromType->is <ClassType>()) {
2459
2461
return false ;
@@ -2469,23 +2471,20 @@ bool ContextualFailure::tryProtocolConformanceFixIt(
2469
2471
diagnostic.flush ();
2470
2472
2471
2473
// 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.
2475
2475
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 ());
2484
2481
}
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 ()) {
2489
2488
missingProtoTypeStrings = {compositionTy->getString ()};
2490
2489
}
2491
2490
}
0 commit comments