Skip to content

Fix diagnostic for wrong where in if statement #1616

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
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
4 changes: 2 additions & 2 deletions Sources/SwiftParser/Statements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ extension Parser {
let condition = self.parseConditionElement(lastBindingKind: elements.last?.condition.as(RawOptionalBindingConditionSyntax.self)?.bindingKeyword)
var unexpectedBeforeKeepGoing: RawUnexpectedNodesSyntax? = nil
keepGoing = self.consume(if: .comma)
if keepGoing == nil, let andOperator = self.consumeIfContextualPunctuator("&&") {
unexpectedBeforeKeepGoing = RawUnexpectedNodesSyntax(combining: unexpectedBeforeKeepGoing, andOperator, arena: self.arena)
if keepGoing == nil, let token = self.consumeIfContextualPunctuator("&&") ?? self.consume(if: .keyword(.where)) {
unexpectedBeforeKeepGoing = RawUnexpectedNodesSyntax(combining: unexpectedBeforeKeepGoing, token, arena: self.arena)
keepGoing = missingToken(.comma)
}
elements.append(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
if let trailingComma = node.trailingComma {
exchangeTokens(
unexpected: node.unexpectedBetweenConditionAndTrailingComma,
unexpectedTokenCondition: { $0.text == "&&" },
unexpectedTokenCondition: { $0.text == "&&" || $0.tokenKind == .keyword(.where) },
correctTokens: [node.trailingComma],
message: { _ in .joinConditionsUsingComma },
moveFixIt: { ReplaceTokensFixIt(replaceTokens: $0, replacements: [trailingComma]) }
Expand Down
22 changes: 11 additions & 11 deletions Tests/SwiftParserTest/translated/RecoveryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2394,25 +2394,25 @@ final class RecoveryTests: XCTestCase {
func testRecovery151() {
// <rdar://problem/20489838> QoI: Nonsensical error and fixit if "let" is missing between 'if let ... where' clauses
assertParse(
#"""
"""
if let y = x 1️⃣where y == 0, let z = x {
_ = y
_ = z
}2️⃣
"""#,
}
""",
diagnostics: [
// TODO: Old parser expected error on line 4: expected ',' joining parts of a multi-clause condition, Fix-It replacements: 15 - 21 = ','
DiagnosticSpec(locationMarker: "1️⃣", message: "expected '{' in 'if' statement", fixIts: ["insert '{'"]),
DiagnosticSpec(locationMarker: "1️⃣", message: "unexpected code 'where y == 0,' before variable"),
DiagnosticSpec(locationMarker: "2️⃣", message: "expected '}' to end 'if' statement", fixIts: ["insert '}'"]),
DiagnosticSpec(
message: "expected ',' joining parts of a multi-clause condition",
fixIts: ["replace 'where' with ','"]
)
],
fixedSource: #"""
if let y = x {where y == 0, let z = x {
fixedSource: """
if let y = x, y == 0, let z = x {
_ = y
_ = z
}
}
"""#
"""

)
}

Expand Down