@@ -1198,10 +1198,10 @@ namespace {
1198
1198
}
1199
1199
1200
1200
public:
1201
-
1202
-
1203
- // / \brief Coerce a closure expression with a non-void return type to a
1204
- // / contextual function type with a void return type.
1201
+
1202
+
1203
+ // / \brief Coerce a closure expression with a non-Void return type to a
1204
+ // / contextual function type with a Void return type.
1205
1205
// /
1206
1206
// / This operation cannot fail.
1207
1207
// /
@@ -1210,6 +1210,17 @@ namespace {
1210
1210
// / \returns The coerced closure expression.
1211
1211
// /
1212
1212
ClosureExpr *coerceClosureExprToVoid (ClosureExpr *expr);
1213
+
1214
+ // / \brief Coerce a closure expression with a Never return type to a
1215
+ // / contextual function type with some other return type.
1216
+ // /
1217
+ // / This operation cannot fail.
1218
+ // /
1219
+ // / \param expr The closure expression to coerce.
1220
+ // /
1221
+ // / \returns The coerced closure expression.
1222
+ // /
1223
+ ClosureExpr *coerceClosureExprFromNever (ClosureExpr *expr);
1213
1224
1214
1225
// / \brief Coerce the given expression to the given type.
1215
1226
// /
@@ -5017,14 +5028,12 @@ ClosureExpr *ExprRewriter::coerceClosureExprToVoid(ClosureExpr *closureExpr) {
5017
5028
5018
5029
// Re-write the single-expression closure to return '()'
5019
5030
assert (closureExpr->hasSingleExpressionBody ());
5020
-
5021
- // Transform the ClosureExpr representation into the "expr + return ()" rep
5022
- // if it isn't already.
5023
- if (!closureExpr->isVoidConversionClosure ()) {
5024
-
5025
- auto member = closureExpr->getBody ()->getElement (0 );
5026
-
5027
- // A single-expression body contains a single return statement.
5031
+
5032
+ // A single-expression body contains a single return statement
5033
+ // prior to this transformation.
5034
+ auto member = closureExpr->getBody ()->getElement (0 );
5035
+
5036
+ if (member.is <Stmt *>()) {
5028
5037
auto returnStmt = cast<ReturnStmt>(member.get <Stmt *>());
5029
5038
auto singleExpr = returnStmt->getResult ();
5030
5039
auto voidExpr = TupleExpr::createEmpty (tc.Context ,
@@ -5051,7 +5060,6 @@ ClosureExpr *ExprRewriter::coerceClosureExprToVoid(ClosureExpr *closureExpr) {
5051
5060
/* implicit*/ true );
5052
5061
5053
5062
closureExpr->setImplicit ();
5054
- closureExpr->setIsVoidConversionClosure ();
5055
5063
closureExpr->setBody (braceStmt, /* isSingleExpression*/ true );
5056
5064
}
5057
5065
@@ -5065,6 +5073,38 @@ ClosureExpr *ExprRewriter::coerceClosureExprToVoid(ClosureExpr *closureExpr) {
5065
5073
return closureExpr;
5066
5074
}
5067
5075
5076
+ ClosureExpr *ExprRewriter::coerceClosureExprFromNever (ClosureExpr *closureExpr) {
5077
+ auto &tc = cs.getTypeChecker ();
5078
+
5079
+ // Re-write the single-expression closure to drop the 'return'.
5080
+ assert (closureExpr->hasSingleExpressionBody ());
5081
+
5082
+ // A single-expression body contains a single return statement
5083
+ // prior to this transformation.
5084
+ auto member = closureExpr->getBody ()->getElement (0 );
5085
+
5086
+ if (member.is <Stmt *>()) {
5087
+ auto returnStmt = cast<ReturnStmt>(member.get <Stmt *>());
5088
+ auto singleExpr = returnStmt->getResult ();
5089
+
5090
+ tc.checkIgnoredExpr (singleExpr);
5091
+
5092
+ SmallVector<ASTNode, 1 > elements;
5093
+ elements.push_back (singleExpr);
5094
+
5095
+ auto braceStmt = BraceStmt::create (tc.Context ,
5096
+ closureExpr->getStartLoc (),
5097
+ elements,
5098
+ closureExpr->getEndLoc (),
5099
+ /* implicit*/ true );
5100
+
5101
+ closureExpr->setImplicit ();
5102
+ closureExpr->setBody (braceStmt, /* isSingleExpression*/ true );
5103
+ }
5104
+
5105
+ return closureExpr;
5106
+ }
5107
+
5068
5108
static void
5069
5109
maybeDiagnoseUnsupportedFunctionConversion (TypeChecker &tc, Expr *expr,
5070
5110
AnyFunctionType *toType) {
@@ -6471,9 +6511,15 @@ namespace {
6471
6511
closure->setSingleExpressionBody (body);
6472
6512
6473
6513
if (body) {
6474
-
6514
+ // A single-expression closure with a non-Void expression type
6515
+ // coerces to a Void-returning function type.
6475
6516
if (fnType->getResult ()->isVoid () && !body->getType ()->isVoid ()) {
6476
6517
closure = Rewriter.coerceClosureExprToVoid (closure);
6518
+ // A single-expression closure with a Never expression type
6519
+ // coerces to any other function type.
6520
+ } else if (!fnType->getResult ()->isUninhabited () &&
6521
+ body->getType ()->isUninhabited ()) {
6522
+ closure = Rewriter.coerceClosureExprFromNever (closure);
6477
6523
} else {
6478
6524
6479
6525
body = Rewriter.coerceToType (body,
0 commit comments