Skip to content

[6.2] Add trailing comma support in cases missing from Swift 6.1 #3081

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

Merged
merged 3 commits into from
May 21, 2025
Merged
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
5 changes: 5 additions & 0 deletions Sources/SwiftParser/Nominals.swift
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,11 @@ extension Parser {
arena: self.arena
)
)

// If this was a trailing comma, there are no more elements
if at(prefix: ">") {
break
}
} while keepGoing != nil && self.hasProgressed(&loopProgress)
}
let rangle = self.expectWithoutRecovery(prefix: ">", as: .rightAngle)
Expand Down
8 changes: 7 additions & 1 deletion Sources/SwiftParser/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,11 @@ extension Parser {
arena: self.arena
)
)

// If this was a trailing comma, we're done parsing the list
if self.at(prefix: ">") {
break
}
} while keepGoing != nil && self.hasProgressed(&loopProgress)
}

Expand Down Expand Up @@ -1052,7 +1057,8 @@ extension Parser.Lookahead {
return false
}
// Parse the comma, if the list continues.
} while self.consume(if: .comma) != nil && self.hasProgressed(&loopProgress)
// This could be the trailing comma.
} while self.consume(if: .comma) != nil && !self.at(prefix: ">") && self.hasProgressed(&loopProgress)
}

guard self.consume(ifPrefix: ">", as: .rightAngle) != nil else {
Expand Down
27 changes: 27 additions & 0 deletions Tests/SwiftParserTest/DeclarationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3529,4 +3529,31 @@ final class DeclarationTests: ParserTestCase {
]
)
}

func testTrailingCommas() {
assertParse(
"""
protocol Baaz<
Foo,
Bar,
> {
associatedtype Foo
associatedtype Bar
}
"""
)

assertParse(
"""
struct Foo<
T1,
T2,
T3,
>: Baaz<
T1,
T2,
> {}
"""
)
}
}
20 changes: 20 additions & 0 deletions Tests/SwiftParserTest/ExpressionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2125,6 +2125,26 @@ final class ExpressionTests: ParserTestCase {
"""
)
}

func testTrailingCommasInTypeExpressions() {
assertParse(
"""
let _ = Foo2<Int, Bool, String,>.self
"""
)

assertParse(
"""
let _ = Foo2<Int, Bool, String,>()
"""
)

assertParse(
"""
let _ = ((Int, Bool, String,) -> Void).self
"""
)
}
}

final class MemberExprTests: ParserTestCase {
Expand Down
38 changes: 38 additions & 0 deletions Tests/SwiftParserTest/TypeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,44 @@ final class TypeTests: ParserTestCase {
fixedSource: "func foo(test: nonisolated(nonsendinghello) () async -> Void)"
)
}

func testTrailingCommas() {
assertParse(
"""
let foo: (
bar: String,
quux: String,
)
"""
)

assertParse(
"""
let closure: (
String,
String,
) -> (
bar: String,
quux: String,
)
"""
)

assertParse(
"""
struct Foo<T1, T2, T3,> {}

typealias Bar<
T1,
T2,
> = Foo<
T1,
T2,
Bool,
>
"""
)
}
}

final class InlineArrayTypeTests: ParserTestCase {
Expand Down