Skip to content

Commit 3a8e31d

Browse files
authored
Merge pull request #71094 from hamishknight/return-tweaks
A couple of ReturnStmt tweaks
2 parents 5c83cae + 71d8a9c commit 3a8e31d

33 files changed

+136
-140
lines changed

include/swift/AST/Stmt.h

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,25 @@ class BraceStmt final : public Stmt,
241241
class ReturnStmt : public Stmt {
242242
SourceLoc ReturnLoc;
243243
Expr *Result;
244-
244+
245+
ReturnStmt(SourceLoc returnLoc, Expr *result, bool isImplicit)
246+
: Stmt(StmtKind::Return, isImplicit), ReturnLoc(returnLoc),
247+
Result(result) {}
248+
245249
public:
246-
ReturnStmt(SourceLoc ReturnLoc, Expr *Result,
247-
llvm::Optional<bool> implicit = llvm::None)
248-
: Stmt(StmtKind::Return, getDefaultImplicitFlag(implicit, ReturnLoc)),
249-
ReturnLoc(ReturnLoc), Result(Result) {}
250+
static ReturnStmt *createParsed(ASTContext &ctx, SourceLoc returnLoc,
251+
Expr *result) {
252+
return new (ctx) ReturnStmt(returnLoc, result, /*isImplicit*/ false);
253+
}
254+
255+
static ReturnStmt *createImplicit(ASTContext &ctx, SourceLoc returnLoc,
256+
Expr *result) {
257+
return new (ctx) ReturnStmt(returnLoc, result, /*isImplicit*/ true);
258+
}
259+
260+
static ReturnStmt *createImplicit(ASTContext &ctx, Expr *result) {
261+
return createImplicit(ctx, SourceLoc(), result);
262+
}
250263

251264
SourceLoc getReturnLoc() const { return ReturnLoc; }
252265

@@ -1621,10 +1634,10 @@ class FailStmt : public Stmt {
16211634
: Stmt(StmtKind::Fail, getDefaultImplicitFlag(implicit, returnLoc)),
16221635
ReturnLoc(returnLoc), NilLoc(nilLoc) {}
16231636

1624-
SourceLoc getLoc() const { return ReturnLoc; }
1625-
1626-
SourceRange getSourceRange() const { return SourceRange(ReturnLoc, NilLoc); }
1627-
1637+
SourceRange getSourceRange() const {
1638+
return SourceRange::combine(ReturnLoc, NilLoc);
1639+
}
1640+
16281641
static bool classof(const Stmt *S) {
16291642
return S->getKind() == StmtKind::Fail;
16301643
}

include/swift/Basic/SourceLoc.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,17 @@ class SourceRange {
121121
/// if (auto x = getSourceRange()) { ... }
122122
explicit operator bool() const { return isValid(); }
123123

124+
/// Combine the given source ranges into the smallest contiguous SourceRange
125+
/// that includes them all, ignoring any invalid ranges present.
126+
static SourceRange combine(ArrayRef<SourceRange> ranges);
127+
128+
/// Combine the given source ranges into the smallest contiguous SourceRange
129+
/// that includes them all, ignoring any invalid ranges present.
130+
template <typename ...T>
131+
static SourceRange combine(T... ranges) {
132+
return SourceRange::combine({ranges...});
133+
}
134+
124135
/// Extend this SourceRange to the smallest continuous SourceRange that
125136
/// includes both this range and the other one.
126137
void widen(SourceRange Other);

lib/AST/ASTBridging.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1464,7 +1464,7 @@ BridgedReturnStmt BridgedReturnStmt_createParsed(BridgedASTContext cContext,
14641464
BridgedSourceLoc cLoc,
14651465
BridgedNullableExpr expr) {
14661466
ASTContext &context = cContext.unbridged();
1467-
return new (context) ReturnStmt(cLoc.unbridged(), expr.unbridged());
1467+
return ReturnStmt::createParsed(context, cLoc.unbridged(), expr.unbridged());
14681468
}
14691469

14701470
BridgedSwitchStmt BridgedSwitchStmt_createParsed(

lib/AST/ArgumentList.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,7 @@ using namespace swift;
2424
Type swift::__Expr_getType(Expr *E) { return E->getType(); }
2525

2626
SourceRange Argument::getSourceRange() const {
27-
auto labelLoc = getLabelLoc();
28-
if (labelLoc.isInvalid())
29-
return getExpr()->getSourceRange();
30-
31-
auto exprEndLoc = getExpr()->getEndLoc();
32-
if (exprEndLoc.isInvalid())
33-
return labelLoc;
34-
35-
return SourceRange(labelLoc, exprEndLoc);
27+
return SourceRange::combine(getLabelLoc(), getExpr()->getSourceRange());
3628
}
3729

3830
Argument Argument::implicitInOut(ASTContext &ctx, Expr *expr) {

lib/AST/Expr.cpp

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2072,7 +2072,7 @@ FORWARD_SOURCE_LOCS_TO(AutoClosureExpr, Body)
20722072

20732073
void AutoClosureExpr::setBody(Expr *E) {
20742074
auto &Context = getASTContext();
2075-
auto *RS = new (Context) ReturnStmt(SourceLoc(), E);
2075+
auto *RS = ReturnStmt::createImplicit(Context, E);
20762076
Body = BraceStmt::create(Context, E->getStartLoc(), { RS }, E->getEndLoc());
20772077
}
20782078

@@ -2715,19 +2715,8 @@ SourceRange TapExpr::getSourceRange() const {
27152715
if (!SubExpr)
27162716
return Body->getSourceRange();
27172717

2718-
SourceLoc start = SubExpr->getStartLoc();
2719-
if (!start.isValid())
2720-
start = Body->getStartLoc();
2721-
if (!start.isValid())
2722-
return SourceRange();
2723-
2724-
SourceLoc end = Body->getEndLoc();
2725-
if (!end.isValid())
2726-
end = SubExpr->getEndLoc();
2727-
if (!end.isValid())
2728-
return SourceRange();
2729-
2730-
return SourceRange(start, end);
2718+
return SourceRange::combine(SubExpr->getSourceRange(),
2719+
Body->getSourceRange());
27312720
}
27322721

27332722
RegexLiteralExpr *

lib/AST/Stmt.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -371,14 +371,7 @@ ThenStmt *ThenStmt::createImplicit(ASTContext &ctx, Expr *result) {
371371
}
372372

373373
SourceRange ThenStmt::getSourceRange() const {
374-
auto range = getResult()->getSourceRange();
375-
if (!range)
376-
return ThenLoc;
377-
378-
if (ThenLoc)
379-
range.widen(ThenLoc);
380-
381-
return range;
374+
return SourceRange::combine(ThenLoc, getResult()->getSourceRange());
382375
}
383376

384377
SourceLoc ThrowStmt::getEndLoc() const { return SubExpr->getEndLoc(); }

lib/Basic/SourceLoc.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,23 @@ bool SourceManager::isOwning(SourceLoc Loc) const {
533533
return findBufferContainingLocInternal(Loc).has_value();
534534
}
535535

536+
SourceRange SourceRange::combine(ArrayRef<SourceRange> ranges) {
537+
if (ranges.empty())
538+
return SourceRange();
539+
540+
SourceRange result = ranges.front();
541+
for (auto other : ranges.drop_front()) {
542+
if (!other)
543+
continue;
544+
if (!result) {
545+
result = other;
546+
continue;
547+
}
548+
result.widen(other);
549+
}
550+
return result;
551+
}
552+
536553
void SourceRange::widen(SourceRange Other) {
537554
if (Other.Start.Value.getPointer() < Start.Value.getPointer())
538555
Start = Other.Start;

lib/ClangImporter/ClangImporter.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5155,8 +5155,7 @@ synthesizeBaseClassMethodBody(AbstractFunctionDecl *afd, void *context) {
51555155
baseMemberCallExpr->setType(baseMember->getResultInterfaceType());
51565156
baseMemberCallExpr->setThrows(nullptr);
51575157

5158-
auto returnStmt = new (ctx) ReturnStmt(SourceLoc(), baseMemberCallExpr,
5159-
/*implicit=*/true);
5158+
auto *returnStmt = ReturnStmt::createImplicit(ctx, baseMemberCallExpr);
51605159

51615160
auto body = BraceStmt::create(ctx, SourceLoc(), {returnStmt}, SourceLoc(),
51625161
/*implicit=*/true);
@@ -5450,8 +5449,7 @@ synthesizeBaseClassFieldGetterOrAddressGetterBody(AbstractFunctionDecl *afd,
54505449
ctx, baseGetterMethod->getResultInterfaceType(), resultType,
54515450
returnExpr);
54525451

5453-
auto returnStmt = new (ctx) ReturnStmt(SourceLoc(), returnExpr,
5454-
/*implicit=*/true);
5452+
auto *returnStmt = ReturnStmt::createImplicit(ctx, returnExpr);
54555453

54565454
auto body = BraceStmt::create(ctx, SourceLoc(), {returnStmt}, SourceLoc(),
54575455
/*implicit=*/true);
@@ -6763,8 +6761,7 @@ synthesizeDependentTypeThunkParamForwarding(AbstractFunctionDecl *afd, void *con
67636761
ctx, specializedFuncCallExpr, thunkDecl->getResultInterfaceType());
67646762
}
67656763

6766-
auto returnStmt = new (ctx)
6767-
ReturnStmt(SourceLoc(), resultExpr, /*implicit=*/true);
6764+
auto *returnStmt = ReturnStmt::createImplicit(ctx, resultExpr);
67686765
auto body = BraceStmt::create(ctx, SourceLoc(), {returnStmt}, SourceLoc(),
67696766
/*implicit=*/true);
67706767
return {body, /*isTypeChecked=*/true};
@@ -6886,8 +6883,7 @@ synthesizeForwardingThunkBody(AbstractFunctionDecl *afd, void *context) {
68866883
specializedFuncCallExpr->setType(thunkDecl->getResultInterfaceType());
68876884
specializedFuncCallExpr->setThrows(nullptr);
68886885

6889-
auto returnStmt = new (ctx) ReturnStmt(SourceLoc(), specializedFuncCallExpr,
6890-
/*implicit=*/true);
6886+
auto *returnStmt = ReturnStmt::createImplicit(ctx, specializedFuncCallExpr);
68916887

68926888
auto body = BraceStmt::create(ctx, SourceLoc(), {returnStmt}, SourceLoc(),
68936889
/*implicit=*/true);

lib/ClangImporter/ImportDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ synthesizeErrorDomainGetterBody(AbstractFunctionDecl *afd, void *context) {
594594
domainDeclRef->setType(
595595
getterDecl->mapTypeIntoContext(swiftValueDecl->getInterfaceType()));
596596

597-
auto ret = new (ctx) ReturnStmt(SourceLoc(), domainDeclRef);
597+
auto *ret = ReturnStmt::createImplicit(ctx, domainDeclRef);
598598
return { BraceStmt::create(ctx, SourceLoc(), {ret}, SourceLoc(), isImplicit),
599599
/*isTypeChecked=*/true };
600600
}

lib/ClangImporter/SwiftDeclSynthesizer.cpp

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ synthesizeConstantGetterBody(AbstractFunctionDecl *afd, void *voidContext) {
373373
}
374374

375375
// Create the return statement.
376-
auto ret = new (ctx) ReturnStmt(SourceLoc(), expr);
376+
auto ret = ReturnStmt::createImplicit(ctx, expr);
377377

378378
return {BraceStmt::create(ctx, SourceLoc(), ASTNode(ret), SourceLoc()),
379379
/*isTypeChecked=*/true};
@@ -471,7 +471,7 @@ synthesizeStructDefaultConstructorBody(AbstractFunctionDecl *afd,
471471
auto assign = new (ctx) AssignExpr(lhs, SourceLoc(), call, /*implicit*/ true);
472472
assign->setType(emptyTuple);
473473

474-
auto ret = new (ctx) ReturnStmt(SourceLoc(), nullptr, /*Implicit=*/true);
474+
auto *ret = ReturnStmt::createImplicit(ctx, /*expr*/ nullptr);
475475

476476
// Create the function body.
477477
auto body = BraceStmt::create(ctx, SourceLoc(), {assign, ret}, SourceLoc());
@@ -565,8 +565,7 @@ synthesizeValueConstructorBody(AbstractFunctionDecl *afd, void *context) {
565565
}
566566
}
567567

568-
auto ret = new (ctx) ReturnStmt(SourceLoc(), nullptr, /*Implicit=*/true);
569-
stmts.push_back(ret);
568+
stmts.push_back(ReturnStmt::createImplicit(ctx, /*expr*/ nullptr));
570569

571570
// Create the function body.
572571
auto body = BraceStmt::create(ctx, SourceLoc(), stmts, SourceLoc());
@@ -689,7 +688,7 @@ synthesizeRawValueBridgingConstructorBody(AbstractFunctionDecl *afd,
689688
/*Implicit=*/true);
690689
assign->setType(TupleType::getEmpty(ctx));
691690

692-
auto ret = new (ctx) ReturnStmt(SourceLoc(), nullptr, /*Implicit=*/true);
691+
auto *ret = ReturnStmt::createImplicit(ctx, /*expr*/ nullptr);
693692

694693
auto body = BraceStmt::create(ctx, SourceLoc(), {assign, ret}, SourceLoc());
695694
return {body, /*isTypeChecked=*/true};
@@ -852,7 +851,7 @@ synthesizeUnionFieldGetterBody(AbstractFunctionDecl *afd, void *context) {
852851
CallExpr::createImplicit(ctx, reinterpretCastRefExpr, argList);
853852
reinterpreted->setType(importedFieldDecl->getInterfaceType());
854853
reinterpreted->setThrows(nullptr);
855-
auto ret = new (ctx) ReturnStmt(SourceLoc(), reinterpreted);
854+
auto *ret = ReturnStmt::createImplicit(ctx, reinterpreted);
856855
auto body = BraceStmt::create(ctx, SourceLoc(), ASTNode(ret), SourceLoc(),
857856
/*implicit*/ true);
858857
return {body, /*isTypeChecked*/ true};
@@ -1118,7 +1117,7 @@ synthesizeIndirectFieldGetterBody(AbstractFunctionDecl *afd, void *context) {
11181117
DeclNameLoc(), /*implicit*/ true);
11191118
expr->setType(anonymousInnerFieldDecl->getInterfaceType());
11201119

1121-
auto ret = new (ctx) ReturnStmt(SourceLoc(), expr);
1120+
auto *ret = ReturnStmt::createImplicit(ctx, expr);
11221121
auto body = BraceStmt::create(ctx, SourceLoc(), ASTNode(ret), SourceLoc(),
11231122
/*implicit*/ true);
11241123
return {body, /*isTypeChecked=*/true};
@@ -1242,7 +1241,7 @@ synthesizeEnumRawValueConstructorBody(AbstractFunctionDecl *afd,
12421241
/*implicit*/ true);
12431242
assign->setType(TupleType::getEmpty(ctx));
12441243

1245-
auto ret = new (ctx) ReturnStmt(SourceLoc(), nullptr, /*Implicit=*/true);
1244+
auto *ret = ReturnStmt::createImplicit(ctx, /*expr*/ nullptr);
12461245

12471246
auto body = BraceStmt::create(ctx, SourceLoc(), {assign, ret}, SourceLoc(),
12481247
/*implicit*/ true);
@@ -1310,7 +1309,7 @@ synthesizeEnumRawValueGetterBody(AbstractFunctionDecl *afd, void *context) {
13101309
reinterpreted->setType(rawTy);
13111310
reinterpreted->setThrows(nullptr);
13121311

1313-
auto ret = new (ctx) ReturnStmt(SourceLoc(), reinterpreted);
1312+
auto *ret = ReturnStmt::createImplicit(ctx, reinterpreted);
13141313
auto body = BraceStmt::create(ctx, SourceLoc(), ASTNode(ret), SourceLoc(),
13151314
/*implicit*/ true);
13161315
return {body, /*isTypeChecked=*/true};
@@ -1380,7 +1379,7 @@ synthesizeStructRawValueGetterBody(AbstractFunctionDecl *afd, void *context) {
13801379
result = CoerceExpr::createImplicit(ctx, bridge, computedType);
13811380
}
13821381

1383-
auto ret = new (ctx) ReturnStmt(SourceLoc(), result);
1382+
auto ret = ReturnStmt::createImplicit(ctx, result);
13841383
auto body = BraceStmt::create(ctx, SourceLoc(), ASTNode(ret), SourceLoc(),
13851384
/*implicit*/ true);
13861385
return {body, /*isTypeChecked=*/true};
@@ -1565,8 +1564,7 @@ synthesizeUnwrappingGetterOrAddressGetterBody(AbstractFunctionDecl *afd,
15651564
propertyExpr = SwiftDeclSynthesizer::synthesizeReturnReinterpretCast(
15661565
ctx, getterImpl->getResultInterfaceType(), elementTy, propertyExpr);
15671566

1568-
auto returnStmt = new (ctx) ReturnStmt(SourceLoc(), propertyExpr,
1569-
/*implicit*/ true);
1567+
auto *returnStmt = ReturnStmt::createImplicit(ctx, propertyExpr);
15701568

15711569
auto body = BraceStmt::create(ctx, SourceLoc(), {returnStmt}, SourceLoc(),
15721570
/*implicit*/ true);
@@ -1645,8 +1643,7 @@ synthesizeUnwrappingAddressSetterBody(AbstractFunctionDecl *afd,
16451643
auto *setterImplCallExpr =
16461644
createAccessorImplCallExpr(setterImpl, selfArg, nullptr);
16471645

1648-
auto returnStmt = new (ctx) ReturnStmt(SourceLoc(), setterImplCallExpr,
1649-
/*implicit*/ true);
1646+
auto *returnStmt = ReturnStmt::createImplicit(ctx, setterImplCallExpr);
16501647

16511648
auto body = BraceStmt::create(ctx, SourceLoc(), {returnStmt}, SourceLoc(),
16521649
/*implicit*/ true);
@@ -1884,8 +1881,7 @@ synthesizeSuccessorFuncBody(AbstractFunctionDecl *afd, void *context) {
18841881
/*implicit*/ true);
18851882
copyRefRValueExpr->setType(copyDecl->getInterfaceType());
18861883

1887-
auto returnStmt = new (ctx) ReturnStmt(SourceLoc(), copyRefRValueExpr,
1888-
/*implicit*/ true);
1884+
auto *returnStmt = ReturnStmt::createImplicit(ctx, copyRefRValueExpr);
18891885

18901886
auto body = BraceStmt::create(ctx, SourceLoc(),
18911887
{
@@ -1974,8 +1970,7 @@ synthesizeOperatorMethodBody(AbstractFunctionDecl *afd, void *context) {
19741970
callExpr->setType(funcDecl->getResultInterfaceType());
19751971
callExpr->setThrows(nullptr);
19761972

1977-
auto returnStmt = new (ctx) ReturnStmt(SourceLoc(), callExpr,
1978-
/*implicit*/ true);
1973+
auto *returnStmt = ReturnStmt::createImplicit(ctx, callExpr);
19791974

19801975
auto body = BraceStmt::create(ctx, SourceLoc(), {returnStmt}, SourceLoc(),
19811976
/*implicit*/ true);
@@ -2061,10 +2056,9 @@ synthesizeComputedGetterFromCXXMethod(AbstractFunctionDecl *afd,
20612056
auto selfArg = createSelfArg(accessor);
20622057

20632058
auto *getterImplCallExpr = createAccessorImplCallExpr(method, selfArg);
2064-
auto returnStmt =
2065-
new (method->getASTContext()) ReturnStmt(SourceLoc(), getterImplCallExpr);
2066-
auto body = BraceStmt::create(method->getASTContext(), SourceLoc(),
2067-
{returnStmt}, SourceLoc());
2059+
auto &ctx = method->getASTContext();
2060+
auto *returnStmt = ReturnStmt::createImplicit(ctx, getterImplCallExpr);
2061+
auto *body = BraceStmt::create(ctx, SourceLoc(), {returnStmt}, SourceLoc());
20682062

20692063
return {body, /*isTypeChecked*/ true};
20702064
}
@@ -2218,8 +2212,7 @@ synthesizeDefaultArgumentBody(AbstractFunctionDecl *afd, void *context) {
22182212
initCall->setThrows(nullptr);
22192213

22202214
// Synthesize `return __cxx__defaultArg_XYZ()`.
2221-
auto returnStmt = new (ctx) ReturnStmt(SourceLoc(), initCall,
2222-
/*implicit=*/true);
2215+
auto *returnStmt = ReturnStmt::createImplicit(ctx, initCall);
22232216

22242217
auto body = BraceStmt::create(ctx, SourceLoc(), {returnStmt}, SourceLoc(),
22252218
/*implicit=*/true);

lib/Parse/ParseDecl.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8670,8 +8670,7 @@ Parser::parseAbstractFunctionBodyImpl(AbstractFunctionDecl *AFD) {
86708670
SourceLoc LBraceLoc, RBraceLoc;
86718671
LBraceLoc = consumeToken(tok::l_brace);
86728672
auto *CCE = new (Context) CodeCompletionExpr(Tok.getLoc());
8673-
auto *Return =
8674-
new (Context) ReturnStmt(SourceLoc(), CCE, /*implicit=*/true);
8673+
auto *Return = ReturnStmt::createImplicit(Context, CCE);
86758674
CodeCompletionCallbacks->setParsedDecl(accessor);
86768675
CodeCompletionCallbacks->completeAccessorBeginning(CCE);
86778676
RBraceLoc = Tok.getLoc();
@@ -8725,15 +8724,15 @@ Parser::parseAbstractFunctionBodyImpl(AbstractFunctionDecl *AFD) {
87258724
}
87268725
}
87278726
if (isa<FuncDecl>(AFD)) {
8728-
auto RS = new (Context) ReturnStmt(SourceLoc(), E);
8727+
auto RS = ReturnStmt::createImplicit(Context, E);
87298728
BS->setLastElement(RS);
87308729
AFD->setHasSingleExpressionBody();
87318730
AFD->setSingleExpressionBody(E);
87328731
} else if (auto *F = dyn_cast<ConstructorDecl>(AFD)) {
87338732
if (F->isFailable() && isa<NilLiteralExpr>(E)) {
87348733
// If it's a nil literal, just insert return. This is the only
87358734
// legal thing to return.
8736-
auto RS = new (Context) ReturnStmt(E->getStartLoc(), E);
8735+
auto RS = ReturnStmt::createImplicit(Context, E);
87378736
BS->setLastElement(RS);
87388737
AFD->setHasSingleExpressionBody();
87398738
AFD->setSingleExpressionBody(E);

lib/Parse/ParseExpr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3072,7 +3072,7 @@ ParserResult<Expr> Parser::parseExprClosure() {
30723072
// Create the wrapping return.
30733073
hasSingleExpressionBody = true;
30743074
auto returnExpr = Element.get<Expr*>();
3075-
BS->setLastElement(new (Context) ReturnStmt(SourceLoc(), returnExpr));
3075+
BS->setLastElement(ReturnStmt::createImplicit(Context, returnExpr));
30763076
}
30773077
}
30783078
}

0 commit comments

Comments
 (0)