Skip to content

Commit 84befd4

Browse files
committed
[AST] Make case body variables for CaseStmt non-optional
We don't really care about the distinction between empty and nil here.
1 parent 3e97d72 commit 84befd4

13 files changed

+34
-50
lines changed

include/swift/AST/Stmt.h

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,13 +1222,12 @@ class CaseStmt final
12221222

12231223
llvm::PointerIntPair<BraceStmt *, 1, bool> BodyAndHasFallthrough;
12241224

1225-
std::optional<ArrayRef<VarDecl *>> CaseBodyVariables;
1225+
ArrayRef<VarDecl *> CaseBodyVariables;
12261226

12271227
CaseStmt(CaseParentKind ParentKind, SourceLoc ItemIntroducerLoc,
12281228
ArrayRef<CaseLabelItem> CaseLabelItems, SourceLoc UnknownAttrLoc,
12291229
SourceLoc ItemTerminatorLoc, BraceStmt *Body,
1230-
std::optional<ArrayRef<VarDecl *>> CaseBodyVariables,
1231-
std::optional<bool> Implicit,
1230+
ArrayRef<VarDecl *> CaseBodyVariables, std::optional<bool> Implicit,
12321231
NullablePtr<FallthroughStmt> fallthroughStmt);
12331232

12341233
public:
@@ -1248,7 +1247,7 @@ class CaseStmt final
12481247
create(ASTContext &C, CaseParentKind ParentKind, SourceLoc ItemIntroducerLoc,
12491248
ArrayRef<CaseLabelItem> CaseLabelItems, SourceLoc UnknownAttrLoc,
12501249
SourceLoc ItemTerminatorLoc, BraceStmt *Body,
1251-
std::optional<ArrayRef<VarDecl *>> CaseBodyVariables,
1250+
ArrayRef<VarDecl *> CaseBodyVariables,
12521251
std::optional<bool> Implicit = std::nullopt,
12531252
NullablePtr<FallthroughStmt> fallthroughStmt = nullptr);
12541253

@@ -1293,7 +1292,7 @@ class CaseStmt final
12931292
void setBody(BraceStmt *body) { BodyAndHasFallthrough.setPointer(body); }
12941293

12951294
/// True if the case block declares any patterns with local variable bindings.
1296-
bool hasBoundDecls() const { return CaseBodyVariables.has_value(); }
1295+
bool hasCaseBodyVariables() const { return !CaseBodyVariables.empty(); }
12971296

12981297
/// Get the source location of the 'case', 'default', or 'catch' of the first
12991298
/// label.
@@ -1345,20 +1344,8 @@ class CaseStmt final
13451344
}
13461345

13471346
/// Return an ArrayRef containing the case body variables of this CaseStmt.
1348-
///
1349-
/// Asserts if case body variables was not explicitly initialized. In contexts
1350-
/// where one wants a non-asserting version, \see
1351-
/// getCaseBodyVariablesOrEmptyArray.
13521347
ArrayRef<VarDecl *> getCaseBodyVariables() const {
1353-
return *CaseBodyVariables;
1354-
}
1355-
1356-
bool hasCaseBodyVariables() const { return CaseBodyVariables.has_value(); }
1357-
1358-
ArrayRef<VarDecl *> getCaseBodyVariablesOrEmptyArray() const {
1359-
if (!CaseBodyVariables)
1360-
return ArrayRef<VarDecl *>();
1361-
return *CaseBodyVariables;
1348+
return CaseBodyVariables;
13621349
}
13631350

13641351
/// Find the next case statement within the same 'switch' or 'do-catch',

lib/AST/ASTScopeLookup.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -378,10 +378,10 @@ bool CaseLabelItemScope::lookupLocalsOrMembers(DeclConsumer consumer) const {
378378
}
379379

380380
bool CaseStmtBodyScope::lookupLocalsOrMembers(DeclConsumer consumer) const {
381-
for (auto *var : stmt->getCaseBodyVariablesOrEmptyArray())
381+
for (auto *var : stmt->getCaseBodyVariables()) {
382382
if (consumer.consume({var}))
383-
return true;
384-
383+
return true;
384+
}
385385
return false;
386386
}
387387

