Skip to content

Commit 1ac8926

Browse files
committed
[Parser] Don't lose outer "disabled" variables when checking a guard's "else".
When checking for uses of "disabled" variables within the "else" statement of a "guard", keep track of the disabled variables from outer scopes. This is a pattern used everywhere else in the parser, but got missed here. Fixes SR-7567 / rdar://problem/39868144.
1 parent 5b6b335 commit 1ac8926

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

lib/Parse/ParseStmt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1612,7 +1612,7 @@ ParserResult<Stmt> Parser::parseStmtGuard() {
16121612
for (auto &elt : Condition)
16131613
if (auto pattern = elt.getPatternOrNull())
16141614
pattern->collectVariables(Vars);
1615-
1615+
Vars.append(DisabledVars.begin(), DisabledVars.end());
16161616
llvm::SaveAndRestore<decltype(DisabledVars)>
16171617
RestoreCurVars(DisabledVars, Vars);
16181618

test/stmt/statements.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,13 @@ func test_guard(_ x : Int, y : Int??, cond : Bool) {
383383

384384

385385
guard case _ = x else {} // expected-warning {{'guard' condition is always true, body is unreachable}}
386+
387+
// SR-7567
388+
guard let outer = y else {
389+
guard true else {
390+
print(outer) // expected-error {{variable declared in 'guard' condition is not usable in its body}}
391+
}
392+
}
386393
}
387394

388395
func test_is_as_patterns() {

0 commit comments

Comments
 (0)