Skip to content
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

[SR-2327] Improve error message when using 'break' inside 'guard' inside 'do' or 'if' #44934

Closed
swift-ci opened this issue Aug 12, 2016 · 2 comments
Labels
compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation good first issue Good for newcomers improvement

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-2327
Radar None
Original Reporter deckard (JIRA User)
Type Improvement
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Improvement, DiagnosticsQoI, StarterBug
Assignee PaulMeng (JIRA)
Priority Medium

md5: e84f619c42ea5e8b503c69325237b44f

Issue Description:

With the following code:

do {
    guard 1 == 2 else { break }
}

The compiler give the not-so-helpful error message:

error: 'break' is only allowed inside a loop, if, do, or switch

It would be much clearer to the user what the problem is and how to fix it, if it gave the same error message as it does in this case:

do {
    break
}

Which is:

error: unlabeled 'break' is only allowed inside a loop or switch, a labeled break is required to exit an if or do

The same problem occurs with if statements. For example, "if (true) { guard 1 == 2 else { break } }" gives the unhelpful message but "if (true) { break }" gives the helpful one.

@swift-ci
Copy link
Collaborator Author

Comment by Paul Meng (JIRA)

A fix attempt with the example added as test case: #4286

@davidbjames
Copy link

Can someone tell me why this is an error in the first place?

do {
    guard 1 == 2 else { break }
}

Why can't the compiler deduce I want to break out of the `do` block?

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation good first issue Good for newcomers improvement
Projects
None yet
Development

No branches or pull requests

2 participants