Skip to content

[SR-15727] swift-format thinks parens used to disambiguate closures in if conditions are not necessary turning compiling code into not compiling code #298

@weissi

Description

@weissi
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions