Skip to content

Commit 6ff8de4

Browse files
committed
Merge pull request swiftlang#1580 from kimdv/kimdv/fix-wrong-diagnostic-for-wrong-pattern
1 parent ebe3885 commit 6ff8de4

File tree

3 files changed

+51
-12
lines changed

3 files changed

+51
-12
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 & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -786,20 +786,39 @@ final class RecoveryTests: XCTestCase {
786786
assertParse(
787787
#"""
788788
struct SS 1️⃣SS : Multi {
789-
private var a 2️⃣b 3️⃣: Int = ""
789+
private var a 2️⃣b : Int = ""
790790
func f() {
791-
var c 4️⃣d = 5
791+
var c 3️⃣d = 5
792792
let _ = 0
793793
}
794794
}
795795
"""#,
796796
diagnostics: [
797-
DiagnosticSpec(locationMarker: "1️⃣", message: "found an unexpected second identifier in struct"),
798-
DiagnosticSpec(locationMarker: "2️⃣", message: "expected ':' in type annotation"),
799-
DiagnosticSpec(locationMarker: "3️⃣", message: #"unexpected code ': Int = ""' before function"#),
800-
// TODO: (good first issue) Old parser expected error on line 4: found an unexpected second identifier in variable declaration; is there an accidental break?
801-
DiagnosticSpec(locationMarker: "4️⃣", message: "expected ':' in type annotation"),
802-
]
797+
DiagnosticSpec(
798+
locationMarker: "1️⃣",
799+
message: "found an unexpected second identifier in struct; is there an accidental break?",
800+
fixIts: ["join the identifiers together"]
801+
),
802+
DiagnosticSpec(
803+
locationMarker: "2️⃣",
804+
message: "found an unexpected second identifier in pattern; is there an accidental break?",
805+
fixIts: ["join the identifiers together", "join the identifiers together with camel-case"]
806+
),
807+
DiagnosticSpec(
808+
locationMarker: "3️⃣",
809+
message: "expected ':' in type annotation",
810+
fixIts: ["insert ':'"]
811+
),
812+
],
813+
fixedSource: #"""
814+
struct SSSS : Multi {
815+
private var ab : Int = ""
816+
func f() {
817+
var c: d = 5
818+
let _ = 0
819+
}
820+
}
821+
"""#
803822
)
804823
}
805824

@@ -815,6 +834,24 @@ final class RecoveryTests: XCTestCase {
815834
)
816835
}
817836

837+
func testRecovery64c() {
838+
assertParse(
839+
"""
840+
private var a 1️⃣b : Int = ""
841+
""",
842+
diagnostics: [
843+
DiagnosticSpec(
844+
locationMarker: "1️⃣",
845+
message: "found an unexpected second identifier in pattern; is there an accidental break?",
846+
fixIts: ["join the identifiers together", "join the identifiers together with camel-case"]
847+
)
848+
],
849+
fixedSource: """
850+
private var ab : Int = ""
851+
"""
852+
)
853+
}
854+
818855
func testRecovery65() {
819856
assertParse(
820857
"""

0 commit comments

Comments
 (0)