Skip to content

[5.9] fix wrong diagnostic for wrong pattern #1687

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions Sources/SwiftParser/Patterns.swift
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,12 @@ extension Parser {
&& !self.currentToken.isAtStartOfLine
&& lookahead.canParseType()
{
// Recovery if the user forgot to add ':'
let result = self.parseResultType()
let (unexpectedBeforeColon, colon) = self.expect(.colon)
let result = self.parseType()

type = RawTypeAnnotationSyntax(
colon: self.missingToken(.colon),
unexpectedBeforeColon,
colon: colon,
type: result,
arena: self.arena
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
} else if node.first?.as(TokenSyntax.self)?.tokenKind.isIdentifier == true,
let previousToken = node.previousToken(viewMode: .sourceAccurate),
previousToken.tokenKind.isIdentifier,
previousToken.parent?.is(DeclSyntax.self) == true
previousToken.parent?.is(DeclSyntax.self) == true || previousToken.parent?.is(IdentifierPatternSyntax.self) == true
{
// If multiple identifiers are used for a declaration name, offer to join them together.
let tokens =
Expand Down
52 changes: 45 additions & 7 deletions Tests/SwiftParserTest/translated/RecoveryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -806,19 +806,39 @@ final class RecoveryTests: XCTestCase {
assertParse(
#"""
struct SS 1️⃣SS : Multi {
private var a 2️⃣b 3️⃣: Int = ""
private var a 2️⃣b : Int = ""
func f() {
var c 4️⃣d = 5
var c 3️⃣d = 5
let _ = 0
}
}
"""#,
diagnostics: [
DiagnosticSpec(locationMarker: "1️⃣", message: "found an unexpected second identifier in struct; is there an accidental break?", fixIts: ["join the identifiers together"]),
DiagnosticSpec(locationMarker: "2️⃣", message: "expected ':' in type annotation"),
DiagnosticSpec(locationMarker: "3️⃣", message: #"unexpected code ': Int = ""' before function"#),
DiagnosticSpec(locationMarker: "4️⃣", message: "expected ':' in type annotation"),
]
DiagnosticSpec(
locationMarker: "1️⃣",
message: "found an unexpected second identifier in struct; is there an accidental break?",
fixIts: ["join the identifiers together"]
),
DiagnosticSpec(
locationMarker: "2️⃣",
message: "found an unexpected second identifier in pattern; is there an accidental break?",
fixIts: ["join the identifiers together", "join the identifiers together with camel-case"]
),
DiagnosticSpec(
locationMarker: "3️⃣",
message: "expected ':' in type annotation",
fixIts: ["insert ':'"]
),
],
fixedSource: #"""
struct SSSS : Multi {
private var ab : Int = ""
func f() {
var c: d = 5
let _ = 0
}
}
"""#
)
}

Expand Down Expand Up @@ -850,6 +870,24 @@ final class RecoveryTests: XCTestCase {
)
}

func testRecovery64c() {
assertParse(
"""
private var a 1️⃣b : Int = ""
""",
diagnostics: [
DiagnosticSpec(
locationMarker: "1️⃣",
message: "found an unexpected second identifier in pattern; is there an accidental break?",
fixIts: ["join the identifiers together", "join the identifiers together with camel-case"]
)
],
fixedSource: """
private var ab : Int = ""
"""
)
}

func testRecovery65() {
assertParse(
"""
Expand Down