Skip to content

Commit 76e7a20

Browse files
committed
Add diagnostic for dictionary type
1 parent a648679 commit 76e7a20

File tree

2 files changed

+73
-15
lines changed

2 files changed

+73
-15
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: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,7 @@ final class RecoveryTests: XCTestCase {
810810
}
811811

812812
func testRecovery63() {
813+
813814
assertParse(
814815
#"""
815816
struct SS 1️⃣SS : Multi {
@@ -831,7 +832,8 @@ final class RecoveryTests: XCTestCase {
831832
message: "expected ':' in type annotation",
832833
fixIts: ["insert ':'"]
833834
),
834-
DiagnosticSpec(locationMarker: "3️⃣", message: #"unexpected code ': Int = ""' before function"#),
835+
DiagnosticSpec(
836+
locationMarker: "3️⃣", message: #"unexpected code ': Int = ""' before function"#),
835837
DiagnosticSpec(
836838
locationMarker: "4️⃣",
837839
message: "expected ':' in type annotation",
@@ -1177,23 +1179,65 @@ final class RecoveryTests: XCTestCase {
11771179
assertParse(
11781180
"""
11791181
struct ErrorTypeInVarDeclDictionaryType {
1180-
let a1: String1️⃣:
1181-
let a2: String2️⃣: Int]
1182-
let a3: String3️⃣: [Int]
1183-
let a4: String4️⃣: Int
1182+
let a1: 1️⃣String:2️⃣
1183+
let a2: 3️⃣String: Int]
1184+
let a3: 4️⃣String: [Int]5️⃣
1185+
let a4: 6️⃣String: Int7️⃣
1186+
let a4: 8️⃣String: Int]??
11841187
}
11851188
""",
11861189
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-
]
1190+
DiagnosticSpec(
1191+
locationMarker: "1️⃣",
1192+
message: "expected '[' to start dictionary type",
1193+
fixIts: ["insert '['"]
1194+
),
1195+
DiagnosticSpec(
1196+
locationMarker: "2️⃣",
1197+
message: "expected value type and ']' to end dictionary type",
1198+
fixIts: ["insert value type and ']'"]
1199+
),
1200+
DiagnosticSpec(
1201+
locationMarker: "3️⃣",
1202+
message: "expected '[' to start dictionary type",
1203+
fixIts: ["insert '['"]
1204+
),
1205+
DiagnosticSpec(
1206+
locationMarker: "4️⃣",
1207+
message: "expected '[' to start dictionary type",
1208+
fixIts: ["insert '['"]
1209+
),
1210+
DiagnosticSpec(
1211+
locationMarker: "5️⃣",
1212+
message: "expected ']' to end dictionary type",
1213+
fixIts: ["insert ']'"]
1214+
),
1215+
DiagnosticSpec(
1216+
locationMarker: "6️⃣",
1217+
message: "expected '[' to start dictionary type",
1218+
fixIts: ["insert '['"]
1219+
),
1220+
DiagnosticSpec(
1221+
locationMarker: "7️⃣",
1222+
message: "expected ']' to end dictionary type",
1223+
fixIts: ["insert ']'"]
1224+
),
1225+
DiagnosticSpec(
1226+
locationMarker: "8️⃣",
1227+
message: "expected '[' to start dictionary type",
1228+
fixIts: ["insert '['"]
1229+
),
1230+
1231+
],
1232+
fixedSource: """
1233+
struct ErrorTypeInVarDeclDictionaryType {
1234+
let a1: [String: <#type#>]
1235+
let a2: [String: Int]
1236+
let a3: [String: [Int]]
1237+
let a4: [String: Int]
1238+
let a4: [String: Int]??
1239+
}
1240+
"""
11971241
)
11981242
}
11991243

0 commit comments

Comments
 (0)