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-15727] swift-format thinks parens used to disambiguate closures in if conditions are not necessary turning compiling code into not compiling code #298

Closed
weissi opened this issue Jan 13, 2022 · 2 comments
Labels
bug Something isn't working swift-format

Comments

@weissi
Copy link

weissi commented Jan 13, 2022

Previous ID SR-15727
Radar rdar://problem/87546580
Original Reporter @weissi
Type Bug
Environment

all swift-formats incl 5.6 and main

Additional Detail from JIRA
Votes 1
Component/s swift-format
Labels Bug
Assignee None
Priority Medium

md5: 3eeb9bd3515a4c45827dcf5f69f24625

Issue Description:

Description

The following Swift program compiles & works just fine

if ({_isDebugAssertConfiguration()}()) {print("debug")}

please note that the parens around {{ {_isDebugAssertConfiguration() {} }() }} are NOT optional.

When formatted with swift-format however, it gets turned into this program

if { _isDebugAssertConfiguration() }() { print("debug") }

which doesn't actually compile... The problem is that now, the curly braces for the closure / the if body are ambiguous to the parser...

Details

Original program works:

$ echo 'if ({_isDebugAssertConfiguration()}()) {print("debug")}' > /tmp/t.swift
$ swift /tmp/t.swift
debug

after formatting however:

$ ./.build/debug/swift-format /tmp/t.swift > /tmp/t1.swift
$ cat /tmp/t1.swift
if { _isDebugAssertConfiguration() }() { print("debug") }

it doesn't compile anymore

$ swift /tmp/t1.swift
/tmp/t1.swift:1:1: error: missing condition in an 'if' statement
if { _isDebugAssertConfiguration() }() { print("debug") }
^~~~
/tmp/t1.swift:1:37: error: consecutive statements on a line must be separated by ';'
if { _isDebugAssertConfiguration() }() { print("debug") }
                  ^
                  ;
/tmp/t1.swift:1:39: error: consecutive statements on a line must be separated by ';'
if { _isDebugAssertConfiguration() }() { print("debug") }
                   ^
                   ;
/tmp/t1.swift:1:40: error: top-level statement cannot begin with a closure expression
if { _isDebugAssertConfiguration() }() { print("debug") }
                    ^
/tmp/t1.swift:1:6: warning: result of call to '_isDebugAssertConfiguration()' is unused
if { _isDebugAssertConfiguration() }() { print("debug") }
   ^             ~~
/tmp/t1.swift:1:40: error: closure expression is unused
if { _isDebugAssertConfiguration() }() { print("debug") }
                    ^
/tmp/t1.swift:1:40: note: did you mean to use a 'do' statement?
if { _isDebugAssertConfiguration() }() { print("debug") }
                    ^
                    do
@weissi
Copy link
Author

weissi commented Jan 13, 2022

CC @allevato

@weissi
Copy link
Author

weissi commented Jan 13, 2022

@swift-ci create

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from swiftlang/swift May 9, 2022
stackotter added a commit to stackotter/swift-format that referenced this issue Apr 4, 2023
…n conditions (swiftlang#298)

A bit of a mouthfull to fit into a reasonably sized commit message. The
NoParensAroundConditions rule removed parens around immediately called closures
and in doing so introduced ambiguities into code that was correct to begin with.
allevato added a commit to allevato/swift-format that referenced this issue Jun 29, 2023
Avoid removing certain disambiguating parens from conditions (fixes swiftlang#298)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working swift-format
Projects
None yet
Development

No branches or pull requests

1 participant