[ast] Add a helper method for checking if a pattern contains a var decl and adopt it.
diff --git a/include/swift/AST/Pattern.h b/include/swift/AST/Pattern.h
index 27397b1..869fd2b 100644
--- a/include/swift/AST/Pattern.h
+++ b/include/swift/AST/Pattern.h
@@ -168,6 +168,13 @@
/// pattern.
void forEachVariable(llvm::function_ref<void(VarDecl *)> f) const;
+ /// Returns true if \p vd is in the pattern.
+ bool containsVarDecl(const VarDecl *inputVD) const {
+ bool result = false;
+ forEachVariable([&](VarDecl *vd) { result |= inputVD == vd; });
+ return result;
+ }
+
/// apply the specified function to all pattern nodes recursively in
/// this pattern. This is a pre-order traversal.
void forEachNode(llvm::function_ref<void(Pattern *)> f);
diff --git a/lib/AST/Decl.cpp b/lib/AST/Decl.cpp
index 8706853..73e951f 100644
--- a/lib/AST/Decl.cpp
+++ b/lib/AST/Decl.cpp
@@ -1307,14 +1307,14 @@
auto List = getPatternList();
if (List.size() == 1) {
- assert(patternContainsVarDeclBinding(List[0].getPattern(), VD) &&
+ assert(List[0].getPattern()->containsVarDecl(VD) &&
"Single entry PatternBindingDecl is set up wrong");
return 0;
}
unsigned Result = 0;
for (auto entry : List) {
- if (patternContainsVarDeclBinding(entry.getPattern(), VD))
+ if (entry.getPattern()->containsVarDecl(VD))
return Result;
++Result;
}
@@ -4927,12 +4927,6 @@
return SourceRange();
}
-static bool isVarInPattern(const VarDecl *vd, Pattern *p) {
- bool foundIt = false;
- p->forEachVariable([&](VarDecl *foundFD) { foundIt |= foundFD == vd; });
- return foundIt;
-}
-
static Optional<std::pair<CaseStmt *, Pattern *>>
findParentPatternCaseStmtAndPattern(const VarDecl *inputVD) {
auto getMatchingPattern = [&](CaseStmt *cs) -> Pattern * {
@@ -4946,7 +4940,7 @@
// Then check the rest of our case label items.
for (auto &item : cs->getMutableCaseLabelItems()) {
- if (isVarInPattern(inputVD, item.getPattern())) {
+ if (item.getPattern()->containsVarDecl(inputVD)) {
return item.getPattern();
}
}
@@ -5039,7 +5033,7 @@
// In a case statement, search for the pattern that contains it. This is
// a bit silly, because you can't have something like "case x, y:" anyway.
for (auto items : cs->getCaseLabelItems()) {
- if (isVarInPattern(this, items.getPattern()))
+ if (items.getPattern()->containsVarDecl(this))
return items.getPattern();
}
}
@@ -5047,7 +5041,7 @@
if (auto *LCS = dyn_cast<LabeledConditionalStmt>(stmt)) {
for (auto &elt : LCS->getCond())
if (auto pat = elt.getPatternOrNull())
- if (isVarInPattern(this, pat))
+ if (pat->containsVarDecl(this))
return pat;
}
diff --git a/lib/Sema/MiscDiagnostics.cpp b/lib/Sema/MiscDiagnostics.cpp
index 8f40b9e..47648e1 100644
--- a/lib/Sema/MiscDiagnostics.cpp
+++ b/lib/Sema/MiscDiagnostics.cpp
@@ -2418,14 +2418,10 @@
// Only diagnose VarDecls from the first CaseLabelItem in CaseStmts, as
// the remaining items must match it anyway.
auto CaseItems = CS->getCaseLabelItems();
- if (!CaseItems.empty()) {
- bool InFirstCaseLabelItem = false;
- CaseItems.front().getPattern()->forEachVariable([&](VarDecl *D) {
- InFirstCaseLabelItem |= var == D;
- });
- if (!InFirstCaseLabelItem)
- continue;
- }
+ assert(!CaseItems.empty() &&
+ "If we have any case stmt var decls, we should have a case item");
+ if (!CaseItems.front().getPattern()->containsVarDecl(var))
+ continue;
}
// If this is a 'let' value, any stores to it are actually initializations,