Skip to content

Add diagnostic for dictionary #1484

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
merged 1 commit into from
Apr 28, 2023
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
24 changes: 24 additions & 0 deletions Sources/SwiftParser/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,30 @@ extension Parser {
arena: self.arena
)
)
} else if self.at(.colon) {
var lookahead = self.lookahead()

// We only want to continue with a dictionary if we can parse a colon and a simpletype.
// Otherwise we can get a wrong diagnostic if we get a Python-style function declaration.
guard lookahead.consume(if: .colon) != nil && lookahead.canParseSimpleType() else {
return result
}

let (unexpectedBeforeColon, colon) = self.expect(.colon)
let secondType = self.parseSimpleType()
let rightSquareBracket = self.consume(if: .rightSquareBracket) ?? self.missingToken(.rightSquareBracket)

result = RawTypeSyntax(
RawDictionaryTypeSyntax(
leftSquareBracket: self.missingToken(.leftSquareBracket),
keyType: result,
unexpectedBeforeColon,
colon: colon,
valueType: secondType,
rightSquareBracket: rightSquareBracket,
arena: self.arena
)
)
}

var loopProgress = LoopProgressCondition()
Expand Down
77 changes: 64 additions & 13 deletions Tests/SwiftParserTest/translated/RecoveryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1204,22 +1204,63 @@ final class RecoveryTests: XCTestCase {
"""
struct ErrorTypeInVarDeclDictionaryType {
let a1: String1️⃣:
let a2: String2️⃣: Int]
let a3: String3️⃣: [Int]
let a4: String4️⃣: Int
let a2: 2️⃣String: Int]
let a3: 3️⃣String: [Int]4️⃣
let a4: 5️⃣String: Int6️⃣
let a4: 7️⃣String: Int]??
}
""",
diagnostics: [
// TODO: Old parser expected error on line 2: unexpected ':' in type; did you mean to write a dictionary type?, Fix-It replacements: 11 - 11 = '['
DiagnosticSpec(locationMarker: "1️⃣", message: "consecutive declarations on a line must be separated by ';'", fixIts: ["insert ';'"]),
DiagnosticSpec(locationMarker: "1️⃣", message: "unexpected code ':' before variable"),
// TODO: Old parser expected error on line 3: unexpected ':' in type; did you mean to write a dictionary type?, Fix-It replacements: 11 - 11 = '['
DiagnosticSpec(locationMarker: "2️⃣", message: "unexpected code ': Int]' before variable"),
// TODO: Old parser expected error on line 4: unexpected ':' in type; did you mean to write a dictionary type?, Fix-It replacements: 11 - 11 = '[', 24 - 24 = ']'
DiagnosticSpec(locationMarker: "3️⃣", message: "unexpected code ': [Int]' before variable"),
// TODO: Old parser expected error on line 5: unexpected ':' in type; did you mean to write a dictionary type?, Fix-It replacements: 11 - 11 = '[', 22 - 22 = ']'
DiagnosticSpec(locationMarker: "4️⃣", message: "unexpected code ': Int' in struct"),
]
DiagnosticSpec(
locationMarker: "1️⃣",
message: "consecutive declarations on a line must be separated by ';'",
fixIts: ["insert ';'"]
),
DiagnosticSpec(
locationMarker: "1️⃣",
message: "unexpected code ':' before variable"
),
DiagnosticSpec(
locationMarker: "2️⃣",
message: "expected '[' to start dictionary type",
fixIts: ["insert '['"]
),
DiagnosticSpec(
locationMarker: "3️⃣",
message: "expected '[' to start dictionary type",
fixIts: ["insert '['"]
),
DiagnosticSpec(
locationMarker: "4️⃣",
message: "expected ']' to end dictionary type",
fixIts: ["insert ']'"]
),
DiagnosticSpec(
locationMarker: "5️⃣",
message: "expected '[' to start dictionary type",
fixIts: ["insert '['"]
),
DiagnosticSpec(
locationMarker: "6️⃣",
message: "expected ']' to end dictionary type",
fixIts: ["insert ']'"]
),
DiagnosticSpec(
locationMarker: "7️⃣",
message: "expected '[' to start dictionary type",
fixIts: ["insert '['"]
),

],
fixedSource: """
struct ErrorTypeInVarDeclDictionaryType {
let a1: String;:
let a2: [String: Int]
let a3: [String: [Int]]
let a4: [String: Int]
let a4: [String: Int]??
}
"""
)
}

Expand Down Expand Up @@ -2340,4 +2381,14 @@ final class RecoveryTests: XCTestCase {
"""
)
}

// https://github.com/apple/swift-syntax/pull/1484/files#r1176740738
func testRecovery184() {
assertParse(
"func foo() -> Int1️⃣:",
diagnostics: [
DiagnosticSpec(message: "extraneous code ':' at top level")
]
)
}
}