Skip to content

Commit de8deb0

Browse files
committed
Merge pull request #1481 from gregomni/switch-cases
Improve var decl completion in switch cases
2 parents e8d2867 + 30b6734 commit de8deb0

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

Diff for: include/swift/AST/Stmt.h

+3
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,9 @@ class CaseStmt final : public Stmt,
985985

986986
SourceLoc getStartLoc() const { return getLoc(); }
987987
SourceLoc getEndLoc() const { return getBody()->getEndLoc(); }
988+
SourceRange getLabelItemsRange() const {
989+
return ColonLoc.isValid() ? SourceRange(getLoc(), ColonLoc) : getSourceRange();
990+
}
988991

989992
bool isDefault() { return getCaseLabelItems()[0].isDefault(); }
990993

Diff for: lib/AST/NameLookupImpl.h

+16-5
Original file line numberDiff line numberDiff line change
@@ -223,12 +223,23 @@ class FindLocalVal : public StmtVisitor<FindLocalVal> {
223223
void visitCaseStmt(CaseStmt *S) {
224224
if (!isReferencePointInRange(S->getSourceRange()))
225225
return;
226-
for (const auto &CLI : S->getCaseLabelItems()) {
227-
auto *P = CLI.getPattern();
228-
if (!isReferencePointInRange(P->getSourceRange()))
229-
checkPattern(P, DeclVisibilityKind::LocalVariable);
226+
// Pattern names aren't visible in the patterns themselves,
227+
// just in the body or in where guards.
228+
auto body = S->getBody();
229+
bool inPatterns = isReferencePointInRange(S->getLabelItemsRange());
230+
auto items = S->getCaseLabelItems();
231+
if (inPatterns) {
232+
for (const auto &CLI : items) {
233+
auto guard = CLI.getGuardExpr();
234+
if (guard && isReferencePointInRange(guard->getSourceRange())) {
235+
inPatterns = false;
236+
break;
237+
}
238+
}
230239
}
231-
visit(S->getBody());
240+
if (!inPatterns && items.size() > 0)
241+
checkPattern(items[0].getPattern(), DeclVisibilityKind::LocalVariable);
242+
visit(body);
232243
}
233244

234245
void visitDoCatchStmt(DoCatchStmt *S) {

Diff for: test/IDE/complete_pattern.swift

+16-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=MULTI_PATTERN_1 | FileCheck %s -check-prefix=MULTI_PATTERN_1
2424
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=MULTI_PATTERN_2 | FileCheck %s -check-prefix=MULTI_PATTERN_2
2525
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=MULTI_PATTERN_3 | FileCheck %s -check-prefix=MULTI_PATTERN_3
26+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=MULTI_PATTERN_4 | FileCheck %s -check-prefix=MULTI_PATTERN_4
2627

2728

2829
//===--- Helper types that are used in this test
@@ -155,13 +156,13 @@ func test_multiple_patterns1(x: Int) {
155156
}
156157

157158
// MULTI_PATTERN_1: Begin completions
158-
// FIXME: ideally we wouldn't offer 'a' here, without 'let' first:
159-
// xxxMULTI_PATTERN_1-NOT: Decl[LocalVar]/Local: a[#Int#]{{; name=.+$}}
159+
// MULTI_PATTERN_1-NOT: Decl[LocalVar]/Local: a[#Int#]{{; name=.+$}}
160+
// MULTI_PATTERN_1-DAG: Decl[LocalVar]/Local: x[#Int#]{{; name=.+$}}
160161
// MULTI_PATTERN_1: End completions
161162

162163
func test_multiple_patterns2(x: Int) {
163164
switch (x, x) {
164-
case let (0, let a), (let a, 0):
165+
case (0, let a), (let a, 0):
165166
#^MULTI_PATTERN_2^#
166167
}
167168
}
@@ -173,7 +174,7 @@ func test_multiple_patterns2(x: Int) {
173174

174175
func test_multiple_patterns3(x: Int) {
175176
switch (x, x) {
176-
case let (0, let a), (let b, 0):
177+
case (0, let a), (let b, 0):
177178
#^MULTI_PATTERN_3^#
178179
}
179180
}
@@ -182,4 +183,15 @@ func test_multiple_patterns3(x: Int) {
182183
// MULTI_PATTERN_3-DAG: Decl[LocalVar]/Local: x[#Int#]{{; name=.+$}}
183184
// MULTI_PATTERN_3: End completions
184185

186+
func test_multiple_patterns4(x: Int) {
187+
switch (x, x) {
188+
case (0, let a) where #^MULTI_PATTERN_4^#
189+
190+
}
191+
}
192+
193+
// MULTI_PATTERN_4: Begin completions
194+
// MULTI_PATTERN_4-DAG: Decl[LocalVar]/Local: a[#Int#]{{; name=.+$}}
195+
// MULTI_PATTERN_4-DAG: Decl[LocalVar]/Local: x[#Int#]{{; name=.+$}}
196+
// MULTI_PATTERN_4: End completions
185197

0 commit comments

Comments
 (0)