swiftlang/swift-syntax

Parsing of closure parameter list without parameter name fails

Closed this issue · 3 comments

Issue Kind

Parse of Valid Source Failed

Source Code

func test(_: ([Int]) -> Void) {}
test { ([Int]) in }

Description

The above code is valid in the old parser but not in SwiftParser.

Tracked in Apple’s issue tracker as rdar://118012839

Also e.g. ([X:Y]), ((X) -> Y), and (any X).

More generally, the old parser accepts (warns about rather than rejects) this kind of closure parameter production when all of the following are true:

  • An argument name cannot be parsed (link)
  • A type can be parsed per Parser::canParseType(), and said type is followed by ,, ), or = (link)
  • Parsed type per Parser::parseType is neither OptionalTypeRepr nor ImplicitlyUnwrappedOptionalTypeRepr (link)

Ideally, I think a Swift parser should accept and recover from this so long as the type is unambiguous.

Omitting the parameter’s name has never been an officially accepted language feature and the Swift grammar also requires a parameter name (https://docs.swift.org/swift-book/documentation/the-swift-programming-language/expressions#Capture-Lists).

The warning in the C++ parser should be an error, which I’m changing in swiftlang/swift#70065. So, the new parser behaves correctly. Closing this issue.