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-5869] trailing closure not parsed correctly in if-statement conditional #48439
Comments
I don't think we have other bugs about this problem. We currently recover only if closure is the outer most expression: error: MyPlayground.playground:5:11: error: trailing closure requires parentheses for disambiguation in this context
if foo(1) { $0 + 42 } { }
~~^
, handler: )
error: trailing closure requires parentheses for disambiguation in this context
if bar { $0 + 42 }, baz() { }
~^
( ) We should expand our recovery logic, say "if |
That makes sense. We could even look for the absence of a space, since people conventionally don't put spaces in for any of those. |
(but just checking the same line is probably sufficient) |
I see this bug hasn't been updated in a while, but just FYI I've stumbled upon a case where trailing closures are actually allowed in an if statement, but probably shouldn't be: func someFuncWithClosure(closure: (Int) -> Bool) -> Bool {
return closure(4)
}
// this prints "true", with no warnings
if true || someFuncWithClosure { $0 == 4 } {
print("true")
} |
Still getting very unhelpful diagnostics from the compiler on this. I just encountered it as follows: if let stat = stats.filter{$0.dateFinished == date}.first {} Result is error messages as follows:
But none of these gets at the heart of the matter, which is merely that we need to surround `stats.filter{$0.dateFinished == date}` with parentheses. |
I had PR for this here #12457 . |
Comment by Owen Voorhees (JIRA) Resolved in #28362 If possible, please verify and close using the next available master snapshot. |
Environment
Xcode 8.3, Xcode 9 beta 6, macOS 10.12.6
Additional Detail from JIRA
md5: ad36f443386a825f7e352edd3209ab5c
is duplicated by:
if
Issue Description:
Summary: When using a function that takes a trailing closure as part of an if-statement conditional, writing the closure in the idiomatic `function { %0.thing }` style causes a compiler error. Writing the closure in the non-idiomatic `function({ $0.thing })` style works, though.
Steps to Reproduce:
swift closure.swift
Expected Results: I expect the code to compile and "contains no even numbers" to be printed to the console
Actual Results: the compiler throws multiple errors due to being unable to parse the conditional correctly. Specifically:
Notes:
This conditional works as expected:
This gives a slightly different compilation error as above (
error: missing argument for parameter #​1 in call
):The text was updated successfully, but these errors were encountered: