Skip to content

Commit 490baac

Browse files
committed
Add diagnostic for dictionary type
1 parent 83d60db commit 490baac

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

Sources/SwiftParser/Types.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,22 @@ extension Parser {
10711071
)
10721072
}
10731073

1074+
if let colon = self.consume(if: .colon) {
1075+
let secondType = self.parseSimpleType()
1076+
let rightSquareBracket = self.consume(if: .rightSquareBracket) ?? self.missingToken(.rightSquareBracket)
1077+
1078+
result = RawTypeSyntax(
1079+
RawDictionaryTypeSyntax(
1080+
leftSquareBracket: self.missingToken(.leftSquareBracket),
1081+
keyType: result,
1082+
colon: colon,
1083+
valueType: secondType,
1084+
rightSquareBracket: rightSquareBracket,
1085+
arena: self.arena
1086+
)
1087+
)
1088+
}
1089+
10741090
if !self.currentToken.isAtStartOfLine {
10751091
if self.at(.postfixQuestionMark) {
10761092
result = RawTypeSyntax(self.parseOptionalType(result))

Tests/SwiftParserTest/translated/RecoveryTests.swift

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,23 +1157,29 @@ final class RecoveryTests: XCTestCase {
11571157
assertParse(
11581158
"""
11591159
struct ErrorTypeInVarDeclDictionaryType {
1160-
let a1: String1️⃣:
1161-
let a2: String2️⃣: Int]
1162-
let a3: String3️⃣: [Int]
1163-
let a4: String4️⃣: Int
1160+
let a1: 1️⃣String:2️⃣
1161+
let a2: 3️⃣String: Int]
1162+
let a3: 4️⃣String: [Int]5️⃣
1163+
let a4: 6️⃣String: Int7️⃣
11641164
}
11651165
""",
11661166
diagnostics: [
1167-
// TODO: Old parser expected error on line 2: unexpected ':' in type; did you mean to write a dictionary type?, Fix-It replacements: 11 - 11 = '['
1168-
DiagnosticSpec(locationMarker: "1️⃣", message: "consecutive declarations on a line must be separated by ';'"),
1169-
DiagnosticSpec(locationMarker: "1️⃣", message: "unexpected code ':' before variable"),
1170-
// TODO: Old parser expected error on line 3: unexpected ':' in type; did you mean to write a dictionary type?, Fix-It replacements: 11 - 11 = '['
1171-
DiagnosticSpec(locationMarker: "2️⃣", message: "unexpected code ': Int]' before variable"),
1172-
// 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 = ']'
1173-
DiagnosticSpec(locationMarker: "3️⃣", message: "unexpected code ': [Int]' before variable"),
1174-
// 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 = ']'
1175-
DiagnosticSpec(locationMarker: "4️⃣", message: "unexpected code ': Int' in struct"),
1176-
]
1167+
DiagnosticSpec(locationMarker: "1️⃣", message: "expected '[' to start dictionary type"),
1168+
DiagnosticSpec(locationMarker: "2️⃣", message: "expected value type and ']' to end dictionary type"),
1169+
DiagnosticSpec(locationMarker: "3️⃣", message: "expected '[' to start dictionary type"),
1170+
DiagnosticSpec(locationMarker: "4️⃣", message: "expected '[' to start dictionary type"),
1171+
DiagnosticSpec(locationMarker: "5️⃣", message: "expected ']' to end dictionary type"),
1172+
DiagnosticSpec(locationMarker: "6️⃣", message: "expected '[' to start dictionary type"),
1173+
DiagnosticSpec(locationMarker: "7️⃣", message: "expected ']' to end dictionary type"),
1174+
],
1175+
fixedSource: """
1176+
struct ErrorTypeInVarDeclDictionaryType {
1177+
let a1: [String: <#type#> ]
1178+
let a2: [String: Int]
1179+
let a3: [String: [Int]]
1180+
let a4: [String: Int]
1181+
}
1182+
"""
11771183
)
11781184
}
11791185

0 commit comments

Comments
 (0)