Skip to content

Commit 65d9325

Browse files
committed
Fix diagnostic for wrong where in if statement
1 parent 5eb8f82 commit 65d9325

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
@@ -2394,25 +2394,25 @@ final class RecoveryTests: XCTestCase {
23942394
func testRecovery151() {
23952395
// <rdar://problem/20489838> QoI: Nonsensical error and fixit if "let" is missing between 'if let ... where' clauses
23962396
assertParse(
2397-
#"""
2397+
"""
23982398
if let y = x 1️⃣where y == 0, let z = x {
23992399
_ = y
24002400
_ = z
2401-
}2️⃣
2402-
"""#,
2401+
}
2402+
""",
24032403
diagnostics: [
2404-
// TODO: Old parser expected error on line 4: expected ',' joining parts of a multi-clause condition, Fix-It replacements: 15 - 21 = ','
2405-
DiagnosticSpec(locationMarker: "1️⃣", message: "expected '{' in 'if' statement", fixIts: ["insert '{'"]),
2406-
DiagnosticSpec(locationMarker: "1️⃣", message: "unexpected code 'where y == 0,' before variable"),
2407-
DiagnosticSpec(locationMarker: "2️⃣", message: "expected '}' to end 'if' statement", fixIts: ["insert '}'"]),
2404+
DiagnosticSpec(
2405+
message: "expected ',' joining parts of a multi-clause condition",
2406+
fixIts: ["replace 'where' with ','"]
2407+
)
24082408
],
2409-
fixedSource: #"""
2410-
if let y = x {where y == 0, let z = x {
2409+
fixedSource: """
2410+
if let y = x, y == 0, let z = x {
24112411
_ = y
24122412
_ = z
24132413
}
2414-
}
2415-
"""#
2414+
"""
2415+
24162416
)
24172417
}
24182418

0 commit comments

Comments
 (0)