Skip to content

Commit 3ed9dcf

Browse files
authored
Merge pull request #1616 from kimdv/kimdv/fix-diagnostic-for-where-keyword-in-if-statement
Fix diagnostic for wrong `where` in if statement
2 parents d3ce991 + 65d9325 commit 3ed9dcf

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

Sources/SwiftParser/Statements.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ extension Parser {
188188
let condition = self.parseConditionElement(lastBindingKind: elements.last?.condition.as(RawOptionalBindingConditionSyntax.self)?.bindingKeyword)
189189
var unexpectedBeforeKeepGoing: RawUnexpectedNodesSyntax? = nil
190190
keepGoing = self.consume(if: .comma)
191-
if keepGoing == nil, let andOperator = self.consumeIfContextualPunctuator("&&") {
192-
unexpectedBeforeKeepGoing = RawUnexpectedNodesSyntax(combining: unexpectedBeforeKeepGoing, andOperator, arena: self.arena)
191+
if keepGoing == nil, let token = self.consumeIfContextualPunctuator("&&") ?? self.consume(if: .keyword(.where)) {
192+
unexpectedBeforeKeepGoing = RawUnexpectedNodesSyntax(combining: unexpectedBeforeKeepGoing, token, arena: self.arena)
193193
keepGoing = missingToken(.comma)
194194
}
195195
elements.append(

Sources/SwiftParserDiagnostics/ParseDiagnosticsGenerator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
582582
if let trailingComma = node.trailingComma {
583583
exchangeTokens(
584584
unexpected: node.unexpectedBetweenConditionAndTrailingComma,
585-
unexpectedTokenCondition: { $0.text == "&&" },
585+
unexpectedTokenCondition: { $0.text == "&&" || $0.tokenKind == .keyword(.where) },
586586
correctTokens: [node.trailingComma],
587587
message: { _ in .joinConditionsUsingComma },
588588
moveFixIt: { ReplaceTokensFixIt(replaceTokens: $0, replacements: [trailingComma]) }

Tests/SwiftParserTest/translated/RecoveryTests.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2398,25 +2398,25 @@ final class RecoveryTests: XCTestCase {
23982398
func testRecovery151() {
23992399
// <rdar://problem/20489838> QoI: Nonsensical error and fixit if "let" is missing between 'if let ... where' clauses
24002400
assertParse(
2401-
#"""
2401+
"""
24022402
if let y = x 1️⃣where y == 0, let z = x {
24032403
_ = y
24042404
_ = z
2405-
}2️⃣
2406-
"""#,
2405+
}
2406+
""",
24072407
diagnostics: [
2408-
// TODO: Old parser expected error on line 4: expected ',' joining parts of a multi-clause condition, Fix-It replacements: 15 - 21 = ','
2409-
DiagnosticSpec(locationMarker: "1️⃣", message: "expected '{' in 'if' statement", fixIts: ["insert '{'"]),
2410-
DiagnosticSpec(locationMarker: "1️⃣", message: "unexpected code 'where y == 0,' before variable"),
2411-
DiagnosticSpec(locationMarker: "2️⃣", message: "expected '}' to end 'if' statement", fixIts: ["insert '}'"]),
2408+
DiagnosticSpec(
2409+
message: "expected ',' joining parts of a multi-clause condition",
2410+
fixIts: ["replace 'where' with ','"]
2411+
)
24122412
],
2413-
fixedSource: #"""
2414-
if let y = x {where y == 0, let z = x {
2413+
fixedSource: """
2414+
if let y = x, y == 0, let z = x {
24152415
_ = y
24162416
_ = z
24172417
}
2418-
}
2419-
"""#
2418+
"""
2419+
24202420
)
24212421
}
24222422

0 commit comments

Comments
 (0)