@@ -94,6 +94,25 @@ class ClosureConstraintGenerator
94
94
visit (doStmt->getBody ());
95
95
}
96
96
97
+ void visitForEachStmt (ForEachStmt *forEachStmt) {
98
+ auto sequenceProto = TypeChecker::getProtocol (
99
+ closure->getASTContext (), forEachStmt->getForLoc (),
100
+ KnownProtocolKind::Sequence);
101
+ assert (sequenceProto && " Missing Sequence protocol" );
102
+
103
+ // Generate constraints for the loop header. This also wires up the
104
+ // types for the patterns.
105
+ auto target = SolutionApplicationTarget::forForEachStmt (
106
+ forEachStmt, sequenceProto, closure, /* bindPatternVarsOneWay=*/ true );
107
+ if (cs.generateConstraints (target, FreeTypeVariableBinding::Disallow)) {
108
+ hadError = true ;
109
+ }
110
+
111
+ cs.setSolutionApplicationTarget (forEachStmt, target);
112
+
113
+ visit (forEachStmt->getBody ());
114
+ }
115
+
97
116
void visitGuardStmt (GuardStmt *guardStmt) {
98
117
if (cs.generateConstraints (guardStmt->getCond (), closure))
99
118
hadError = true ;
@@ -159,7 +178,6 @@ class ClosureConstraintGenerator
159
178
UNSUPPORTED_STMT (Yield)
160
179
UNSUPPORTED_STMT (Defer)
161
180
UNSUPPORTED_STMT (DoCatch)
162
- UNSUPPORTED_STMT (ForEach)
163
181
UNSUPPORTED_STMT (Switch)
164
182
UNSUPPORTED_STMT (Case)
165
183
UNSUPPORTED_STMT (Break)
@@ -256,6 +274,19 @@ class ClosureConstraintApplication
256
274
return doStmt;
257
275
}
258
276
277
+ ASTNode visitForEachStmt (ForEachStmt *forEachStmt) {
278
+ ConstraintSystem &cs = solution.getConstraintSystem ();
279
+ auto forEachTarget =
280
+ rewriteTarget (*cs.getSolutionApplicationTarget (forEachStmt));
281
+ if (!forEachTarget)
282
+ hadError = true ;
283
+
284
+ auto body = visit (forEachStmt->getBody ()).get <Stmt *>();
285
+ forEachStmt->setBody (cast<BraceStmt>(body));
286
+
287
+ return forEachStmt;
288
+ }
289
+
259
290
ASTNode visitGuardStmt (GuardStmt *guardStmt) {
260
291
// Rewrite the condition.
261
292
if (auto condition = rewriteTarget (
@@ -387,7 +418,6 @@ class ClosureConstraintApplication
387
418
UNSUPPORTED_STMT (Yield)
388
419
UNSUPPORTED_STMT(Defer)
389
420
UNSUPPORTED_STMT(DoCatch)
390
- UNSUPPORTED_STMT(ForEach)
391
421
UNSUPPORTED_STMT(Switch)
392
422
UNSUPPORTED_STMT(Case)
393
423
UNSUPPORTED_STMT(Break)
0 commit comments