Skip to content

Commit 8fc622b

Browse files
committed
Avoid adding diagnostic for array and dictionary when the closing bracket is on a new line
1 parent 3ed9dcf commit 8fc622b

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

Sources/SwiftParser/Types.swift

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,35 +1057,33 @@ extension Parser {
10571057
return result
10581058
}
10591059

1060-
if self.at(.rightSquareBracket) {
1061-
let (unexpectedBeforeRSquareBracket, rightSquareBracket) = self.expect(.rightSquareBracket)
1060+
// If the right square bracket is at a new line, we should just return the result
1061+
if let rightSquareBracket = self.consume(if: TokenSpec(.rightSquareBracket, allowAtStartOfLine: false)) {
10621062
result = RawTypeSyntax(
10631063
RawArrayTypeSyntax(
10641064
leftSquareBracket: missingToken(.leftSquareBracket),
10651065
elementType: result,
1066-
unexpectedBeforeRSquareBracket,
10671066
rightSquareBracket: rightSquareBracket,
10681067
arena: self.arena
10691068
)
10701069
)
10711070
} else if self.at(.colon) {
10721071
var lookahead = self.lookahead()
1073-
10741072
// We only want to continue with a dictionary if we can parse a colon and a simpletype.
10751073
// Otherwise we can get a wrong diagnostic if we get a Python-style function declaration.
1076-
guard lookahead.consume(if: .colon) != nil && lookahead.canParseSimpleType() else {
1074+
guard lookahead.consume(if: .colon) != nil && lookahead.canParseSimpleType(),
1075+
let colon = self.consume(if: TokenSpec(.colon, allowAtStartOfLine: false))
1076+
else {
10771077
return result
10781078
}
10791079

1080-
let (unexpectedBeforeColon, colon) = self.expect(.colon)
10811080
let secondType = self.parseSimpleType()
10821081
let rightSquareBracket = self.consume(if: .rightSquareBracket) ?? self.missingToken(.rightSquareBracket)
10831082

10841083
result = RawTypeSyntax(
10851084
RawDictionaryTypeSyntax(
10861085
leftSquareBracket: self.missingToken(.leftSquareBracket),
10871086
keyType: result,
1088-
unexpectedBeforeColon,
10891087
colon: colon,
10901088
valueType: secondType,
10911089
rightSquareBracket: rightSquareBracket,

Tests/SwiftParserTest/translated/RecoveryTests.swift

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,6 +1498,18 @@ final class RecoveryTests: XCTestCase {
14981498
line: line
14991499
)
15001500
}
1501+
1502+
assertParse(
1503+
"""
1504+
let a8: Int
1505+
1️⃣]
1506+
""",
1507+
diagnostics: [
1508+
DiagnosticSpec(
1509+
message: "extraneous code ']' at top level"
1510+
)
1511+
]
1512+
)
15011513
}
15021514

15031515
func testRecovery98b() {
@@ -1527,7 +1539,7 @@ final class RecoveryTests: XCTestCase {
15271539
let a2: 2️⃣String: Int]
15281540
let a3: 3️⃣String: [Int]4️⃣
15291541
let a4: 5️⃣String: Int6️⃣
1530-
let a4: 7️⃣String: Int]??
1542+
let a5: 7️⃣String: Int]??
15311543
}
15321544
""",
15331545
diagnostics: [
@@ -1577,10 +1589,22 @@ final class RecoveryTests: XCTestCase {
15771589
let a2: [String: Int]
15781590
let a3: [String: [Int]]
15791591
let a4: [String: Int]
1580-
let a4: [String: Int]??
1592+
let a5: [String: Int]??
15811593
}
15821594
"""
15831595
)
1596+
1597+
assertParse(
1598+
"""
1599+
let a6: [Int: String]
1600+
1️⃣]
1601+
""",
1602+
diagnostics: [
1603+
DiagnosticSpec(
1604+
message: "extraneous code ']' at top level"
1605+
)
1606+
]
1607+
)
15841608
}
15851609

15861610
func testRecovery100() {

0 commit comments

Comments
 (0)