Skip to content

Commit 9a05827

Browse files
committed
[TypeChecker] ForEach: Don't diagnose unrelated errors if type doesn't conform to Sequence
If the type used in for-each loop doesn't conform to `Sequence` let's not try to diagnose anything about its element since such diagnostics would include unresolved types and actual problem (missing conformance) would already be diagnosed.
1 parent 95d0d14 commit 9a05827

File tree

3 files changed

+5
-4
lines changed

3 files changed

+5
-4
lines changed

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2857,6 +2857,11 @@ bool TypeChecker::typeCheckForEachBinding(DeclContext *dc, ForEachStmt *stmt) {
28572857
ElementType = solution.simplifyType(ElementType);
28582858
IteratorType = solution.simplifyType(IteratorType);
28592859

2860+
// If the type doesn't conform to Sequence we'll get its element type
2861+
// bound to `UnresolvedType` since fixes are allowed.
2862+
if (InitType->is<UnresolvedType>())
2863+
return nullptr;
2864+
28602865
cs.cacheExprTypes(expr);
28612866
Stmt->setSequence(expr);
28622867
solution.setExprTypes(expr);

test/Constraints/generic_protocol_witness.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,4 @@ struct L<T>: Sequence {} // expected-error {{type 'L<T>' does not conform to pro
6060

6161
func z(_ x: L<Int>) {
6262
for xx in x {}
63-
// expected-warning@-1{{immutable value 'xx' was never used; consider replacing with '_' or removing it}}
6463
}

test/stmt/foreach.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ struct BadContainer2 : Sequence { // expected-error{{type 'BadContainer2' does n
1414

1515
func bad_containers_2(bc: BadContainer2) {
1616
for e in bc { }
17-
// expected-warning@-1 {{immutable value 'e' was never used; consider replacing with '_' or removing it}}
1817
}
1918

2019
struct BadContainer3 : Sequence { // expected-error{{type 'BadContainer3' does not conform to protocol 'Sequence'}}
@@ -23,7 +22,6 @@ struct BadContainer3 : Sequence { // expected-error{{type 'BadContainer3' does n
2322

2423
func bad_containers_3(bc: BadContainer3) {
2524
for e in bc { }
26-
// expected-warning@-1 {{immutable value 'e' was never used; consider replacing with '_' or removing it}}
2725
}
2826

2927
struct BadIterator1 {}
@@ -35,7 +33,6 @@ struct BadContainer4 : Sequence { // expected-error{{type 'BadContainer4' does n
3533

3634
func bad_containers_4(bc: BadContainer4) {
3735
for e in bc { }
38-
// expected-warning@-1 {{immutable value 'e' was never used; consider replacing with '_' or removing it}}
3936
}
4037

4138
// Pattern type-checking

0 commit comments

Comments
 (0)