Skip to content

Commit 0dae0bb

Browse files
committed
[Multi-expression closures] Add support for 'throw' statement.
1 parent 0d5987d commit 0dae0bb

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

lib/Sema/CSClosure.cpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,23 @@ class ClosureConstraintGenerator
176176
void visitContinueStmt(ContinueStmt *continueStmt) { }
177177
void visitDeferStmt(DeferStmt *deferStmt) { }
178178

179+
void visitThrowStmt(ThrowStmt *throwStmt) {
180+
Type exnType =
181+
cs.getASTContext().getErrorDecl()->getDeclaredInterfaceType();
182+
if (!exnType) {
183+
hadError = true;
184+
return;
185+
}
186+
187+
SolutionApplicationTarget target(
188+
throwStmt->getSubExpr(), closure, CTP_ThrowStmt, exnType,
189+
/*isDiscarded=*/false);
190+
if (cs.generateConstraints(target, FreeTypeVariableBinding::Disallow))
191+
hadError = true;
192+
193+
cs.setSolutionApplicationTarget(throwStmt, target);
194+
}
195+
179196
#define UNSUPPORTED_STMT(STMT) void visit##STMT##Stmt(STMT##Stmt *) { \
180197
llvm_unreachable("Unsupported statement kind " #STMT); \
181198
}
@@ -185,7 +202,6 @@ class ClosureConstraintGenerator
185202
UNSUPPORTED_STMT(Case)
186203
UNSUPPORTED_STMT(Fallthrough)
187204
UNSUPPORTED_STMT(Fail)
188-
UNSUPPORTED_STMT(Throw)
189205
UNSUPPORTED_STMT(PoundAssert)
190206
#undef UNSUPPORTED_STMT
191207
};
@@ -450,6 +466,17 @@ class ClosureConstraintApplication
450466
return deferStmt;
451467
}
452468

469+
ASTNode visitThrowStmt(ThrowStmt *throwStmt) {
470+
// Rewrite the condition.
471+
auto target = *solution.getConstraintSystem()
472+
.getSolutionApplicationTarget(throwStmt);
473+
if (auto result = rewriteTarget(target))
474+
throwStmt->setSubExpr(result->getAsExpr());
475+
else
476+
hadError = true;
477+
return throwStmt;
478+
}
479+
453480
#define UNSUPPORTED_STMT(STMT) ASTNode visit##STMT##Stmt(STMT##Stmt *) { \
454481
llvm_unreachable("Unsupported statement kind " #STMT); \
455482
}
@@ -459,7 +486,6 @@ class ClosureConstraintApplication
459486
UNSUPPORTED_STMT(Case)
460487
UNSUPPORTED_STMT(Fallthrough)
461488
UNSUPPORTED_STMT(Fail)
462-
UNSUPPORTED_STMT(Throw)
463489
UNSUPPORTED_STMT(PoundAssert)
464490
#undef UNSUPPORTED_STMT
465491

test/expr/closure/multi_statement.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@ func maybeGetValue<T>(_ value: T) -> T? {
88
return value
99
}
1010

11+
enum MyError: Error {
12+
case featureIsTooCool
13+
}
14+
1115
func random(_: Int) -> Bool { return false }
1216

13-
func mapWithMoreStatements(ints: [Int]) {
14-
let _ = ints.map { i in
17+
func mapWithMoreStatements(ints: [Int]) throws {
18+
let _ = try ints.map { i in
1519
guard var actualValue = maybeGetValue(i) else {
1620
return String(0)
1721
}
@@ -46,7 +50,9 @@ func mapWithMoreStatements(ints: [Int]) {
4650
if j % 7 == 0 {
4751
continue
4852
}
53+
4954
print("even")
55+
throw MyError.featureIsTooCool
5056
}
5157

5258
return String(value)

0 commit comments

Comments
 (0)