Skip to content

Commit b985f3a

Browse files
committed
Fix SILGen and make it work with all RangeReplaceableCollections
1 parent e7aac5d commit b985f3a

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

lib/SILGen/SILGenExpr.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2515,6 +2515,16 @@ RValue RValueEmitter::visitEnumIsCaseExpr(EnumIsCaseExpr *E,
25152515

25162516
RValue RValueEmitter::visitSingleValueStmtExpr(SingleValueStmtExpr *E,
25172517
SGFContext C) {
2518+
if (E->getStmtKind() == SingleValueStmtExpr::Kind::For) {
2519+
auto *decl = E->getForExpressionPreamble()->ForAccumulatorDecl;
2520+
auto *binding = E->getForExpressionPreamble()->ForAccumulatorBinding;
2521+
SGF.visit(decl);
2522+
SGF.visit(binding);
2523+
SGF.emitStmt(E->getStmt());
2524+
2525+
return SGF.emitRValueForDecl(E, ConcreteDeclRef(decl), E->getType(), AccessSemantics::Ordinary);
2526+
}
2527+
25182528
auto emitStmt = [&]() {
25192529
SGF.emitStmt(E->getStmt());
25202530

lib/Sema/CSSyntacticElement.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,18 +1503,19 @@ bool ConstraintSystem::generateConstraints(SingleValueStmtExpr *E) {
15031503
Type elementType = DependentMemberType::get(resultType, sequenceProtocol->getAssociatedType(ctx.Id_Element));
15041504

15051505
addConstraint(ConstraintKind::Bind, elementTypeVar, elementType, loc);
1506-
addConstraint(ConstraintKind::Bind, resultType, ArraySliceType::get(elementTypeVar), loc);
1506+
addConstraint(ConstraintKind::Defaultable, resultType, ArraySliceType::get(elementTypeVar), loc);
15071507

15081508
auto *binding = E->getForExpressionPreamble()->ForAccumulatorBinding;
15091509

15101510
auto *initializer = binding->getInit(0);
15111511
auto target = SyntacticElementTarget::forInitialization(initializer, Type(), binding, 0, false);
1512+
setTargetFor({ binding, 0 }, target);
15121513

15131514
if (generateConstraints(target)) {
15141515
return true;
15151516
}
15161517

1517-
addConstraint(ConstraintKind::Conversion, getType(initializer), resultType, loc);
1518+
addConstraint(ConstraintKind::Bind, getType(initializer), resultType, loc);
15181519
}
15191520

15201521
// Propagate the implied result kind from the if/switch expression itself
@@ -2692,6 +2693,18 @@ bool ConstraintSystem::applySolutionToSingleValueStmt(
26922693
if (!stmt || application.hadError)
26932694
return true;
26942695

2696+
if (SVE->getStmtKind() == SingleValueStmtExpr::Kind::For) {
2697+
auto *binding = SVE->getForExpressionPreamble()->ForAccumulatorBinding;
2698+
auto target = getTargetFor({ binding, 0 }).value();
2699+
2700+
auto newTarget = rewriter.rewriteTarget(target);
2701+
if (!newTarget) {
2702+
return true;
2703+
}
2704+
2705+
binding->setInit(0, newTarget->getAsExpr());
2706+
}
2707+
26952708
SVE->setStmt(stmt);
26962709
return false;
26972710
}

lib/Sema/TypeCheckEffects.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4036,6 +4036,12 @@ class CheckEffectsCoverage : public EffectsHandlingWalker<CheckEffectsCoverage>
40364036
ContextScope scope(*this, /*newContext*/ std::nullopt);
40374037
scope.setCoverageForSingleValueStmtExpr();
40384038
SVE->getStmt()->walk(*this);
4039+
4040+
if (auto preamble = SVE->getForExpressionPreamble()) {
4041+
preamble->ForAccumulatorDecl->walk(*this);
4042+
preamble->ForAccumulatorBinding->walk(*this);
4043+
}
4044+
40394045
scope.preserveCoverageFromSingleValueStmtExpr();
40404046
return ShouldNotRecurse;
40414047
}

test/Sema/for-expr.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %target-typecheck-verify-swift -enable-experimental-feature ForExpressions -enable-experimental-feature ThenStatements
2+
3+
func f() -> String {
4+
for (i, x) in "hello".enumerated() {
5+
then if i % 2 == 0 {
6+
x.uppercased()
7+
} else {
8+
x
9+
}
10+
}
11+
}
12+
print(f())

0 commit comments

Comments
 (0)