Skip to content

[Sema] SR-2327: cherry-pick the change from pull request 4286 for swift-3.0-branch #4345

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/Sema/TypeCheckStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -765,10 +765,14 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {

// If someone is using an unlabeled break inside of an 'if' or 'do'
// statement, produce a more specific error.
if (S->getTargetName().empty() && !ActiveLabeledStmts.empty() &&
(isa<IfStmt>(ActiveLabeledStmts.back()) ||
isa<DoStmt>(ActiveLabeledStmts.back())))
if (S->getTargetName().empty() &&
std::any_of(ActiveLabeledStmts.rbegin(),
ActiveLabeledStmts.rend(),
[&](Stmt *S) -> bool {
return isa<IfStmt>(S) || isa<DoStmt>(S);
})) {
diagid = diag::unlabeled_break_outside_loop;
}

TC.diagnose(S->getLoc(), diagid);
return nullptr;
Expand Down
22 changes: 22 additions & 0 deletions test/stmt/statements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,28 @@ func r25178926(_ a : Type) {
}
}

do {
guard 1 == 2 else {
break // expected-error {{unlabeled 'break' is only allowed inside a loop or switch, a labeled break is required to exit an if or do}}
}
}

func fn(a: Int) {
guard a < 1 else {
break // expected-error {{'break' is only allowed inside a loop, if, do, or switch}}
}
}

func fn(x: Int) {
if x >= 0 {
guard x < 1 else {
guard x < 2 else {
break // expected-error {{unlabeled 'break' is only allowed inside a loop or switch, a labeled break is required to exit an if or do}}
}
return
}
}
}


// Errors in case syntax
Expand Down