Skip to content

Commit ed94b13

Browse files
committed
[ast] Add a helper method for checking if a pattern contains a var decl and adopt it.
1 parent 8311f2b commit ed94b13

File tree

3 files changed

+16
-19
lines changed

3 files changed

+16
-19
lines changed

include/swift/AST/Pattern.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,13 @@ class alignas(8) Pattern {
168168
/// pattern.
169169
void forEachVariable(llvm::function_ref<void(VarDecl *)> f) const;
170170

171+
/// Returns true if \p vd is in the pattern.
172+
bool containsVarDecl(const VarDecl *inputVD) const {
173+
bool result = false;
174+
forEachVariable([&](VarDecl *vd) { result |= inputVD == vd; });
175+
return result;
176+
}
177+
171178
/// apply the specified function to all pattern nodes recursively in
172179
/// this pattern. This is a pre-order traversal.
173180
void forEachNode(llvm::function_ref<void(Pattern *)> f);

lib/AST/Decl.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,14 +1307,14 @@ unsigned PatternBindingDecl::getPatternEntryIndexForVarDecl(const VarDecl *VD) c
13071307

13081308
auto List = getPatternList();
13091309
if (List.size() == 1) {
1310-
assert(patternContainsVarDeclBinding(List[0].getPattern(), VD) &&
1310+
assert(List[0].getPattern()->containsVarDecl(VD) &&
13111311
"Single entry PatternBindingDecl is set up wrong");
13121312
return 0;
13131313
}
13141314

13151315
unsigned Result = 0;
13161316
for (auto entry : List) {
1317-
if (patternContainsVarDeclBinding(entry.getPattern(), VD))
1317+
if (entry.getPattern()->containsVarDecl(VD))
13181318
return Result;
13191319
++Result;
13201320
}
@@ -4927,12 +4927,6 @@ SourceRange VarDecl::getTypeSourceRangeForDiagnostics() const {
49274927
return SourceRange();
49284928
}
49294929

4930-
static bool isVarInPattern(const VarDecl *vd, Pattern *p) {
4931-
bool foundIt = false;
4932-
p->forEachVariable([&](VarDecl *foundFD) { foundIt |= foundFD == vd; });
4933-
return foundIt;
4934-
}
4935-
49364930
static Optional<std::pair<CaseStmt *, Pattern *>>
49374931
findParentPatternCaseStmtAndPattern(const VarDecl *inputVD) {
49384932
auto getMatchingPattern = [&](CaseStmt *cs) -> Pattern * {
@@ -4946,7 +4940,7 @@ findParentPatternCaseStmtAndPattern(const VarDecl *inputVD) {
49464940

49474941
// Then check the rest of our case label items.
49484942
for (auto &item : cs->getMutableCaseLabelItems()) {
4949-
if (isVarInPattern(inputVD, item.getPattern())) {
4943+
if (item.getPattern()->containsVarDecl(inputVD)) {
49504944
return item.getPattern();
49514945
}
49524946
}
@@ -5039,15 +5033,15 @@ Pattern *VarDecl::getParentPattern() const {
50395033
// In a case statement, search for the pattern that contains it. This is
50405034
// a bit silly, because you can't have something like "case x, y:" anyway.
50415035
for (auto items : cs->getCaseLabelItems()) {
5042-
if (isVarInPattern(this, items.getPattern()))
5036+
if (items.getPattern()->containsVarDecl(this))
50435037
return items.getPattern();
50445038
}
50455039
}
50465040

50475041
if (auto *LCS = dyn_cast<LabeledConditionalStmt>(stmt)) {
50485042
for (auto &elt : LCS->getCond())
50495043
if (auto pat = elt.getPatternOrNull())
5050-
if (isVarInPattern(this, pat))
5044+
if (pat->containsVarDecl(this))
50515045
return pat;
50525046
}
50535047

lib/Sema/MiscDiagnostics.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2418,14 +2418,10 @@ VarDeclUsageChecker::~VarDeclUsageChecker() {
24182418
// Only diagnose VarDecls from the first CaseLabelItem in CaseStmts, as
24192419
// the remaining items must match it anyway.
24202420
auto CaseItems = CS->getCaseLabelItems();
2421-
if (!CaseItems.empty()) {
2422-
bool InFirstCaseLabelItem = false;
2423-
CaseItems.front().getPattern()->forEachVariable([&](VarDecl *D) {
2424-
InFirstCaseLabelItem |= var == D;
2425-
});
2426-
if (!InFirstCaseLabelItem)
2427-
continue;
2428-
}
2421+
assert(!CaseItems.empty() &&
2422+
"If we have any case stmt var decls, we should have a case item");
2423+
if (!CaseItems.front().getPattern()->containsVarDecl(var))
2424+
continue;
24292425
}
24302426

24312427
// If this is a 'let' value, any stores to it are actually initializations,

0 commit comments

Comments
 (0)