Skip to content

Commit 77e4aa4

Browse files
authored
Merge pull request #1687 from kimdv/kimdv/cherry-pick-fix-wrong-diagnostic-for-wrong-pattern
2 parents 83c2be9 + 0c2a8e7 commit 77e4aa4

File tree

3 files changed

+51
-11
lines changed

3 files changed

+51
-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
@@ -315,7 +315,7 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
315315
} else if node.first?.as(TokenSyntax.self)?.tokenKind.isIdentifier == true,
316316
let previousToken = node.previousToken(viewMode: .sourceAccurate),
317317
previousToken.tokenKind.isIdentifier,
318-
previousToken.parent?.is(DeclSyntax.self) == true
318+
previousToken.parent?.is(DeclSyntax.self) == true || previousToken.parent?.is(IdentifierPatternSyntax.self) == true
319319
{
320320
// If multiple identifiers are used for a declaration name, offer to join them together.
321321
let tokens =

Tests/SwiftParserTest/translated/RecoveryTests.swift

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -806,19 +806,39 @@ final class RecoveryTests: XCTestCase {
806806
assertParse(
807807
#"""
808808
struct SS 1️⃣SS : Multi {
809-
private var a 2️⃣b 3️⃣: Int = ""
809+
private var a 2️⃣b : Int = ""
810810
func f() {
811-
var c 4️⃣d = 5
811+
var c 3️⃣d = 5
812812
let _ = 0
813813
}
814814
}
815815
"""#,
816816
diagnostics: [
817-
DiagnosticSpec(locationMarker: "1️⃣", message: "found an unexpected second identifier in struct; is there an accidental break?", fixIts: ["join the identifiers together"]),
818-
DiagnosticSpec(locationMarker: "2️⃣", message: "expected ':' in type annotation"),
819-
DiagnosticSpec(locationMarker: "3️⃣", message: #"unexpected code ': Int = ""' before function"#),
820-
DiagnosticSpec(locationMarker: "4️⃣", message: "expected ':' in type annotation"),
821-
]
817+
DiagnosticSpec(
818+
locationMarker: "1️⃣",
819+
message: "found an unexpected second identifier in struct; is there an accidental break?",
820+
fixIts: ["join the identifiers together"]
821+
),
822+
DiagnosticSpec(
823+
locationMarker: "2️⃣",
824+
message: "found an unexpected second identifier in pattern; is there an accidental break?",
825+
fixIts: ["join the identifiers together", "join the identifiers together with camel-case"]
826+
),
827+
DiagnosticSpec(
828+
locationMarker: "3️⃣",
829+
message: "expected ':' in type annotation",
830+
fixIts: ["insert ':'"]
831+
),
832+
],
833+
fixedSource: #"""
834+
struct SSSS : Multi {
835+
private var ab : Int = ""
836+
func f() {
837+
var c: d = 5
838+
let _ = 0
839+
}
840+
}
841+
"""#
822842
)
823843
}
824844

@@ -850,6 +870,24 @@ final class RecoveryTests: XCTestCase {
850870
)
851871
}
852872

873+
func testRecovery64c() {
874+
assertParse(
875+
"""
876+
private var a 1️⃣b : Int = ""
877+
""",
878+
diagnostics: [
879+
DiagnosticSpec(
880+
locationMarker: "1️⃣",
881+
message: "found an unexpected second identifier in pattern; is there an accidental break?",
882+
fixIts: ["join the identifiers together", "join the identifiers together with camel-case"]
883+
)
884+
],
885+
fixedSource: """
886+
private var ab : Int = ""
887+
"""
888+
)
889+
}
890+
853891
func testRecovery65() {
854892
assertParse(
855893
"""

0 commit comments

Comments
 (0)