Skip to content

Commit cf38c18

Browse files
committed
Add diagnostic for comparison in default value
1 parent 74c544e commit cf38c18

File tree

5 files changed

+40
-7
lines changed

5 files changed

+40
-7
lines changed

Sources/SwiftParser/Declarations.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1229,7 +1229,7 @@ extension Parser {
12291229
}
12301230

12311231
let defaultArgument: RawInitializerClauseSyntax?
1232-
if self.at(.equal) {
1232+
if self.at(.equal) || self.atContextualPunctuator("==") {
12331233
defaultArgument = self.parseDefaultArgument()
12341234
} else {
12351235
defaultArgument = nil

Sources/SwiftParser/Expressions.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1671,7 +1671,18 @@ extension Parser {
16711671
extension Parser {
16721672
@_spi(RawSyntax)
16731673
public mutating func parseDefaultArgument() -> RawInitializerClauseSyntax {
1674-
let (unexpectedBeforeEq, eq) = self.expect(.equal)
1674+
let unexpectedBeforeEq: RawUnexpectedNodesSyntax?
1675+
let eq: RawTokenSyntax
1676+
if let comparison = self.consumeIfContextualPunctuator("==") {
1677+
unexpectedBeforeEq = RawUnexpectedNodesSyntax(
1678+
elements: [RawSyntax(comparison)],
1679+
arena: self.arena
1680+
)
1681+
eq = missingToken(.equal)
1682+
} else {
1683+
(unexpectedBeforeEq, eq) = self.expect(.equal)
1684+
}
1685+
16751686
let expr = self.parseExpression()
16761687
return RawInitializerClauseSyntax(
16771688
unexpectedBeforeEq,

Sources/SwiftParserDiagnostics/ParseDiagnosticsGenerator.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,23 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
776776
if shouldSkip(node) {
777777
return .skipChildren
778778
}
779+
780+
if let unexpected = node.unexpectedBeforeEqual,
781+
unexpected.first?.as(TokenSyntax.self)?.tokenKind == .binaryOperator("==")
782+
{
783+
addDiagnostic(
784+
unexpected,
785+
.expectedAssignmentInsteadOfComparisonOperator,
786+
fixIts: [
787+
FixIt(
788+
message: ReplaceTokensFixIt(replaceTokens: [.binaryOperator("==")], replacement: node.equal),
789+
changes: [.makeMissing(unexpected), .makePresent(node.equal, leadingTrivia: [])]
790+
)
791+
],
792+
handledNodes: [unexpected.id, node.equal.id]
793+
)
794+
}
795+
779796
if node.equal.presence == .missing {
780797
exchangeTokens(
781798
unexpected: node.unexpectedBeforeEqual,

Sources/SwiftParserDiagnostics/ParserDiagnosticMessages.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ extension DiagnosticMessage where Self == StaticParserError {
128128
public static var expectedExpressionAfterTry: Self {
129129
.init("expected expression after 'try'")
130130
}
131+
public static var expectedAssignmentInsteadOfComparisonOperator: Self {
132+
.init("expected '=' instead of '==' to assign default value for parameter")
133+
}
131134
public static var expectedLeftBraceOrIfAfterElse: Self {
132135
.init("expected '{' or 'if' after 'else'")
133136
}

Tests/SwiftParserTest/translated/RecoveryTests.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2139,16 +2139,18 @@ final class RecoveryTests: XCTestCase {
21392139
}
21402140

21412141
func testRecovery176() {
2142+
// rdar://problem/22478168
2143+
// https://github.com/apple/swift/issues/53396
21422144
assertParse(
21432145
"""
2144-
// rdar://problem/22478168
2145-
// https://github.com/apple/swift/issues/53396
21462146
func f_53396(a: Int 1️⃣== 0) {}
21472147
""",
21482148
diagnostics: [
2149-
// TODO: Old parser expected error on line 3: expected '=' instead of '==' to assign default value for parameter, Fix-It replacements: 21 - 23 = '='
2150-
DiagnosticSpec(message: "unexpected code '== 0' in parameter clause")
2151-
]
2149+
DiagnosticSpec(message: "expected '=' instead of '==' to assign default value for parameter", fixIts: ["replace '==' with '='"])
2150+
],
2151+
fixedSource: """
2152+
func f_53396(a: Int = 0) {}
2153+
"""
21522154
)
21532155
}
21542156

0 commit comments

Comments
 (0)