Skip to content

Fix parsing generic argument lists of variadic types in expression context #1114

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 1 commit into from
Dec 7, 2022
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
42 changes: 24 additions & 18 deletions Sources/SwiftParser/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -618,22 +618,26 @@ extension Parser.Lookahead {
break
}

guard self.isAtFunctionTypeArrow() else {
return true
}
if self.isAtFunctionTypeArrow() {
// Handle type-function if we have an '->' with optional
// 'async' and/or 'throws'.
var loopProgress = LoopProgressCondition()
while let (_, handle) = self.at(anyIn: EffectsSpecifier.self), loopProgress.evaluate(currentToken) {
self.eat(handle)
}

// Handle type-function if we have an '->' with optional
// 'async' and/or 'throws'.
var loopProgress = LoopProgressCondition()
while let (_, handle) = self.at(anyIn: EffectsSpecifier.self), loopProgress.evaluate(currentToken) {
self.eat(handle)
guard self.consume(if: .arrow) != nil else {
return false
}

return self.canParseType()
}

guard self.consume(if: .arrow) != nil else {
return false
if self.currentToken.isEllipsis {
self.consumeAnyToken()
}

return self.canParseType()
return true
}

mutating func canParseTupleBodyType() -> Bool {
Expand Down Expand Up @@ -822,14 +826,16 @@ extension Parser.Lookahead {
}

self.consumePrefix("<", as: .leftAngle)
var loopProgress = LoopProgressCondition()
repeat {
guard self.canParseType() else {
return false
}
// Parse the comma, if the list continues.
} while self.consume(if: .comma) != nil && loopProgress.evaluate(currentToken)

if !self.currentToken.starts(with: ">") {
var loopProgress = LoopProgressCondition()
repeat {
guard self.canParseType() else {
return false
}
// Parse the comma, if the list continues.
} while self.consume(if: .comma) != nil && loopProgress.evaluate(currentToken)
}

guard self.currentToken.starts(with: ">") else {
return false
Expand Down
12 changes: 12 additions & 0 deletions Tests/SwiftParserTest/TypeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,18 @@ final class TypeParameterPackTests: XCTestCase {
func f1<T ...>(_ x: T ...) -> (T ...) {}
""")
}
func testVariadicTypes() {
AssertParse(
"""
let _: G< > = G()
let _: G<T... > = G()
let _: G<Int, T... > = G()
let _ = G< >.self
let _ = G<T... >.self
let _ = G<Int, T... >.self
""")

}

func testMissingCommaInType() throws {
AssertParse(
Expand Down