@@ -4412,7 +4412,8 @@ generateForEachStmtConstraints(ConstraintSystem &cs, DeclContext *dc,
4412
4412
4413
4413
static llvm::Optional<SequenceIterationInfo>
4414
4414
generateForEachStmtConstraints (ConstraintSystem &cs, DeclContext *dc,
4415
- ForEachStmt *stmt, Type patternType) {
4415
+ ForEachStmt *stmt, Pattern *typeCheckedPattern,
4416
+ bool shouldBindPatternVarsOneWay) {
4416
4417
ASTContext &ctx = cs.getASTContext ();
4417
4418
auto *forEachExpr = stmt->getParsedSequence ();
4418
4419
bool isAsync = stmt->getAwaitLoc ().isValid ();
@@ -4537,7 +4538,12 @@ generateForEachStmtConstraints(ConstraintSystem &cs, DeclContext *dc,
4537
4538
cs.setTargetFor (sequenceIterationInfo.nextCall , nextTarget);
4538
4539
}
4539
4540
4540
- Type initType = patternType;
4541
+ // Create a type variable for a named pattern
4542
+ Type initType =
4543
+ cs.generateConstraints (typeCheckedPattern, elementLocator,
4544
+ shouldBindPatternVarsOneWay, nullptr , 0 );
4545
+ if (!initType)
4546
+ return llvm::None;
4541
4547
4542
4548
// Add a conversion constraint between the element type of the sequence
4543
4549
// and the type of the element pattern.
@@ -4567,12 +4573,12 @@ generateForEachStmtConstraints(ConstraintSystem &cs,
4567
4573
SyntacticElementTarget target) {
4568
4574
ASTContext &ctx = cs.getASTContext ();
4569
4575
ForEachStmt *stmt = target.getAsForEachStmt ();
4570
- auto *forEachExpr = stmt->getParsedSequence ();
4576
+ auto *sequenceExpr = stmt->getParsedSequence ();
4571
4577
auto *dc = target.getDeclContext ();
4572
4578
4573
4579
// Collect constraints from the element pattern.
4574
4580
auto elementLocator = cs.getConstraintLocator (
4575
- forEachExpr , ConstraintLocator::SequenceElementType);
4581
+ sequenceExpr , ConstraintLocator::SequenceElementType);
4576
4582
4577
4583
Pattern *pattern = TypeChecker::resolvePattern (stmt->getPattern (), dc,
4578
4584
/* isStmtCondition*/ false );
@@ -4585,23 +4591,24 @@ generateForEachStmtConstraints(ConstraintSystem &cs,
4585
4591
if (TypeChecker::typeCheckPattern (contextualPattern)->hasError ()) {
4586
4592
return llvm::None;
4587
4593
}
4588
- // Create a type variable for a named pattern
4589
- Type patternType =
4590
- cs.generateConstraints (pattern, elementLocator,
4591
- target.shouldBindPatternVarsOneWay (), nullptr , 0 );
4592
- if (!patternType)
4593
- return llvm::None;
4594
4594
4595
4595
auto forEachLocator = cs.getConstraintLocator (stmt);
4596
4596
4597
- if (isa<PackExpansionExpr>(forEachExpr )) {
4597
+ if (isa<PackExpansionExpr>(sequenceExpr )) {
4598
4598
if (!ctx.LangOpts .hasFeature (Feature::PackIteration)) {
4599
4599
if (!cs.shouldAttemptFixes ())
4600
4600
return llvm::None;
4601
4601
4602
4602
cs.recordFix (AllowInvalidPackExpansion::create (cs, forEachLocator));
4603
4603
}
4604
- auto *expansion = cast<PackExpansionExpr>(forEachExpr);
4604
+ auto *expansion = cast<PackExpansionExpr>(sequenceExpr);
4605
+
4606
+ // Create a type variable for a named pattern
4607
+ Type patternType = cs.generateConstraints (
4608
+ pattern, elementLocator, target.shouldBindPatternVarsOneWay (), nullptr ,
4609
+ 0 );
4610
+ if (!patternType)
4611
+ return llvm::None;
4605
4612
4606
4613
auto packIterationInfo =
4607
4614
generateForEachStmtConstraints (cs, dc, expansion, patternType);
@@ -4611,8 +4618,8 @@ generateForEachStmtConstraints(ConstraintSystem &cs,
4611
4618
4612
4619
target.getForEachStmtInfo () = *packIterationInfo;
4613
4620
} else {
4614
- auto sequenceIterationInfo =
4615
- generateForEachStmtConstraints ( cs, dc, stmt, patternType );
4621
+ auto sequenceIterationInfo = generateForEachStmtConstraints (
4622
+ cs, dc, stmt, pattern, target. shouldBindPatternVarsOneWay () );
4616
4623
if (!sequenceIterationInfo) {
4617
4624
return llvm::None;
4618
4625
}
0 commit comments