Skip to content

Commit 8cddae2

Browse files
committed
Make nested expr work, convert test to executable
1 parent b985f3a commit 8cddae2

File tree

4 files changed

+27
-16
lines changed

4 files changed

+27
-16
lines changed

lib/Sema/CSSyntacticElement.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,8 +1292,12 @@ class SyntacticElementConstraintGenerator
12921292

12931293
// First check to make sure the ThenStmt is in a valid position.
12941294
SmallVector<ThenStmt *, 4> validThenStmts;
1295-
if (auto SVE = context.getAsSingleValueStmtExpr())
1295+
if (auto SVE = context.getAsSingleValueStmtExpr()) {
12961296
(void)SVE.get()->getThenStmts(validThenStmts);
1297+
if (SVE.get()->getStmtKind() == SingleValueStmtExpr::Kind::For) {
1298+
contextInfo = std::nullopt;
1299+
}
1300+
}
12971301

12981302
if (!llvm::is_contained(validThenStmts, thenStmt)) {
12991303
auto *thenLoc = cs.getConstraintLocator(thenStmt);
@@ -2251,7 +2255,9 @@ class SyntacticElementSolutionApplication
22512255
// not the branch result type. This is necessary as there may be
22522256
// an additional conversion required for the branch.
22532257
auto target = solution.getTargetFor(thenStmt->getResult());
2254-
target->setExprConversionType(ty);
2258+
if (SVE.get()->getStmtKind() != SingleValueStmtExpr::Kind::For) {
2259+
target->setExprConversionType(ty);
2260+
}
22552261

22562262
auto *resultExpr = thenStmt->getResult();
22572263
if (auto newResultTarget = rewriter.rewriteTarget(*target))

lib/Sema/PreCheckTarget.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1742,10 +1742,14 @@ void PreCheckTarget::transformForExpression(SingleValueStmtExpr *SVE) {
17421742
if (auto stmt = result.dyn_cast<Stmt *>()) {
17431743
if (auto *then = dyn_cast<ThenStmt>(stmt)) {
17441744
auto *declRefExpr = new(astCtx) DeclRefExpr(varDecl, DeclNameLoc(), true);
1745-
auto *dotExpr = new(astCtx) UnresolvedDotExpr(declRefExpr, sveLoc, DeclNameRef(DeclBaseName(astCtx.Id_append)), DeclNameLoc(), true);
1745+
auto *dotExpr = new(astCtx) UnresolvedDotExpr(declRefExpr,
1746+
SourceLoc(),
1747+
DeclNameRef(DeclBaseName(astCtx.Id_append)),
1748+
DeclNameLoc(),
1749+
true);
17461750
auto *argumentList = ArgumentList::createImplicit(astCtx, { Argument::unlabeled(then->getResult()) });
17471751
auto *callExpr = CallExpr::createImplicit(astCtx, dotExpr, argumentList);
1748-
result = callExpr;
1752+
then->setResult(callExpr);
17491753
}
17501754
}
17511755
}

test/Sema/for-expr.swift

Lines changed: 0 additions & 12 deletions
This file was deleted.

test/stmt/for-expr.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %target-run-simple-swift(-enable-experimental-feature ForExpressions) | %FileCheck %s
2+
3+
func f() -> String {
4+
for (i, x) in "hello".enumerated() {
5+
if i % 2 == 0 {
6+
x.uppercased()
7+
} else {
8+
"*skip*"
9+
10+
}
11+
}
12+
}
13+
print(f()) // CHECK: H*skip*L*skip*O

0 commit comments

Comments
 (0)