Skip to content

Commit 8e14bba

Browse files
committed
fix <rdar://problem/21110580> guard let in top level code doesn't work
We were just not injecting the vardecls into the SourceFile, so unqual name lookup couldn't find them later. Swift SVN r29456
1 parent 96cc406 commit 8e14bba

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

lib/Parse/ParseStmt.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,18 @@ ParserStatus Parser::parseBraceItems(SmallVectorImpl<ASTNode> &Entries,
367367
Result, Result.getEndLoc());
368368
TLCD->setBody(Brace);
369369
Entries.push_back(TLCD);
370+
371+
// If the parsed stmt was a GuardStmt, push the VarDecls into the
372+
// Entries list, so that they can be found by unqual name lookup later.
373+
if (auto guard = dyn_cast_or_null<GuardStmt>(Result.dyn_cast<Stmt*>())){
374+
for (const auto &elt : guard->getCond()) {
375+
if (!elt.getPatternOrNull()) continue;
376+
377+
elt.getPattern()->forEachVariable([&](VarDecl *VD) {
378+
Entries.push_back(VD);
379+
});
380+
}
381+
}
370382
}
371383
} else {
372384
SourceLoc StartLoc = Tok.getLoc();

test/Interpreter/statements.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ for case let i? in OptionalArr where i != 3 {
4141
// CHECK-NEXT: 4
4242
// CHECK-NEXT: 5
4343

44+
print("guard top level")
45+
guard let guardvalue = Optional(42) else { preconditionFailure() }
46+
print(guardvalue)
4447

48+
// CHECK-LABEL: guard top level
49+
// CHECK-NEXT: 42
4550

4651
print("done");
4752
// CHECK-LABEL: done

0 commit comments

Comments
 (0)