Skip to content

Commit 72dbbaf

Browse files
committed
Fix diagnostic for wrong where in if statement
1 parent 7f1f40f commit 72dbbaf

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

Sources/SwiftParser/Statements.swift

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

Sources/SwiftParserDiagnostics/ParseDiagnosticsGenerator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
538538
if let trailingComma = node.trailingComma {
539539
exchangeTokens(
540540
unexpected: node.unexpectedBetweenConditionAndTrailingComma,
541-
unexpectedTokenCondition: { $0.text == "&&" },
541+
unexpectedTokenCondition: { $0.text == "&&" || $0.tokenKind == .keyword(.where) },
542542
correctTokens: [node.trailingComma],
543543
message: { _ in .joinConditionsUsingComma },
544544
moveFixIt: { ReplaceTokensFixIt(replaceTokens: $0, replacements: [trailingComma]) }

Tests/SwiftParserTest/translated/RecoveryTests.swift

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1911,20 +1911,25 @@ final class RecoveryTests: XCTestCase {
19111911
}
19121912

19131913
func testRecovery151() {
1914+
// <rdar://problem/20489838> QoI: Nonsensical error and fixit if "let" is missing between 'if let ... where' clauses
19141915
assertParse(
1915-
#"""
1916-
// <rdar://problem/20489838> QoI: Nonsensical error and fixit if "let" is missing between 'if let ... where' clauses
1916+
"""
19171917
if let y = x 1️⃣where y == 0, let z = x {
19181918
_ = y
19191919
_ = z
1920-
}2️⃣
1921-
"""#,
1920+
}
1921+
""",
19221922
diagnostics: [
19231923
// TODO: Old parser expected error on line 4: expected ',' joining parts of a multi-clause condition, Fix-It replacements: 15 - 21 = ','
1924-
DiagnosticSpec(locationMarker: "1️⃣", message: "expected '{' in 'if' statement", fixIts: ["insert '{'"]),
1925-
DiagnosticSpec(locationMarker: "1️⃣", message: "unexpected code 'where y == 0,' before variable"),
1926-
DiagnosticSpec(locationMarker: "2️⃣", message: "expected '}' to end 'if' statement", fixIts: ["insert '}'"]),
1927-
]
1924+
DiagnosticSpec(message: "expected ',' joining parts of a multi-clause condition",
1925+
fixIts: ["replace 'where' with ','"])
1926+
],
1927+
fixedSource: """
1928+
if let y = x , y == 0, let z = x {
1929+
_ = y
1930+
_ = z
1931+
}
1932+
"""
19281933
)
19291934
}
19301935

0 commit comments

Comments
 (0)