Skip to content

Commit db9f1e5

Browse files
authored
Merge pull request #3081 from calda/cal--6.2-trailing-closure-fixes
[6.2] Add trailing comma support in cases missing from Swift 6.1
2 parents a0400e0 + 85c1d4b commit db9f1e5

File tree

5 files changed

+97
-1
lines changed

5 files changed

+97
-1
lines changed

Sources/SwiftParser/Nominals.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,11 @@ extension Parser {
361361
arena: self.arena
362362
)
363363
)
364+
365+
// If this was a trailing comma, there are no more elements
366+
if at(prefix: ">") {
367+
break
368+
}
364369
} while keepGoing != nil && self.hasProgressed(&loopProgress)
365370
}
366371
let rangle = self.expectWithoutRecovery(prefix: ">", as: .rightAngle)

Sources/SwiftParser/Types.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,11 @@ extension Parser {
430430
arena: self.arena
431431
)
432432
)
433+
434+
// If this was a trailing comma, we're done parsing the list
435+
if self.at(prefix: ">") {
436+
break
437+
}
433438
} while keepGoing != nil && self.hasProgressed(&loopProgress)
434439
}
435440

@@ -1052,7 +1057,8 @@ extension Parser.Lookahead {
10521057
return false
10531058
}
10541059
// Parse the comma, if the list continues.
1055-
} while self.consume(if: .comma) != nil && self.hasProgressed(&loopProgress)
1060+
// This could be the trailing comma.
1061+
} while self.consume(if: .comma) != nil && !self.at(prefix: ">") && self.hasProgressed(&loopProgress)
10561062
}
10571063

10581064
guard self.consume(ifPrefix: ">", as: .rightAngle) != nil else {

Tests/SwiftParserTest/DeclarationTests.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3529,4 +3529,31 @@ final class DeclarationTests: ParserTestCase {
35293529
]
35303530
)
35313531
}
3532+
3533+
func testTrailingCommas() {
3534+
assertParse(
3535+
"""
3536+
protocol Baaz<
3537+
Foo,
3538+
Bar,
3539+
> {
3540+
associatedtype Foo
3541+
associatedtype Bar
3542+
}
3543+
"""
3544+
)
3545+
3546+
assertParse(
3547+
"""
3548+
struct Foo<
3549+
T1,
3550+
T2,
3551+
T3,
3552+
>: Baaz<
3553+
T1,
3554+
T2,
3555+
> {}
3556+
"""
3557+
)
3558+
}
35323559
}

Tests/SwiftParserTest/ExpressionTests.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2125,6 +2125,26 @@ final class ExpressionTests: ParserTestCase {
21252125
"""
21262126
)
21272127
}
2128+
2129+
func testTrailingCommasInTypeExpressions() {
2130+
assertParse(
2131+
"""
2132+
let _ = Foo2<Int, Bool, String,>.self
2133+
"""
2134+
)
2135+
2136+
assertParse(
2137+
"""
2138+
let _ = Foo2<Int, Bool, String,>()
2139+
"""
2140+
)
2141+
2142+
assertParse(
2143+
"""
2144+
let _ = ((Int, Bool, String,) -> Void).self
2145+
"""
2146+
)
2147+
}
21282148
}
21292149

21302150
final class MemberExprTests: ParserTestCase {

Tests/SwiftParserTest/TypeTests.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,44 @@ final class TypeTests: ParserTestCase {
734734
fixedSource: "func foo(test: nonisolated(nonsendinghello) () async -> Void)"
735735
)
736736
}
737+
738+
func testTrailingCommas() {
739+
assertParse(
740+
"""
741+
let foo: (
742+
bar: String,
743+
quux: String,
744+
)
745+
"""
746+
)
747+
748+
assertParse(
749+
"""
750+
let closure: (
751+
String,
752+
String,
753+
) -> (
754+
bar: String,
755+
quux: String,
756+
)
757+
"""
758+
)
759+
760+
assertParse(
761+
"""
762+
struct Foo<T1, T2, T3,> {}
763+
764+
typealias Bar<
765+
T1,
766+
T2,
767+
> = Foo<
768+
T1,
769+
T2,
770+
Bool,
771+
>
772+
"""
773+
)
774+
}
737775
}
738776

739777
final class InlineArrayTypeTests: ParserTestCase {

0 commit comments

Comments
 (0)