@@ -6657,75 +6657,44 @@ BraceStmt *Parser::parseAbstractFunctionBodyImpl(AbstractFunctionDecl *AFD) {
6657
6657
BraceStmt *BS = Body.get ();
6658
6658
AFD->setBodyParsed (BS);
6659
6659
6660
- if (BS->getNumElements () == 0 )
6661
- return BS;
6662
-
6663
- auto Element = BS->getFirstElement ();
6664
-
6665
- // If the body consists of an #if declaration with a single
6666
- // expression active clause, turn it into a return statement.
6667
- bool ReturnsLastElement = false ;
6668
- if (BS->getNumElements () == 2 ) {
6669
- if (auto *D = Element.dyn_cast <Decl *>()) {
6670
- // Step into nested active clause.
6671
- while (auto *ICD = dyn_cast<IfConfigDecl>(D)) {
6672
- auto ACE = ICD->getActiveClauseElements ();
6673
- if (ACE.size () == 1 ) {
6674
- Element = BS->getLastElement ();
6675
- ReturnsLastElement = true ;
6676
- break ;
6677
- } else if (ACE.size () == 2 ) {
6678
- if (auto *ND = ACE.front ().dyn_cast <Decl *>()) {
6679
- D = ND;
6680
- continue ;
6660
+ if (Parser::shouldReturnSingleExpressionElement (BS->getElements ())) {
6661
+ auto Element = BS->getLastElement ();
6662
+ if (auto *stmt = Element.dyn_cast <Stmt *>()) {
6663
+ if (isa<FuncDecl>(AFD)) {
6664
+ if (auto *returnStmt = dyn_cast<ReturnStmt>(stmt)) {
6665
+ if (!returnStmt->hasResult ()) {
6666
+ auto returnExpr = TupleExpr::createEmpty (Context,
6667
+ SourceLoc (),
6668
+ SourceLoc (),
6669
+ /* implicit*/ true );
6670
+ returnStmt->setResult (returnExpr);
6671
+ AFD->setHasSingleExpressionBody ();
6672
+ AFD->setSingleExpressionBody (returnExpr);
6681
6673
}
6682
6674
}
6683
- break ;
6684
6675
}
6685
- }
6686
- }
6687
-
6688
- // If the body consists of a single expression, turn it into a return
6689
- // statement.
6690
- if (BS->getNumElements () != 1 &&
6691
- !ReturnsLastElement)
6692
- return BS;
6693
-
6694
- if (auto *stmt = Element.dyn_cast <Stmt *>()) {
6695
- if (isa<FuncDecl>(AFD)) {
6696
- if (auto *returnStmt = dyn_cast<ReturnStmt>(stmt)) {
6697
- if (!returnStmt->hasResult ()) {
6698
- auto returnExpr = TupleExpr::createEmpty (Context,
6699
- SourceLoc (),
6700
- SourceLoc (),
6701
- /* implicit*/ true );
6702
- returnStmt->setResult (returnExpr);
6703
- AFD->setHasSingleExpressionBody ();
6704
- AFD->setSingleExpressionBody (returnExpr);
6676
+ } else if (auto *E = Element.dyn_cast <Expr *>()) {
6677
+ if (auto SE = dyn_cast<SequenceExpr>(E->getSemanticsProvidingExpr ())) {
6678
+ if (SE->getNumElements () > 1 && isa<AssignExpr>(SE->getElement (1 ))) {
6679
+ // This is an assignment. We don't want to implicitly return
6680
+ // it.
6681
+ return BS;
6705
6682
}
6706
6683
}
6707
- }
6708
- } else if (auto *E = Element.dyn_cast <Expr *>()) {
6709
- if (auto SE = dyn_cast<SequenceExpr>(E->getSemanticsProvidingExpr ())) {
6710
- if (SE->getNumElements () > 1 && isa<AssignExpr>(SE->getElement (1 ))) {
6711
- // This is an assignment. We don't want to implicitly return
6712
- // it.
6713
- return BS;
6714
- }
6715
- }
6716
- if (isa<FuncDecl>(AFD)) {
6717
- auto RS = new (Context) ReturnStmt (SourceLoc (), E);
6718
- BS->getElements ().back () = RS;
6719
- AFD->setHasSingleExpressionBody ();
6720
- AFD->setSingleExpressionBody (E);
6721
- } else if (auto *F = dyn_cast<ConstructorDecl>(AFD)) {
6722
- if (F->isFailable () && isa<NilLiteralExpr>(E)) {
6723
- // If it's a nil literal, just insert return. This is the only
6724
- // legal thing to return.
6725
- auto RS = new (Context) ReturnStmt (E->getStartLoc (), E);
6684
+ if (isa<FuncDecl>(AFD)) {
6685
+ auto RS = new (Context) ReturnStmt (SourceLoc (), E);
6726
6686
BS->getElements ().back () = RS;
6727
6687
AFD->setHasSingleExpressionBody ();
6728
6688
AFD->setSingleExpressionBody (E);
6689
+ } else if (auto *F = dyn_cast<ConstructorDecl>(AFD)) {
6690
+ if (F->isFailable () && isa<NilLiteralExpr>(E)) {
6691
+ // If it's a nil literal, just insert return. This is the only
6692
+ // legal thing to return.
6693
+ auto RS = new (Context) ReturnStmt (E->getStartLoc (), E);
6694
+ BS->getElements ().back () = RS;
6695
+ AFD->setHasSingleExpressionBody ();
6696
+ AFD->setSingleExpressionBody (E);
6697
+ }
6729
6698
}
6730
6699
}
6731
6700
}
0 commit comments