Skip to content

Commit bb59f6c

Browse files
committed
Fix wrong diagnostic parse typed pattern
1 parent a648679 commit bb59f6c

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
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
)

Tests/SwiftParserTest/translated/RecoveryTests.swift

Lines changed: 32 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,23 @@ final class RecoveryTests: XCTestCase {
828828
),
829829
DiagnosticSpec(
830830
locationMarker: "2️⃣",
831-
message: "expected ':' in type annotation",
832-
fixIts: ["insert ':'"]
831+
message: "unexpected code 'b' before type annotation"
833832
),
834-
DiagnosticSpec(locationMarker: "3️⃣", message: #"unexpected code ': Int = ""' before function"#),
835833
DiagnosticSpec(
836-
locationMarker: "4️⃣",
834+
locationMarker: "3️⃣",
837835
message: "expected ':' in type annotation",
838836
fixIts: ["insert ':'"]
839837
),
840-
]
838+
],
839+
fixedSource: #"""
840+
struct SSSS : Multi {
841+
private var a b : Int = ""
842+
func f() {
843+
var c: d = 5
844+
let _ = 0
845+
}
846+
}
847+
"""#
841848
)
842849
}
843850

@@ -869,6 +876,24 @@ final class RecoveryTests: XCTestCase {
869876
)
870877
}
871878

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

0 commit comments

Comments
 (0)