Skip to content

[Sema] SR-2327: Improve error message when using 'break' inside 'guar… #4286

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 16, 2016

Conversation

MnO2
Copy link
Contributor

@MnO2 MnO2 commented Aug 13, 2016

What's in this pull request?

Improve the error message what SR-2327 has described.

With

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

prompt the more helpful error message of error: unlabeled 'break' is only allowed inside a loop or switch, a labeled break is required to exit an if or do

Resolved bug number: (SR-2327)


Before merging this pull request to apple/swift repository:

  • Test pull request on Swift continuous integration.

Triggering Swift CI

The swift-ci is triggered by writing a comment on this PR addressed to the GitHub user @swift-ci. Different tests will run depending on the specific comment that you use. The currently available comments are:

Smoke Testing

Platform Comment
All supported platforms @swift-ci Please smoke test
All supported platforms @swift-ci Please smoke test and merge
OS X platform @swift-ci Please smoke test OS X platform
Linux platform @swift-ci Please smoke test Linux platform

A smoke test on macOS does the following:

  1. Builds the compiler incrementally.
  2. Builds the standard library only for macOS. Simulator standard libraries and
    device standard libraries are not built.
  3. lldb is not built.
  4. The test and validation-test targets are run only for macOS. The optimized
    version of these tests are not run.

A smoke test on Linux does the following:

  1. Builds the compiler incrementally.
  2. Builds the standard library incrementally.
  3. lldb is built incrementally.
  4. The swift test and validation-test targets are run. The optimized version of these
    tests are not run.
  5. lldb is tested.

Validation Testing

Platform Comment
All supported platforms @swift-ci Please test
All supported platforms @swift-ci Please test and merge
OS X platform @swift-ci Please test OS X platform
OS X platform @swift-ci Please benchmark
Linux platform @swift-ci Please test Linux platform

Lint Testing

Language Comment
Python @swift-ci Please Python lint

Note: Only members of the Apple organization can trigger swift-ci.

…d' inside 'do' or 'if'

@CodaFi
Copy link
Contributor

CodaFi commented Aug 13, 2016

Very cool!

@swift-ci please smoke test OS X platform.

@@ -767,7 +767,8 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
// statement, produce a more specific error.
if (S->getTargetName().empty() && !ActiveLabeledStmts.empty() &&
(isa<IfStmt>(ActiveLabeledStmts.back()) ||
isa<DoStmt>(ActiveLabeledStmts.back())))
isa<DoStmt>(ActiveLabeledStmts.back()) ||
isa<GuardStmt>(ActiveLabeledStmts.back())))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work for this?

func fn(a: Int) {
  guard a < 1 else {
    break
  }
}

In this case, IMO, the message should be normal 'break' is only allowed inside a loop, if, do, or switch.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we put if, do, in single quotes, just like break?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rintaro I've updated the code to handle the case and added your example into the test cases.

@tkremenek I prefer to submit another pull request to change the format of diagnosis message. I am also in favor of quoting if and do, but touching the message in this commit would make some noises on the logic change behind this commit.

Copy link
Member

@tkremenek tkremenek Aug 14, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rintaro @MnO2 That works for me.

@xwu
Copy link
Collaborator

xwu commented Aug 14, 2016

One suggestion: either in this commit or in the follow-up, probably best to change the guard_body_must_not_fallthrough diagnostic message from...

'guard' body may not fall through, consider using 'return' or 'break'

...to...

'guard' body may not fall through, consider using 'return' or labeled 'break'

}
}
}
}
Copy link
Member

@rintaro rintaro Aug 15, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for nit-picking. This doesn't work for

func fn(x: Int) {
  if x >= 0 {
    guard x < 1 else {
      guard x < 2 else {
        break
      }
      return
    }
  }
}

The rule here is: If ActiveLabeledStmts contains if or do, we can use unlabeled_break_outside_loop.
I think, something like

if (S->getTargetName().empty() && 
    std::any_of(ActiveLabeledStmts.rbegin(), ActiveLabeledStmts.rend(),
      [&](Stmt *S) -> bool { return isa<IfStmt>(S) || isa<DoStmt>(S); }))

should work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are absolutely right, I would change accordingly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made the change and updated the test cases. :-)

diagid = diag::unlabeled_break_outside_loop;
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant, this whole if block can be replaced with:

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;

I think we don't need to care about guard statement here.
Am I missing something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh! duh, (to myself) what were you thinking. Thanks for pointing out. I've stolen your snippet and copied-pasted and pushed. :)

@jrose-apple
Copy link
Contributor

The intent of "return or break" is to break out of a loop being guarded, not to exit the guard itself.

@rintaro
Copy link
Member

rintaro commented Aug 16, 2016

@jrose-apple I don't understand the point. What do you mean?
We can break out of labeled if or do as well.

LABEL: do {
  guard [condition] else {
    break LABEL
  }
  // ...
}

@jrose-apple
Copy link
Contributor

Sorry, yes, you can. But to have the error message suggest a labeled break (per @xwu) might imply that you can't use it with unlabeled break, which of course you can if there's a loop:

for person in friends {
  guard person.name != "Jordan" else { break }
}

@xwu
Copy link
Collaborator

xwu commented Aug 16, 2016

(D'oh!)

@rintaro
Copy link
Member

rintaro commented Aug 16, 2016

Ah, make sense. You are replying to @xwu's comment 😄

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); })) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: swiftc style limits lines to 80 columns. Can you reflow this, or use a helper variable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I've inserted a few new lines in between.

@jrose-apple
Copy link
Contributor

@swift-ci Please test

@jrose-apple
Copy link
Contributor

Linux, OS X

@rintaro
Copy link
Member

rintaro commented Aug 16, 2016

Test passed.
The change looks good to me.
Thank you! @MnO2

@jrose-apple
Copy link
Contributor

Force-merging. Thanks, Paul!

@jrose-apple jrose-apple merged commit af36803 into swiftlang:master Aug 16, 2016
@tkremenek
Copy link
Member

@MnO2 please create a pull request for swift-3.0-branch to pull this change in there.

@MnO2
Copy link
Contributor Author

MnO2 commented Aug 17, 2016

I cherry-picked the change and created pull-request against swift-3.0-branch: #4345

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants