Skip to content

Commit abd7ec6

Browse files
committed
Add diagnostic for dictionary type
1 parent 61cc73e commit abd7ec6

File tree

2 files changed

+70
-14
lines changed

2 files changed

+70
-14
lines changed

Sources/SwiftParser/Types.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,6 +1069,20 @@ extension Parser {
10691069
arena: self.arena
10701070
)
10711071
)
1072+
} else if let colon = self.consume(if: .colon) {
1073+
let secondType = self.parseSimpleType()
1074+
let rightSquareBracket = self.consume(if: .rightSquareBracket) ?? self.missingToken(.rightSquareBracket)
1075+
1076+
result = RawTypeSyntax(
1077+
RawDictionaryTypeSyntax(
1078+
leftSquareBracket: self.missingToken(.leftSquareBracket),
1079+
keyType: result,
1080+
colon: colon,
1081+
valueType: secondType,
1082+
rightSquareBracket: rightSquareBracket,
1083+
arena: self.arena
1084+
)
1085+
)
10721086
}
10731087

10741088
var loopProgress = LoopProgressCondition()

Tests/SwiftParserTest/translated/RecoveryTests.swift

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,23 +1177,65 @@ final class RecoveryTests: XCTestCase {
11771177
assertParse(
11781178
"""
11791179
struct ErrorTypeInVarDeclDictionaryType {
1180-
let a1: String1️⃣:
1181-
let a2: String2️⃣: Int]
1182-
let a3: String3️⃣: [Int]
1183-
let a4: String4️⃣: Int
1180+
let a1: 1️⃣String:2️⃣
1181+
let a2: 3️⃣String: Int]
1182+
let a3: 4️⃣String: [Int]5️⃣
1183+
let a4: 6️⃣String: Int7️⃣
1184+
let a4: 8️⃣String: Int]??
11841185
}
11851186
""",
11861187
diagnostics: [
1187-
// TODO: Old parser expected error on line 2: unexpected ':' in type; did you mean to write a dictionary type?, Fix-It replacements: 11 - 11 = '['
1188-
DiagnosticSpec(locationMarker: "1️⃣", message: "consecutive declarations on a line must be separated by ';'", fixIts: ["insert ';'"]),
1189-
DiagnosticSpec(locationMarker: "1️⃣", message: "unexpected code ':' before variable"),
1190-
// TODO: Old parser expected error on line 3: unexpected ':' in type; did you mean to write a dictionary type?, Fix-It replacements: 11 - 11 = '['
1191-
DiagnosticSpec(locationMarker: "2️⃣", message: "unexpected code ': Int]' before variable"),
1192-
// 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 = ']'
1193-
DiagnosticSpec(locationMarker: "3️⃣", message: "unexpected code ': [Int]' before variable"),
1194-
// 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 = ']'
1195-
DiagnosticSpec(locationMarker: "4️⃣", message: "unexpected code ': Int' in struct"),
1196-
]
1188+
DiagnosticSpec(
1189+
locationMarker: "1️⃣",
1190+
message: "expected '[' to start dictionary type",
1191+
fixIts: ["insert '['"]
1192+
),
1193+
DiagnosticSpec(
1194+
locationMarker: "2️⃣",
1195+
message: "expected value type and ']' to end dictionary type",
1196+
fixIts: ["insert value type and ']'"]
1197+
),
1198+
DiagnosticSpec(
1199+
locationMarker: "3️⃣",
1200+
message: "expected '[' to start dictionary type",
1201+
fixIts: ["insert '['"]
1202+
),
1203+
DiagnosticSpec(
1204+
locationMarker: "4️⃣",
1205+
message: "expected '[' to start dictionary type",
1206+
fixIts: ["insert '['"]
1207+
),
1208+
DiagnosticSpec(
1209+
locationMarker: "5️⃣",
1210+
message: "expected ']' to end dictionary type",
1211+
fixIts: ["insert ']'"]
1212+
),
1213+
DiagnosticSpec(
1214+
locationMarker: "6️⃣",
1215+
message: "expected '[' to start dictionary type",
1216+
fixIts: ["insert '['"]
1217+
),
1218+
DiagnosticSpec(
1219+
locationMarker: "7️⃣",
1220+
message: "expected ']' to end dictionary type",
1221+
fixIts: ["insert ']'"]
1222+
),
1223+
DiagnosticSpec(
1224+
locationMarker: "8️⃣",
1225+
message: "expected '[' to start dictionary type",
1226+
fixIts: ["insert '['"]
1227+
),
1228+
1229+
],
1230+
fixedSource: """
1231+
struct ErrorTypeInVarDeclDictionaryType {
1232+
let a1: [String: <#type#>]
1233+
let a2: [String: Int]
1234+
let a3: [String: [Int]]
1235+
let a4: [String: Int]
1236+
let a4: [String: Int]??
1237+
}
1238+
"""
11971239
)
11981240
}
11991241

0 commit comments

Comments
 (0)