Skip to content

Commit 5ff7fa6

Browse files
authored
Merge pull request #12438 from gregomni/4082
[AST] Name lookup shouldn't find pattern declarations later in the same condition.
2 parents 74116d4 + 86d8c21 commit 5ff7fa6

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

lib/AST/NameLookupImpl.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,16 @@ class FindLocalVal : public StmtVisitor<FindLocalVal> {
121121
}
122122

123123
void checkStmtCondition(const StmtCondition &Cond) {
124-
for (auto entry : Cond)
125-
if (auto *P = entry.getPatternOrNull())
126-
if (!isReferencePointInRange(entry.getSourceRange()))
124+
SourceLoc start = SourceLoc();
125+
for (auto entry : Cond) {
126+
if (start.isInvalid())
127+
start = entry.getStartLoc();
128+
if (auto *P = entry.getPatternOrNull()) {
129+
SourceRange previousConditionsToHere = SourceRange(start, entry.getEndLoc());
130+
if (!isReferencePointInRange(previousConditionsToHere))
127131
checkPattern(P, DeclVisibilityKind::LocalVariable);
132+
}
133+
}
128134
}
129135

130136
void visitIfStmt(IfStmt *S) {

test/NameBinding/name_lookup.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,3 +566,9 @@ func foo1() {
566566
_ = MyGenericEnum<Int>.One // expected-error {{enum type 'MyGenericEnum<Int>' has no case 'One'; did you mean 'one'}}{{26-29=one}}
567567
_ = MyGenericEnum<Int>.OneTwo // expected-error {{enum type 'MyGenericEnum<Int>' has no case 'OneTwo'; did you mean 'oneTwo'}}{{26-32=oneTwo}}
568568
}
569+
570+
// SR-4082
571+
func foo2() {
572+
let x = 5
573+
if x < 0, let x = Optional(1) { } // expected-warning {{immutable value 'x' was never used; consider replacing with '_' or removing it}}
574+
}

0 commit comments

Comments
 (0)