lib/AST/ASTVerifier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2795,7 +2795,7 @@ class Verifier : public ASTWalker {
27952795
// guarantee that all case label items bind corresponding patterns and
27962796
// the case body var decls of a case stmt are created from the var decls
27972797
// of the first case label items.
2798-
if (!caseStmt->hasBoundDecls()) {
2798+
if (!caseStmt->hasCaseBodyVariables()) {
27992799
Out << "parent CaseStmt of VarDecl does not have any case body "
28002800
"decls?!\n";
28012801
abort();

lib/AST/Decl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8164,7 +8164,7 @@ findParentPatternCaseStmtAndPattern(const VarDecl *inputVD) {
81648164
auto getMatchingPattern = [&](CaseStmt *cs) -> Pattern * {
81658165
// Check if inputVD is in our case body var decls if we have any. If we do,
81668166
// treat its pattern as our first case label item pattern.
8167-
for (auto *vd : cs->getCaseBodyVariablesOrEmptyArray()) {
8167+
for (auto *vd : cs->getCaseBodyVariables()) {
81688168
if (vd == inputVD) {
81698169
return cs->getMutableCaseLabelItems().front().getPattern();
81708170
}
@@ -8345,7 +8345,7 @@ bool VarDecl::isCaseBodyVariable() const {
83458345
auto *caseStmt = dyn_cast_or_null<CaseStmt>(getRecursiveParentPatternStmt());
83468346
if (!caseStmt)
83478347
return false;
8348-
return llvm::any_of(caseStmt->getCaseBodyVariablesOrEmptyArray(),
8348+
return llvm::any_of(caseStmt->getCaseBodyVariables(),
83498349
[&](VarDecl *vd) { return vd == this; });
83508350
}
83518351

lib/AST/Stmt.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -752,8 +752,7 @@ SourceLoc CaseLabelItem::getEndLoc() const {
752752
CaseStmt::CaseStmt(CaseParentKind parentKind, SourceLoc itemIntroducerLoc,
753753
ArrayRef<CaseLabelItem> caseLabelItems,
754754
SourceLoc unknownAttrLoc, SourceLoc itemTerminatorLoc,
755-
BraceStmt *body,
756-
std::optional<ArrayRef<VarDecl *>> caseBodyVariables,
755+
BraceStmt *body, ArrayRef<VarDecl *> caseBodyVariables,
757756
std::optional<bool> implicit,
758757
NullablePtr<FallthroughStmt> fallthroughStmt)
759758
: Stmt(StmtKind::Case, getDefaultImplicitFlag(implicit, itemIntroducerLoc)),
@@ -781,7 +780,7 @@ CaseStmt::CaseStmt(CaseParentKind parentKind, SourceLoc itemIntroducerLoc,
781780
new (&items[i]) CaseLabelItem(caseLabelItems[i]);
782781
items[i].getPattern()->markOwnedByStatement(this);
783782
}
784-
for (auto *vd : getCaseBodyVariablesOrEmptyArray()) {
783+
for (auto *vd : getCaseBodyVariables()) {
785784
vd->setParentPatternStmt(this);
786785
}
787786
}
@@ -871,8 +870,7 @@ CaseStmt *
871870
CaseStmt::create(ASTContext &ctx, CaseParentKind ParentKind, SourceLoc caseLoc,
872871
ArrayRef<CaseLabelItem> caseLabelItems,
873872
SourceLoc unknownAttrLoc, SourceLoc colonLoc, BraceStmt *body,
874-
std::optional<ArrayRef<VarDecl *>> caseVarDecls,
875-
std::optional<bool> implicit,
873+
ArrayRef<VarDecl *> caseVarDecls, std::optional<bool> implicit,
876874
NullablePtr<FallthroughStmt> fallthroughStmt) {
877875
void *mem =
878876
ctx.Allocate(totalSizeToAlloc<FallthroughStmt *, CaseLabelItem>(

lib/SILGen/SILGenPattern.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2836,7 +2836,7 @@ void PatternMatchEmission::initSharedCaseBlockDest(CaseStmt *caseBlock,
28362836
result.first->second.first = block;
28372837

28382838
// Add args for any pattern variables if we have any.
2839-
for (auto *vd : caseBlock->getCaseBodyVariablesOrEmptyArray()) {
2839+
for (auto *vd : caseBlock->getCaseBodyVariables()) {
28402840
if (!vd->hasName())
28412841
continue;
28422842

@@ -2867,7 +2867,7 @@ void PatternMatchEmission::emitAddressOnlyAllocations() {
28672867
// If we have a shared case with bound decls, setup the arguments for the
28682868
// shared block by emitting the temporary allocation used for the arguments
28692869
// of the shared block.
2870-
for (auto *vd : caseBlock->getCaseBodyVariablesOrEmptyArray()) {
2870+
for (auto *vd : caseBlock->getCaseBodyVariables()) {
28712871
if (!vd->hasName())
28722872
continue;
28732873

lib/Sema/BuilderTransform.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ class ResultBuilderTransform
650650
caseStmt->getCaseLabelItems(),
651651
caseStmt->hasUnknownAttr() ? caseStmt->getStartLoc() : SourceLoc(),
652652
caseStmt->getItemTerminatorLoc(), cloneBraceWith(body, newBody),
653-
caseStmt->getCaseBodyVariablesOrEmptyArray(), caseStmt->isImplicit(),
653+
caseStmt->getCaseBodyVariables(), caseStmt->isImplicit(),
654654
caseStmt->getFallthroughStmt());
655655

656656
return std::make_pair(caseVarRef.get(), newCase);

lib/Sema/CSSyntacticElement.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,7 +1378,7 @@ class SyntacticElementConstraintGenerator
13781378
pattern->forEachVariable([&](VarDecl *var) { recordVar(var); });
13791379
}
13801380

1381-
for (auto bodyVar : caseStmt->getCaseBodyVariablesOrEmptyArray()) {
1381+
for (auto bodyVar : caseStmt->getCaseBodyVariables()) {
13821382
if (!bodyVar->hasName())
13831383
continue;
13841384

@@ -2007,7 +2007,7 @@ class SyntacticElementSolutionApplication
20072007

20082008
bindSwitchCasePatternVars(context.getAsDeclContext(), caseStmt);
20092009

2010-
for (auto *expected : caseStmt->getCaseBodyVariablesOrEmptyArray()) {
2010+
for (auto *expected : caseStmt->getCaseBodyVariables()) {
20112011
assert(expected->hasName());
20122012
auto prev = expected->getParentVarDecl();
20132013
auto type = solution.getResolvedType(prev)->mapTypeOutOfContext();

lib/Sema/DerivedConformance/DerivedConformanceCodable.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,7 @@ createEnumSwitch(ASTContext &C, DeclContext *DC, Expr *expr, EnumDecl *enumDecl,
946946
// .<elt>(let a0, let a1, ...)
947947
SmallVector<VarDecl *, 3> payloadVars;
948948
Pattern *subpattern = nullptr;
949-
std::optional<MutableArrayRef<VarDecl *>> caseBodyVarDecls;
949+
ArrayRef<VarDecl *> caseBodyVarDecls;
950950

951951
if (createSubpattern) {
952952
subpattern = DerivedConformance::enumElementPayloadSubpattern(
@@ -965,7 +965,7 @@ createEnumSwitch(ASTContext &C, DeclContext *DC, Expr *expr, EnumDecl *enumDecl,
965965
vNew->setImplicit();
966966
copy[i] = vNew;
967967
}
968-
caseBodyVarDecls.emplace(copy);
968+
caseBodyVarDecls = copy;
969969
}
970970
}
971971

@@ -988,8 +988,7 @@ createEnumSwitch(ASTContext &C, DeclContext *DC, Expr *expr, EnumDecl *enumDecl,
988988
auto stmt =
989989
CaseStmt::create(C, CaseParentKind::Switch, SourceLoc(), labelItem,
990990
SourceLoc(), SourceLoc(), caseBody,
991-
/*case body vardecls*/
992-
createSubpattern ? caseBodyVarDecls : std::nullopt);
991+
caseBodyVarDecls);
993992
cases.push_back(stmt);
994993
}
995994
}

lib/Sema/DerivedConformance/DerivedConformanceComparable.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ deriveBodyComparable_enum_hasAssociatedValues_lt(AbstractFunctionDecl *ltDecl, v
128128
enumType, elt, rhsSubpattern, /*DC*/ ltDecl);
129129

130130
auto hasBoundDecls = !lhsPayloadVars.empty();
131-
std::optional<MutableArrayRef<VarDecl *>> caseBodyVarDecls;
131+
ArrayRef<VarDecl *> caseBodyVarDecls;
132132
if (hasBoundDecls) {
133133
// We allocated a direct copy of our lhs var decls for the case
134134
// body.
@@ -141,7 +141,7 @@ deriveBodyComparable_enum_hasAssociatedValues_lt(AbstractFunctionDecl *ltDecl, v
141141
vNew->setImplicit();
142142
copy[i] = vNew;
143143
}
144-
caseBodyVarDecls.emplace(copy);
144+
caseBodyVarDecls = copy;
145145
}
146146

147147
// case (.<elt>(let l0, let l1, ...), .<elt>(let r0, let r1, ...))

0 commit comments

Comments
 (0)