Skip to content

Commit 5ca9a20

Browse files
committed
[ConstraintSystem] Use missing protocol fix for sequence element mismatches related to protocols
(cherry picked from commit e76ae9c)
1 parent c8e1479 commit 5ca9a20

File tree

4 files changed

+12
-2
lines changed

4 files changed

+12
-2
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ ERROR(cannot_convert_sequence_element_value,none,
456456
"cannot convert sequence element type %0 to expected type %1",
457457
(Type, Type))
458458
ERROR(cannot_convert_sequence_element_protocol,none,
459-
"sequence element type %0 does not conform to expected type %1",
459+
"sequence element type %0 does not conform to expected protocol %1",
460460
(Type, Type))
461461

462462
ERROR(throws_functiontype_mismatch,none,

lib/Sema/CSDiagnostics.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2832,6 +2832,11 @@ bool MissingContextualConformanceFailure::diagnoseAsError() {
28322832
diagnostic = getDiagnosticFor(Context);
28332833
break;
28342834

2835+
case ConstraintLocator::SequenceElementType: {
2836+
diagnostic = diag::cannot_convert_sequence_element_protocol;
2837+
break;
2838+
}
2839+
28352840
default:
28362841
break;
28372842
}

lib/Sema/CSSimplify.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2192,6 +2192,11 @@ bool ConstraintSystem::repairFailures(
21922192
}
21932193

21942194
case ConstraintLocator::SequenceElementType: {
2195+
// This is going to be diagnosed as `missing conformance`,
2196+
// so no need to create duplicate fixes.
2197+
if (rhs->isExistentialType())
2198+
break;
2199+
21952200
conversionsOrFixes.push_back(CollectionElementContextualMismatch::create(
21962201
*this, lhs, rhs, getConstraintLocator(locator)));
21972202
break;

test/stmt/foreach.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func patterns(gir: GoodRange<Int>, gtr: GoodTupleIterator) {
6161
for i : Int in gir { sum = sum + i }
6262
for i in gir { sum = sum + i }
6363
for f : Float in gir { sum = sum + f } // expected-error{{cannot convert sequence element type 'Int' to expected type 'Float'}}
64-
for f : ElementProtocol in gir { } // expected-error {{sequence element type 'Int' does not conform to expected type 'ElementProtocol'}}
64+
for f : ElementProtocol in gir { } // expected-error {{sequence element type 'Int' does not conform to expected protocol 'ElementProtocol'}}
6565

6666
for (i, f) : (Int, Float) in gtr { sum = sum + i }
6767

0 commit comments

Comments
 (0)