Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions Sources/SwiftParser/Expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -932,10 +932,19 @@ extension Parser {
leadingExpr: RawExprSyntax,
flavor: ExprFlavor
) -> RawExprSyntax? {
guard
self.at(.leftBrace) && !leadingExpr.raw.kind.isLiteral
&& self.withLookahead({ $0.atValidTrailingClosure(flavor: flavor) })
else {
guard self.at(.leftBrace) else {
return nil
}

if leadingExpr.raw.kind.isLiteral {
// Trailing closures are allowed after array and dictionary types,
// since this can mean `[Element].init { }` or `[Key: Value].init { }`.
guard leadingExpr.raw.kind == .arrayExpr || leadingExpr.raw.kind == .dictionaryExpr else {
return nil
}
}

guard self.withLookahead({ $0.atValidTrailingClosure(flavor: flavor) }) else {
return nil
}

Expand Down
92 changes: 76 additions & 16 deletions Tests/SwiftParserTest/ExpressionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,80 @@ final class ExpressionTests: ParserTestCase {
assertParse("compactMap { (parserDiag) in }")
}

func testCollectionTrailingClosureInits() {
assertParse(
"let value = 1️⃣[Foo] { bar }",
substructure: FunctionCallExprSyntax(
calledExpression: ArrayExprSyntax(
leftSquare: .leftSquareToken(),
elements: ArrayElementListSyntax([
ArrayElementSyntax(
expression: DeclReferenceExprSyntax(baseName: .identifier("Foo")),
trailingComma: nil
)
]),
rightSquare: .rightSquareToken()
),
leftParen: nil,
arguments: LabeledExprListSyntax([]),
rightParen: nil,
trailingClosure: ClosureExprSyntax(
leftBrace: .leftBraceToken(),
signature: nil,
statements: CodeBlockItemListSyntax([
CodeBlockItemSyntax(
item: .expr(
ExprSyntax(
DeclReferenceExprSyntax(baseName: .identifier("bar"))
)
)
)
]),
rightBrace: .rightBraceToken()
)
),
substructureAfterMarker: "1️⃣"
)

assertParse(
"let value = 1️⃣[String: Int] { value }",
substructure: FunctionCallExprSyntax(
calledExpression: DictionaryExprSyntax(
leftSquare: .leftSquareToken(),
content: .elements(
DictionaryElementListSyntax([
DictionaryElementSyntax(
key: DeclReferenceExprSyntax(baseName: .identifier("String")),
colon: .colonToken(),
value: DeclReferenceExprSyntax(baseName: .identifier("Int")),
trailingComma: nil
)
])
),
rightSquare: .rightSquareToken()
),
leftParen: nil,
arguments: LabeledExprListSyntax([]),
rightParen: nil,
trailingClosure: ClosureExprSyntax(
leftBrace: .leftBraceToken(),
signature: nil,
statements: CodeBlockItemListSyntax([
CodeBlockItemSyntax(
item: .expr(
ExprSyntax(
DeclReferenceExprSyntax(baseName: .identifier("value"))
)
)
)
]),
rightBrace: .rightBraceToken()
)
),
substructureAfterMarker: "1️⃣"
)
}

func testSequenceExpressions() {
assertParse("await a()")
assertParse(
Expand Down Expand Up @@ -2180,22 +2254,8 @@ final class ExpressionTests: ParserTestCase {
{ return /foo/ }
"""
)
assertParse(
"_ = [1]1️⃣ { return [1] }",
diagnostics: expectedDiagnostics,
fixedSource: """
_ = [1]
{ return [1] }
"""
)
assertParse(
"_ = [1: 1]1️⃣ { return [1: 1] }",
diagnostics: expectedDiagnostics,
fixedSource: """
_ = [1: 1]
{ return [1: 1] }
"""
)
assertParse("_ = [1] { return [1] }")
assertParse("_ = [1: 1] { return [1: 1] }")
Comment on lines +2257 to +2258
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These now parse, but fail type checking in the full compiler:

  _ = [1] { return [1] } // expected-error {{cannot call value of non-function type '[Int]'}}
  _ = [1: 1] { return [1: 1] } // expected-error {{cannot call value of non-function type '[Int : Int]'}}

  _ = [1] // expected-error {{cannot call value of non-function type '[Int]'}}
  { return [1] }

  _ = [1: 1] // expected-error {{cannot call value of non-function type '[Int : Int]'}}
  { return [1: 1] }


assertParse(
"_ = 1 + 11️⃣ { return 1 }",
Expand Down