Skip to content

[SwiftParser] Handle extraneous case keyword after comma in switch case. #2665

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
Jun 4, 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
11 changes: 11 additions & 0 deletions Sources/SwiftParser/Expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2361,17 +2361,28 @@ extension Parser {
do {
var keepGoing: RawTokenSyntax? = nil
var loopProgress = LoopProgressCondition()
var unexpectedPrePatternCase: RawUnexpectedNodesSyntax? = nil
repeat {
let (pattern, whereClause) = self.parseGuardedCasePattern()
keepGoing = self.consume(if: .comma)
caseItems.append(
RawSwitchCaseItemSyntax(
unexpectedPrePatternCase,
pattern: pattern,
whereClause: whereClause,
trailingComma: keepGoing,
arena: self.arena
)
)

if keepGoing != nil, let caseToken = self.consume(if: .keyword(.case)) {
unexpectedPrePatternCase = RawUnexpectedNodesSyntax(
elements: [RawSyntax(caseToken)],
arena: self.arena
)
} else {
unexpectedPrePatternCase = nil
}
} while keepGoing != nil && self.hasProgressed(&loopProgress)
}
let (unexpectedBeforeColon, colon) = self.expect(.colon)
Expand Down
13 changes: 13 additions & 0 deletions Tests/SwiftParserTest/translated/SwitchTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1351,4 +1351,17 @@ final class SwitchTests: ParserTestCase {
"""
)
}

func testSwitch84() {
assertParse(
"""
switch x {
case 1, 1️⃣case 2, 3:
}
""",
diagnostics: [
DiagnosticSpec(message: "unexpected 'case' keyword in switch case")
]
)
}
}