Skip to content

Commit 8ddbb9a

Browse files
authored
Merge pull request #1580 from kimdv/kimdv/fix-wrong-diagnostic-for-wrong-pattern
2 parents f756568 + 655613f commit 8ddbb9a

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

Sources/SwiftParser/Patterns.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,12 @@ extension Parser {
175175
&& !self.currentToken.isAtStartOfLine
176176
&& lookahead.canParseType()
177177
{
178-
// Recovery if the user forgot to add ':'
179-
let result = self.parseResultType()
178+
let (unexpectedBeforeColon, colon) = self.expect(.colon)
179+
let result = self.parseType()
180+
180181
type = RawTypeAnnotationSyntax(
181-
colon: self.missingToken(.colon),
182+
unexpectedBeforeColon,
183+
colon: colon,
182184
type: result,
183185
arena: self.arena
184186
)

Sources/SwiftParserDiagnostics/ParseDiagnosticsGenerator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
331331
} else if node.first?.as(TokenSyntax.self)?.tokenKind.isIdentifier == true,
332332
let previousToken = node.previousToken(viewMode: .sourceAccurate),
333333
previousToken.tokenKind.isIdentifier,
334-
previousToken.parent?.is(DeclSyntax.self) == true
334+
previousToken.parent?.is(DeclSyntax.self) == true || previousToken.parent?.is(IdentifierPatternSyntax.self) == true
335335
{
336336
// If multiple identifiers are used for a declaration name, offer to join them together.
337337
let tokens =

Tests/SwiftParserTest/translated/RecoveryTests.swift

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -813,9 +813,9 @@ final class RecoveryTests: XCTestCase {
813813
assertParse(
814814
#"""
815815
struct SS 1️⃣SS : Multi {
816-
private var a 2️⃣b 3️⃣: Int = ""
816+
private var a 2️⃣b : Int = ""
817817
func f() {
818-
var c 4️⃣d = 5
818+
var c 3️⃣d = 5
819819
let _ = 0
820820
}
821821
}
@@ -828,16 +828,24 @@ final class RecoveryTests: XCTestCase {
828828
),
829829
DiagnosticSpec(
830830
locationMarker: "2️⃣",
831-
message: "expected ':' in type annotation",
832-
fixIts: ["insert ':'"]
831+
message: "found an unexpected second identifier in pattern; is there an accidental break?",
832+
fixIts: ["join the identifiers together", "join the identifiers together with camel-case"]
833833
),
834-
DiagnosticSpec(locationMarker: "3️⃣", message: #"unexpected code ': Int = ""' before function"#),
835834
DiagnosticSpec(
836-
locationMarker: "4️⃣",
835+
locationMarker: "3️⃣",
837836
message: "expected ':' in type annotation",
838837
fixIts: ["insert ':'"]
839838
),
840-
]
839+
],
840+
fixedSource: #"""
841+
struct SSSS : Multi {
842+
private var ab : Int = ""
843+
func f() {
844+
var c: d = 5
845+
let _ = 0
846+
}
847+
}
848+
"""#
841849
)
842850
}
843851

@@ -869,6 +877,24 @@ final class RecoveryTests: XCTestCase {
869877
)
870878
}
871879

880+
func testRecovery64c() {
881+
assertParse(
882+
"""
883+
private var a 1️⃣b : Int = ""
884+
""",
885+
diagnostics: [
886+
DiagnosticSpec(
887+
locationMarker: "1️⃣",
888+
message: "found an unexpected second identifier in pattern; is there an accidental break?",
889+
fixIts: ["join the identifiers together", "join the identifiers together with camel-case"]
890+
)
891+
],
892+
fixedSource: """
893+
private var ab : Int = ""
894+
"""
895+
)
896+
}
897+
872898
func testRecovery65() {
873899
assertParse(
874900
"""

0 commit comments

Comments
 (0)