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-15344] Trailing closure in guard condition gives inscrutable error #57666

Open
RonAvitzur opened this issue Oct 15, 2021 · 1 comment
Open
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself

Comments

@RonAvitzur
Copy link

Previous ID SR-15344
Radar None
Original Reporter @RonAvitzur
Type Bug
Environment

Version 13.0 beta 5 (13A5212g)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 668dc78ec042659d0b3a118604c2d487

Issue Description:

guard samples.contains{ $0.isInfinite } else { return }

gives

Anonymous closure argument not contained in a closure
Cannot convert value of type '((Sample) throws -> Bool) throws -> Bool' to expected condition type 'Bool'
Closure expression is unused
Consecutive statements on a line must be separated by ';'
Expected 'else' after 'guard' condition

For comparison, a trailing closure in an if condition gives:

{{ if samples.contains { $0.isInfinite } { return }}}}}{}}}

{{ ~^}}

{{ (where: )}}

While guard is using the same condition grammar that if uses, which prohibits the trailing closure here, since guard has no then clause the trailing closure is not actually ambiguous, so it either requires a slightly different diagnostic, or a relaxation of the grammar to allow the trailing closure in a guard condition.

@rintaro
Copy link
Mannequin

rintaro mannequin commented Oct 15, 2021

if case is actually not "ambiguous" either.
For 'if something { ... } { ... }', the first block can't be a then block because then blocks can't have a trailing block without `else`, also the second block can't be a trailing closure because multiple trailing closure requires labels. But it's is still confusable. That's why the diagnostic is a warning, and recommends using a parenthesized argument.

guard case is similar. It is not ambiguous, but confusable. So guard case should be diagnosed as a warning just like if.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself
Projects
None yet
Development

No branches or pull requests

1 participant