Skip to content

Commit 5c16778

Browse files
committed
[Multi-expression closures] Add support for experimental #assert statement
1 parent 0dae0bb commit 5c16778

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

lib/Sema/CSClosure.cpp

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,22 @@ class ClosureConstraintGenerator
193193
cs.setSolutionApplicationTarget(throwStmt, target);
194194
}
195195

196+
void visitPoundAssertStmt(PoundAssertStmt *poundAssertStmt) {
197+
auto *boolDecl = cs.getASTContext().getBoolDecl();
198+
if (!boolDecl) {
199+
hadError = true;
200+
return;
201+
}
202+
203+
SolutionApplicationTarget target(
204+
poundAssertStmt->getCondition(), closure, CTP_Condition,
205+
boolDecl->getDeclaredInterfaceType(), /*isDiscarded=*/false);
206+
if (cs.generateConstraints(target, FreeTypeVariableBinding::Disallow))
207+
hadError = true;
208+
209+
cs.setSolutionApplicationTarget(poundAssertStmt, target);
210+
}
211+
196212
#define UNSUPPORTED_STMT(STMT) void visit##STMT##Stmt(STMT##Stmt *) { \
197213
llvm_unreachable("Unsupported statement kind " #STMT); \
198214
}
@@ -202,7 +218,6 @@ class ClosureConstraintGenerator
202218
UNSUPPORTED_STMT(Case)
203219
UNSUPPORTED_STMT(Fallthrough)
204220
UNSUPPORTED_STMT(Fail)
205-
UNSUPPORTED_STMT(PoundAssert)
206221
#undef UNSUPPORTED_STMT
207222
};
208223

@@ -467,7 +482,7 @@ class ClosureConstraintApplication
467482
}
468483

469484
ASTNode visitThrowStmt(ThrowStmt *throwStmt) {
470-
// Rewrite the condition.
485+
// Rewrite the error.
471486
auto target = *solution.getConstraintSystem()
472487
.getSolutionApplicationTarget(throwStmt);
473488
if (auto result = rewriteTarget(target))
@@ -477,6 +492,17 @@ class ClosureConstraintApplication
477492
return throwStmt;
478493
}
479494

495+
ASTNode visitPoundAssertStmt(PoundAssertStmt *poundAssertStmt) {
496+
// Rewrite the condition.
497+
auto target = *solution.getConstraintSystem()
498+
.getSolutionApplicationTarget(poundAssertStmt);
499+
if (auto result = rewriteTarget(target))
500+
poundAssertStmt->setCondition(result->getAsExpr());
501+
else
502+
hadError = true;
503+
return poundAssertStmt;
504+
}
505+
480506
#define UNSUPPORTED_STMT(STMT) ASTNode visit##STMT##Stmt(STMT##Stmt *) { \
481507
llvm_unreachable("Unsupported statement kind " #STMT); \
482508
}
@@ -486,7 +512,6 @@ class ClosureConstraintApplication
486512
UNSUPPORTED_STMT(Case)
487513
UNSUPPORTED_STMT(Fallthrough)
488514
UNSUPPORTED_STMT(Fail)
489-
UNSUPPORTED_STMT(PoundAssert)
490515
#undef UNSUPPORTED_STMT
491516

492517
};

test/expr/closure/multi_statement.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-typecheck-verify-swift -swift-version 5 -experimental-multi-statement-closures
1+
// RUN: %target-typecheck-verify-swift -swift-version 5 -experimental-multi-statement-closures -enable-experimental-static-assert
22

33
func isInt<T>(_ value: T) -> Bool {
44
return value is Int
@@ -50,11 +50,13 @@ func mapWithMoreStatements(ints: [Int]) throws {
5050
if j % 7 == 0 {
5151
continue
5252
}
53-
53+
5454
print("even")
5555
throw MyError.featureIsTooCool
5656
}
5757

58+
#assert(true)
59+
5860
return String(value)
5961
}
6062
}

0 commit comments

Comments
 (0)