@@ -1198,10 +1198,10 @@ namespace {
11981198 }
11991199
12001200 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.
12051205 // /
12061206 // / This operation cannot fail.
12071207 // /
@@ -1210,6 +1210,17 @@ namespace {
12101210 // / \returns The coerced closure expression.
12111211 // /
12121212 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);
12131224
12141225 // / \brief Coerce the given expression to the given type.
12151226 // /
@@ -5017,14 +5028,12 @@ ClosureExpr *ExprRewriter::coerceClosureExprToVoid(ClosureExpr *closureExpr) {
50175028
50185029 // Re-write the single-expression closure to return '()'
50195030 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 *>()) {
50285037 auto returnStmt = cast<ReturnStmt>(member.get <Stmt *>());
50295038 auto singleExpr = returnStmt->getResult ();
50305039 auto voidExpr = TupleExpr::createEmpty (tc.Context ,
@@ -5051,7 +5060,6 @@ ClosureExpr *ExprRewriter::coerceClosureExprToVoid(ClosureExpr *closureExpr) {
50515060 /* implicit*/ true );
50525061
50535062 closureExpr->setImplicit ();
5054- closureExpr->setIsVoidConversionClosure ();
50555063 closureExpr->setBody (braceStmt, /* isSingleExpression*/ true );
50565064 }
50575065
@@ -5065,6 +5073,38 @@ ClosureExpr *ExprRewriter::coerceClosureExprToVoid(ClosureExpr *closureExpr) {
50655073 return closureExpr;
50665074}
50675075
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+
50685108static void
50695109maybeDiagnoseUnsupportedFunctionConversion (TypeChecker &tc, Expr *expr,
50705110 AnyFunctionType *toType) {
@@ -6471,9 +6511,15 @@ namespace {
64716511 closure->setSingleExpressionBody (body);
64726512
64736513 if (body) {
6474-
6514+ // A single-expression closure with a non-Void expression type
6515+ // coerces to a Void-returning function type.
64756516 if (fnType->getResult ()->isVoid () && !body->getType ()->isVoid ()) {
64766517 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);
64776523 } else {
64786524
64796525 body = Rewriter.coerceToType (body,
0 commit comments