Skip to content

Fix up a mismatch between effect specifier lookahead and parsing #2660

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
May 21, 2024
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
7 changes: 6 additions & 1 deletion Sources/SwiftParser/Expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2428,10 +2428,15 @@ extension Parser.Lookahead {
// Consume 'async', 'throws', and 'rethrows', but in any order.
mutating func consumeEffectsSpecifiers() {
var loopProgress = LoopProgressCondition()
while let (_, handle) = self.at(anyIn: EffectSpecifier.self),
while let (spec, handle) = self.at(anyIn: EffectSpecifier.self),
self.hasProgressed(&loopProgress)
{
self.eat(handle)

if spec.isThrowsSpecifier, self.consume(if: .leftParen) != nil {
_ = self.canParseSimpleOrCompositionType()
self.consume(if: .rightParen)
}
}
}

Expand Down
45 changes: 6 additions & 39 deletions Sources/SwiftParser/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ extension Parser {
mutating func parseTypeScalar(misplacedSpecifiers: [RawTokenSyntax] = []) -> RawTypeSyntax {
let specifiersAndAttributes = self.parseTypeAttributeList(misplacedSpecifiers: misplacedSpecifiers)
var base = RawTypeSyntax(self.parseSimpleOrCompositionType())
if self.withLookahead({ $0.atFunctionTypeArrow() }) {
if self.withLookahead({ $0.canParseFunctionTypeArrow() }) {
var effectSpecifiers = self.parseTypeEffectSpecifiers()
let returnClause = self.parseFunctionReturnClause(
effectSpecifiers: &effectSpecifiers,
Expand Down Expand Up @@ -656,21 +656,9 @@ extension Parser.Lookahead {
return false
}

if self.atFunctionTypeArrow() {
// Handle type-function if we have an '->' with optional
// 'async' and/or 'throws'.
var loopProgress = LoopProgressCondition()
while let (_, handle) = self.at(anyIn: EffectSpecifier.self), self.hasProgressed(&loopProgress) {
self.eat(handle)
}

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

if self.canParseFunctionTypeArrow() {
return self.canParseType()
}

return true
}

Expand Down Expand Up @@ -819,33 +807,12 @@ extension Parser.Lookahead {
return self.consume(if: .rightParen) != nil
}

mutating func atFunctionTypeArrow() -> Bool {
if self.at(.arrow) {
mutating func canParseFunctionTypeArrow() -> Bool {
if self.consume(if: .arrow) != nil {
return true
}

if let effect = self.at(anyIn: EffectSpecifier.self) {
if self.peek().rawTokenKind == .arrow {
return true
}

if effect.spec.isThrowsSpecifier && self.peek().rawTokenKind == .leftParen {
var lookahead = self.lookahead()
lookahead.consumeAnyToken()
lookahead.skipSingle()
return lookahead.atFunctionTypeArrow()
}

if peek(isAtAnyIn: EffectSpecifier.self) != nil {
var lookahead = self.lookahead()
lookahead.consumeAnyToken()
return lookahead.atFunctionTypeArrow()
}

return false
}

return false
self.consumeEffectsSpecifiers()
return self.consume(if: .arrow) != nil
}

mutating func canParseTypeIdentifier(allowKeyword: Bool = false) -> Bool {
Expand Down
19 changes: 19 additions & 0 deletions Tests/SwiftParserTest/ExpressionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2972,6 +2972,9 @@ final class StatementExpressionTests: ParserTestCase {
assertParse(
"[() throws(MyError) -> Void]()"
)
assertParse(
"[() throws(any Error) -> Void]()"
)
assertParse(
"X<() throws(MyError) -> Int>()"
)
Expand All @@ -2980,6 +2983,22 @@ final class StatementExpressionTests: ParserTestCase {
)
}

func testTypedThrowsClosureParam() {
assertParse(
"""
try foo { (a, b) throws(S) in 1 }
"""
)
}

func testTypedThrowsShorthandClosureParams() {
assertParse(
"""
try foo { a, b throws(S) in 1 }
"""
)
}

func testArrayExprWithNoCommas() {
assertParse("[() ()]")

Expand Down