Skip to content

Commit f5c007a

Browse files
committed
[Sema] Avoid forming double braced single expression closures
For a single expression closure, just use the expression as the body in the case where we're coercing to Void, as the return is already implied. This avoids crashing in `ClosureExpr::getSingleExpressionBody` with a double braced body. Surprisingly it seems nothing is currently calling `ClosureExpr::getSingleExpressionBody` after type-checking, so no test case, but later commits in this patch will exercise this case.
1 parent 0bc0bf5 commit f5c007a

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

lib/Sema/CSSyntacticElement.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,6 +1768,14 @@ class SyntacticElementSolutionApplication
17681768
// Evaluate the expression, then produce a return statement that
17691769
// returns nothing.
17701770
TypeChecker::checkIgnoredExpr(resultExpr);
1771+
1772+
// For a single expression closure, we can just preserve the result expr,
1773+
// and leave the return as implied. This avoids neededing to jump through
1774+
// nested brace statements to dig out the single expression in
1775+
// ClosureExpr::getSingleExpressionBody.
1776+
if (context.isSingleExpressionClosure(cs))
1777+
return resultExpr;
1778+
17711779
auto &ctx = solution.getConstraintSystem().getASTContext();
17721780
auto newReturnStmt =
17731781
new (ctx) ReturnStmt(

test/PCMacro/nested_function.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,5 @@ foo()
4747
// CHECK-NEXT: [19:3-19:8] pc before
4848
// CHECK-NEXT: [14:5-14:6] pc before
4949
// CHECK-NEXT: [14:5-14:6] pc after
50-
// CHECK-NEXT: [14:5-14:6] pc before
51-
// CHECK-NEXT: [14:5-14:6] pc after
5250
// CHECK-NEXT: [19:3-19:8] pc after
5351
// CHECK-NEXT: [22:1-22:6] pc after

0 commit comments

Comments
 (0)