Skip to content

Commit a8e5363

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

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

Sources/SwiftParser/Types.swift

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

1060-
if self.at(.rightSquareBracket) {
1061-
let (unexpectedBeforeRSquareBracket, rightSquareBracket) = self.expect(.rightSquareBracket)
1060+
var lookahead = self.lookahead()
1061+
1062+
// If we are at a new line, we cannot assume it's an unfinished dictionary or array.
1063+
if lookahead.currentToken.isAtStartOfLine {
1064+
return result
1065+
}
1066+
1067+
if let rightSquareBracket = self.consume(if: .rightSquareBracket) {
10621068
result = RawTypeSyntax(
10631069
RawArrayTypeSyntax(
10641070
leftSquareBracket: missingToken(.leftSquareBracket),
10651071
elementType: result,
1066-
unexpectedBeforeRSquareBracket,
10671072
rightSquareBracket: rightSquareBracket,
10681073
arena: self.arena
10691074
)
10701075
)
10711076
} else if self.at(.colon) {
1072-
var lookahead = self.lookahead()
1073-
10741077
// We only want to continue with a dictionary if we can parse a colon and a simpletype.
10751078
// Otherwise we can get a wrong diagnostic if we get a Python-style function declaration.
10761079
guard lookahead.consume(if: .colon) != nil && lookahead.canParseSimpleType() else {

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)