@@ -6683,48 +6683,45 @@ BraceStmt *Parser::parseAbstractFunctionBodyImpl(AbstractFunctionDecl *AFD) {
66836683
66846684 BraceStmt *BS = Body.get ();
66856685 AFD->setBodyParsed (BS);
6686-
6687- // If the body consists of a single expression, turn it into a return
6688- // statement.
6689- if (BS->getNumElements () != 1 )
6690- return BS;
6691-
6692- auto Element = BS->getFirstElement ();
6693- if (auto *stmt = Element.dyn_cast <Stmt *>()) {
6694- if (isa<FuncDecl>(AFD)) {
6695- if (auto *returnStmt = dyn_cast<ReturnStmt>(stmt)) {
6696- if (!returnStmt->hasResult ()) {
6697- auto returnExpr = TupleExpr::createEmpty (Context,
6698- SourceLoc (),
6699- SourceLoc (),
6700- /* implicit*/ true );
6701- returnStmt->setResult (returnExpr);
6702- AFD->setHasSingleExpressionBody ();
6703- AFD->setSingleExpressionBody (returnExpr);
6686+
6687+ if (Parser::shouldReturnSingleExpressionElement (BS->getElements ())) {
6688+ auto Element = BS->getLastElement ();
6689+ if (auto *stmt = Element.dyn_cast <Stmt *>()) {
6690+ if (isa<FuncDecl>(AFD)) {
6691+ if (auto *returnStmt = dyn_cast<ReturnStmt>(stmt)) {
6692+ if (!returnStmt->hasResult ()) {
6693+ auto returnExpr = TupleExpr::createEmpty (Context,
6694+ SourceLoc (),
6695+ SourceLoc (),
6696+ /* implicit*/ true );
6697+ returnStmt->setResult (returnExpr);
6698+ AFD->setHasSingleExpressionBody ();
6699+ AFD->setSingleExpressionBody (returnExpr);
6700+ }
67046701 }
67056702 }
6706- }
6707- } else if (auto *E = Element. dyn_cast <Expr *>( )) {
6708- if (auto SE = dyn_cast<SequenceExpr>(E-> getSemanticsProvidingExpr ( ))) {
6709- if (SE-> getNumElements () > 1 && isa<AssignExpr>(SE-> getElement ( 1 ))) {
6710- // This is an assignment. We don't want to implicitly return
6711- // it.
6712- return BS;
6703+ } else if ( auto *E = Element. dyn_cast <Expr *>()) {
6704+ if (auto SE = dyn_cast<SequenceExpr>(E-> getSemanticsProvidingExpr () )) {
6705+ if (SE-> getNumElements () > 1 && isa<AssignExpr>(SE-> getElement ( 1 ))) {
6706+ // This is an assignment. We don't want to implicitly return
6707+ // it.
6708+ return BS;
6709+ }
67136710 }
6714- }
6715- if (isa<FuncDecl>(AFD)) {
6716- auto RS = new (Context) ReturnStmt (SourceLoc (), E);
6717- BS->setFirstElement (RS);
6718- AFD->setHasSingleExpressionBody ();
6719- AFD->setSingleExpressionBody (E);
6720- } else if (auto *F = dyn_cast<ConstructorDecl>(AFD)) {
6721- if (F->isFailable () && isa<NilLiteralExpr>(E)) {
6722- // If it's a nil literal, just insert return. This is the only
6723- // legal thing to return.
6724- auto RS = new (Context) ReturnStmt (E->getStartLoc (), E);
6725- BS->setFirstElement (RS);
6711+ if (isa<FuncDecl>(AFD)) {
6712+ auto RS = new (Context) ReturnStmt (SourceLoc (), E);
6713+ BS->setLastElement (RS);
67266714 AFD->setHasSingleExpressionBody ();
67276715 AFD->setSingleExpressionBody (E);
6716+ } else if (auto *F = dyn_cast<ConstructorDecl>(AFD)) {
6717+ if (F->isFailable () && isa<NilLiteralExpr>(E)) {
6718+ // If it's a nil literal, just insert return. This is the only
6719+ // legal thing to return.
6720+ auto RS = new (Context) ReturnStmt (E->getStartLoc (), E);
6721+ BS->setLastElement (RS);
6722+ AFD->setHasSingleExpressionBody ();
6723+ AFD->setSingleExpressionBody (E);
6724+ }
67286725 }
67296726 }
67306727 }
0 commit comments