Skip to content

Commit 97a8919

Browse files
committed
guard statements aren't really labeled statements
`guard` statements are prohibited from having labels, and aren't actually break/continue targets. Stop producing them as results from ASTScope-based labeled statement lookup and don't add them as a labeled statement in the recursive walk.
1 parent 2e03726 commit 97a8919

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

include/swift/AST/ASTScope.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1820,6 +1820,7 @@ class LookupParentDiversionScope final : public ASTScopeImpl {
18201820
NullablePtr<const ASTScopeImpl> getLookupParent() const override {
18211821
return lookupParent;
18221822
}
1823+
bool isLabeledStmtLookupTerminator() const override;
18231824
};
18241825

18251826
class RepeatWhileScope final : public AbstractStmtScope {

lib/AST/ASTScopeLookup.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,10 @@ bool ASTScopeImpl::isLabeledStmtLookupTerminator() const {
858858
return true;
859859
}
860860

861+
bool LookupParentDiversionScope::isLabeledStmtLookupTerminator() const {
862+
return false;
863+
}
864+
861865
bool ConditionalClauseScope::isLabeledStmtLookupTerminator() const {
862866
return false;
863867
}
@@ -886,12 +890,17 @@ ASTScopeImpl::lookupLabeledStmts(SourceFile *sourceFile, SourceLoc loc) {
886890
for (auto scope = innermost; scope && !scope->isLabeledStmtLookupTerminator();
887891
scope = scope->getParent().getPtrOrNull()) {
888892
// If we have a labeled statement, record it.
889-
if (auto stmt = scope->getStmtIfAny()) {
890-
if (auto labeledStmt = dyn_cast<LabeledStmt>(stmt.get())) {
891-
labeledStmts.push_back(labeledStmt);
892-
continue;
893-
}
894-
}
893+
auto stmt = scope->getStmtIfAny();
894+
if (!stmt) continue;
895+
896+
auto labeledStmt = dyn_cast<LabeledStmt>(stmt.get());
897+
if (!labeledStmt) continue;
898+
899+
// Skip guard statements; they aren't actually targets for break or
900+
// continue.
901+
if (isa<GuardStmt>(labeledStmt)) continue;
902+
903+
labeledStmts.push_back(labeledStmt);
895904
}
896905

897906
return labeledStmts;

lib/Sema/TypeCheckStmt.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -687,8 +687,6 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
687687
Stmt *visitGuardStmt(GuardStmt *GS) {
688688
TypeChecker::typeCheckConditionForStatement(GS, DC);
689689

690-
AddLabeledStmt ifNest(*this, GS);
691-
692690
Stmt *S = GS->getBody();
693691
typeCheckStmt(S);
694692
GS->setBody(S);

0 commit comments

Comments
 (0)