Skip to content

Commit f6c9769

Browse files
committed
Statement setter for last element
1 parent 037edf3 commit f6c9769

File tree

5 files changed

+11
-10
lines changed

5 files changed

+11
-10
lines changed

include/swift/AST/Stmt.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ class BraceStmt final : public Stmt,
180180
ASTNode getLastElement() const { return getElements().back(); }
181181

182182
void setFirstElement(ASTNode node) { getElements().front() = node; }
183+
void setLastElement(ASTNode node) { getElements().back() = node; }
183184

184185
/// The elements contained within the BraceStmt.
185186
MutableArrayRef<ASTNode> getElements() {

lib/AST/Decl.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ Expr *AbstractFunctionDecl::getSingleExpressionBody() const {
684684
assert(hasSingleExpressionBody() && "Not a single-expression body");
685685
auto braceStmt = getBody();
686686
assert(braceStmt != nullptr && "No body currently available.");
687-
auto body = getBody()->getElements().back();
687+
auto body = getBody()->getLastElement();
688688
if (auto *stmt = body.dyn_cast<Stmt *>()) {
689689
if (auto *returnStmt = dyn_cast<ReturnStmt>(stmt)) {
690690
return returnStmt->getResult();
@@ -701,7 +701,7 @@ Expr *AbstractFunctionDecl::getSingleExpressionBody() const {
701701

702702
void AbstractFunctionDecl::setSingleExpressionBody(Expr *NewBody) {
703703
assert(hasSingleExpressionBody() && "Not a single-expression body");
704-
auto body = getBody()->getElements().back();
704+
auto body = getBody()->getLastElement();
705705
if (auto *stmt = body.dyn_cast<Stmt *>()) {
706706
if (auto *returnStmt = dyn_cast<ReturnStmt>(stmt)) {
707707
returnStmt->setResult(NewBody);
@@ -717,7 +717,7 @@ void AbstractFunctionDecl::setSingleExpressionBody(Expr *NewBody) {
717717
return;
718718
}
719719
}
720-
getBody()->getElements().back() = NewBody;
720+
getBody()->setLastElement(NewBody);
721721
}
722722

723723
bool AbstractStorageDecl::isTransparent() const {

lib/IDE/CodeCompletion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6107,7 +6107,7 @@ static void undoSingleExpressionReturn(DeclContext *DC) {
61076107
if (!RS || !RS->isImplicit())
61086108
return false;
61096109

6110-
BS->getElements().back() = RS->getResult();
6110+
BS->setLastElement(RS->getResult());
61116111
return true;
61126112
};
61136113

lib/Parse/ParseDecl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6683,15 +6683,15 @@ BraceStmt *Parser::parseAbstractFunctionBodyImpl(AbstractFunctionDecl *AFD) {
66836683
}
66846684
if (isa<FuncDecl>(AFD)) {
66856685
auto RS = new (Context) ReturnStmt(SourceLoc(), E);
6686-
BS->getElements().back() = RS;
6686+
BS->setLastElement(RS);
66876687
AFD->setHasSingleExpressionBody();
66886688
AFD->setSingleExpressionBody(E);
66896689
} else if (auto *F = dyn_cast<ConstructorDecl>(AFD)) {
66906690
if (F->isFailable() && isa<NilLiteralExpr>(E)) {
66916691
// If it's a nil literal, just insert return. This is the only
66926692
// legal thing to return.
66936693
auto RS = new (Context) ReturnStmt(E->getStartLoc(), E);
6694-
BS->getElements().back() = RS;
6694+
BS->setLastElement(RS);
66956695
AFD->setHasSingleExpressionBody();
66966696
AFD->setSingleExpressionBody(E);
66976697
}

lib/Sema/TypeCheckStmt.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1926,7 +1926,7 @@ bool TypeCheckASTNodeAtLocRequest::evaluate(Evaluator &evaluator,
19261926
func->getResultInterfaceType()->isVoid()) {
19271927
// The function returns void. We don't need an explicit return, no matter
19281928
// what the type of the expression is. Take the inserted return back out.
1929-
func->getBody()->getElements().back() = func->getSingleExpressionBody();
1929+
func->getBody()->setLastElement(func->getSingleExpressionBody());
19301930
}
19311931
}
19321932

@@ -1991,7 +1991,7 @@ TypeCheckFunctionBodyRequest::evaluate(Evaluator &evaluator,
19911991
func->getResultInterfaceType()->isVoid()) {
19921992
// The function returns void. We don't need an explicit return, no matter
19931993
// what the type of the expression is. Take the inserted return back out.
1994-
body->getElements().back() = func->getSingleExpressionBody();
1994+
body->setLastElement(func->getSingleExpressionBody());
19951995
}
19961996
} else if (isa<ConstructorDecl>(AFD) &&
19971997
(body->empty() ||
@@ -2025,12 +2025,12 @@ TypeCheckFunctionBodyRequest::evaluate(Evaluator &evaluator,
20252025
// that we have eagerly converted something like `{ fatalError() }`
20262026
// into `{ return fatalError() }` that has to be corrected here.
20272027
if (isa<FuncDecl>(AFD) && cast<FuncDecl>(AFD)->hasSingleExpressionBody()) {
2028-
if (auto *stmt = body->getElements().back().dyn_cast<Stmt *>()) {
2028+
if (auto *stmt = body->getLastElement().dyn_cast<Stmt *>()) {
20292029
if (auto *retStmt = dyn_cast<ReturnStmt>(stmt)) {
20302030
if (retStmt->isImplicit() && retStmt->hasResult()) {
20312031
auto returnType = retStmt->getResult()->getType();
20322032
if (returnType && returnType->isUninhabited())
2033-
body->getElements().back() = retStmt->getResult();
2033+
body->setLastElement(retStmt->getResult());
20342034
}
20352035
}
20362036
}

0 commit comments

Comments
 (0)