Skip to content

Add missing comma diagnostic if it's inside an array or dictionary #2257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 26 additions & 27 deletions Sources/SwiftParser/Expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1497,17 +1497,38 @@ extension Parser {
var elements = [RawSyntax]()
do {
var collectionProgress = LoopProgressCondition()
COLLECTION_LOOP: while self.hasProgressed(&collectionProgress) {
var keepGoing: RawTokenSyntax?
COLLECTION_LOOP: repeat {
elementKind = self.parseCollectionElement(elementKind)

/// Whether expression of an array element or the value of a dictionary
/// element is missing. If this is the case, we shouldn't recover from
/// a missing comma since most likely the closing `]` is missing.
var elementIsMissingExpression: Bool {
switch elementKind! {
case .dictionary(_, _, _, let value):
return value.is(RawMissingExprSyntax.self)
case .array(let rawExprSyntax):
return rawExprSyntax.is(RawMissingExprSyntax.self)
}
}

// Parse the ',' if exists.
let comma = self.consume(if: .comma)
if let token = self.consume(if: .comma) {
keepGoing = token
} else if !self.at(.rightSquare, .endOfFile) && !self.atStartOfLine && !elementIsMissingExpression && !self.atStartOfDeclaration()
&& !self.atStartOfStatement(preferExpr: false)
{
keepGoing = missingToken(.comma)
} else {
keepGoing = nil
}

switch elementKind! {
case .array(let el):
let element = RawArrayElementSyntax(
expression: el,
trailingComma: comma,
trailingComma: keepGoing,
arena: self.arena
)
if element.isEmpty {
Expand All @@ -1521,7 +1542,7 @@ extension Parser {
unexpectedBeforeColon,
colon: colon,
value: value,
trailingComma: comma,
trailingComma: keepGoing,
arena: self.arena
)
if element.isEmpty {
Expand All @@ -1530,29 +1551,7 @@ extension Parser {
elements.append(RawSyntax(element))
}
}

// If we saw a comma, that's a strong indicator we have more elements
// to process. If that's not the case, we have to do some legwork to
// determine if we should bail out.
guard comma == nil || self.at(.rightSquare, .endOfFile) else {
continue
}

// If we found EOF or the closing square bracket, bailout.
if self.at(.rightSquare, .endOfFile) {
break
}

// If the next token is at the beginning of a new line and can never start
// an element, break.
if self.atStartOfLine
&& (self.at(.rightBrace, .poundEndif)
|| self.atStartOfDeclaration()
|| self.atStartOfStatement(preferExpr: false))
{
break
}
}
} while keepGoing != nil && self.hasProgressed(&collectionProgress)
}

let (unexpectedBeforeRSquare, rsquare) = self.expect(.rightSquare)
Expand Down
102 changes: 93 additions & 9 deletions Tests/SwiftParserTest/ExpressionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2762,11 +2762,15 @@ final class StatementExpressionTests: ParserTestCase {
experimentalFeatures: .typedThrows
)
assertParse(
"[() try(MyError) async -> Void]()",
"[() 1️⃣try(MyError) async -> Void]()",
diagnostics: [DiagnosticSpec(message: "expected ',' in array element", fixIts: ["insert ','"])],
fixedSource: "[(), try(MyError) async -> Void]()",
experimentalFeatures: .typedThrows
)
assertParse(
"[() try async(MyError) -> Void]()",
"[() 1️⃣try async(MyError) -> Void]()",
diagnostics: [DiagnosticSpec(message: "expected ',' in array element", fixIts: ["insert ','"])],
fixedSource: "[(), try async(MyError) -> Void]()",
experimentalFeatures: .typedThrows
)
assertParse(
Expand All @@ -2787,30 +2791,49 @@ final class StatementExpressionTests: ParserTestCase {
experimentalFeatures: .typedThrows
)
assertParse(
"[() try(MyError) await 1️⃣-> Void]()",
"[() 1️⃣try(MyError) 2️⃣await 3️⃣-> Void]()",
diagnostics: [
DiagnosticSpec(
locationMarker: "1️⃣",
message: "expected ',' in array element",
fixIts: ["insert ','"]
),
DiagnosticSpec(
locationMarker: "2️⃣",
message: "expected ',' in array element",
fixIts: ["insert ','"]
),
DiagnosticSpec(
locationMarker: "3️⃣",
message: "expected expression in 'await' expression",
fixIts: ["insert expression"]
)
),
],
fixedSource: "[() try(MyError) await <#expression#> -> Void]()",
fixedSource: "[(), try(MyError), await <#expression#> -> Void]()",
experimentalFeatures: .typedThrows
)
assertParse(
"[() try await(MyError) -> Void]()",
"[() 1️⃣try await(MyError) -> Void]()",
diagnostics: [DiagnosticSpec(message: "expected ',' in array element", fixIts: ["insert ','"])],
fixedSource: "[(), try await(MyError) -> Void]()",
experimentalFeatures: .typedThrows
)
assertParse(
"[() async(MyError) -> Void]()",
"[() 1️⃣async(MyError) -> Void]()",
diagnostics: [DiagnosticSpec(message: "expected ',' in array element", fixIts: ["insert ','"])],
fixedSource: "[(), async(MyError) -> Void]()",
experimentalFeatures: .typedThrows
)
assertParse(
"[() await(MyError) -> Void]()",
"[() 1️⃣await(MyError) -> Void]()",
diagnostics: [DiagnosticSpec(message: "expected ',' in array element", fixIts: ["insert ','"])],
fixedSource: "[(), await(MyError) -> Void]()",
experimentalFeatures: .typedThrows
)
assertParse(
"[() try(MyError) -> Void]()",
"[() 1️⃣try(MyError) -> Void]()",
diagnostics: [DiagnosticSpec(message: "expected ',' in array element", fixIts: ["insert ','"])],
fixedSource: "[(), try(MyError) -> Void]()",
experimentalFeatures: .typedThrows
)
assertParse(
Expand All @@ -2826,4 +2849,65 @@ final class StatementExpressionTests: ParserTestCase {
experimentalFeatures: .typedThrows
)
}

func testArrayExprWithNoCommas() {
assertParse("[() ()]")

assertParse(
"[1 1️⃣2]",
diagnostics: [
DiagnosticSpec(
message: "expected ',' in array element",
fixIts: ["insert ','"]
)
],
fixedSource: "[1, 2]"
)

assertParse(
#"["hello" 1️⃣"world"]"#,
diagnostics: [
DiagnosticSpec(
message: "expected ',' in array element",
fixIts: ["insert ','"]
)
],
fixedSource: #"["hello", "world"]"#
)
}

func testDictionaryExprWithNoCommas() {
assertParse(
"[1: () 1️⃣2: ()]",
diagnostics: [
DiagnosticSpec(
message: "expected ',' in dictionary element",
fixIts: ["insert ','"]
)
],
fixedSource: #"[1: (), 2: ()]"#
)

assertParse(
#"["foo": 1 1️⃣"bar": 2]"#,
diagnostics: [
DiagnosticSpec(
message: "expected ',' in dictionary element",
fixIts: ["insert ','"]
)
],
fixedSource: #"["foo": 1, "bar": 2]"#
)

assertParse(
#"[1: "hello" 1️⃣2: "world"]"#,
diagnostics: [
DiagnosticSpec(
message: "expected ',' in dictionary element",
fixIts: ["insert ','"]
)
],
fixedSource: #"[1: "hello", 2: "world"]"#
)
}
}
45 changes: 27 additions & 18 deletions Tests/SwiftParserTest/translated/ObjectLiteralsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,44 @@ final class ObjectLiteralsTests: ParserTestCase {
func testObjectLiterals1a() {
assertParse(
"""
let _ = [#Color(colorLiteralRed: red, green: green, blue: blue, alpha: alpha)#1️⃣]
let _ = [#Color(colorLiteralRed: red, green: green, blue: blue, alpha: alpha)1️⃣#2️⃣]
""",
diagnostics: [
DiagnosticSpec(locationMarker: "1️⃣", message: "expected identifier in macro expansion", fixIts: ["insert identifier"])
DiagnosticSpec(locationMarker: "1️⃣", message: "expected ',' in array element", fixIts: ["insert ','"]),
DiagnosticSpec(locationMarker: "2️⃣", message: "expected identifier in macro expansion", fixIts: ["insert identifier"]),
],
fixedSource: """
let _ = [#Color(colorLiteralRed: red, green: green, blue: blue, alpha: alpha)#<#identifier#>]
let _ = [#Color(colorLiteralRed: red, green: green, blue: blue, alpha: alpha), #<#identifier#>]
"""
)
}

func testObjectLiterals1b() {
assertParse(
"""
let _ = [#Image(imageLiteral: localResourceNameAsString)#1️⃣]
let _ = [#Image(imageLiteral: localResourceNameAsString)1️⃣#2️⃣]
""",
diagnostics: [
DiagnosticSpec(locationMarker: "1️⃣", message: "expected identifier in macro expansion", fixIts: ["insert identifier"])
DiagnosticSpec(locationMarker: "1️⃣", message: "expected ',' in array element", fixIts: ["insert ','"]),
DiagnosticSpec(locationMarker: "2️⃣", message: "expected identifier in macro expansion", fixIts: ["insert identifier"]),
],
fixedSource: """
let _ = [#Image(imageLiteral: localResourceNameAsString)#<#identifier#>]
let _ = [#Image(imageLiteral: localResourceNameAsString), #<#identifier#>]
"""
)
}

func testObjectLiterals1c() {
assertParse(
"""
let _ = [#FileReference(fileReferenceLiteral: localResourceNameAsString)#1️⃣]
let _ = [#FileReference(fileReferenceLiteral: localResourceNameAsString)1️⃣#2️⃣]
""",
diagnostics: [
DiagnosticSpec(locationMarker: "1️⃣", message: "expected identifier in macro expansion", fixIts: ["insert identifier"])
DiagnosticSpec(locationMarker: "1️⃣", message: "expected ',' in array element", fixIts: ["insert ','"]),
DiagnosticSpec(locationMarker: "2️⃣", message: "expected identifier in macro expansion", fixIts: ["insert identifier"]),
],
fixedSource: """
let _ = [#FileReference(fileReferenceLiteral: localResourceNameAsString)#<#identifier#>]
let _ = [#FileReference(fileReferenceLiteral: localResourceNameAsString), #<#identifier#>]
"""
)
}
Expand Down Expand Up @@ -112,22 +115,22 @@ final class ObjectLiteralsTests: ParserTestCase {
""",
diagnostics: [
DiagnosticSpec(locationMarker: "1️⃣", message: "expected identifier in macro expansion", fixIts: ["insert identifier"]),
DiagnosticSpec(locationMarker: "1️⃣", message: "expected ',' in array element", fixIts: ["insert ','"]),
DiagnosticSpec(locationMarker: "2️⃣", message: "expected identifier in macro expansion", fixIts: ["insert identifier"]),
],
fixedSource: """
let _ = [#<#identifier#> #<#identifier#>]
let _ = [#<#identifier#>, #<#identifier#>]
"""
)
}

func testObjectLiterals5() {
assertParse(
"""
let _ = ℹ️[#Color(_: 1, green: 1, 2)2️⃣
let _ = ℹ️[#Color(_: 1, green: 1, 2)1️⃣
""",
diagnostics: [
DiagnosticSpec(
locationMarker: "2️⃣",
message: "expected ']' to end array",
notes: [NoteSpec(message: "to match this opening '['")],
fixIts: ["insert ']'"]
Expand All @@ -142,37 +145,43 @@ final class ObjectLiteralsTests: ParserTestCase {
func testObjectLiterals6() {
assertParse(
"""
let _ = ℹ️[1️⃣#Color(red: 1, green: 1, blue: 1)#2️⃣3️⃣
let _ = ℹ️[#Color(red: 1, green: 1, blue: 1)1️⃣#2️⃣
""",
diagnostics: [
DiagnosticSpec(
locationMarker: "1️⃣",
message: "expected ',' in array element",
fixIts: ["insert ','"]
),
DiagnosticSpec(
locationMarker: "2️⃣",
message: "expected identifier in macro expansion",
fixIts: ["insert identifier"]
),
DiagnosticSpec(
locationMarker: "3️⃣",
locationMarker: "2️⃣",
message: "expected ']' to end array",
notes: [NoteSpec(message: "to match this opening '['")],
fixIts: ["insert ']'"]
),
],
fixedSource: """
let _ = [#Color(red: 1, green: 1, blue: 1)#<#identifier#>]
let _ = [#Color(red: 1, green: 1, blue: 1), #<#identifier#>]
"""
)
}

func testObjectLiterals7() {
assertParse(
"""
let _ = [#Color(withRed: 1, green: 1, whatever: 2)#1️⃣]
let _ = [#Color(withRed: 1, green: 1, whatever: 2)1️⃣#2️⃣]
""",
diagnostics: [
DiagnosticSpec(locationMarker: "1️⃣", message: "expected identifier in macro expansion", fixIts: ["insert identifier"])
DiagnosticSpec(locationMarker: "1️⃣", message: "expected ',' in array element", fixIts: ["insert ','"]),
DiagnosticSpec(locationMarker: "2️⃣", message: "expected identifier in macro expansion", fixIts: ["insert identifier"]),
],
fixedSource: """
let _ = [#Color(withRed: 1, green: 1, whatever: 2)#<#identifier#>]
let _ = [#Color(withRed: 1, green: 1, whatever: 2), #<#identifier#>]
"""
)
}
Expand Down
7 changes: 6 additions & 1 deletion Tests/SwiftParserTest/translated/RecoveryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2433,6 +2433,11 @@ final class RecoveryTests: ParserTestCase {
locationMarker: "4️⃣",
message: "unexpected code ') -> Int {}' in closure"
),
DiagnosticSpec(
locationMarker: "5️⃣",
message: "expected ',' in array element",
fixIts: ["insert ','"]
),
DiagnosticSpec(
locationMarker: "5️⃣",
message: "expected ']' to end array",
Expand All @@ -2449,7 +2454,7 @@ final class RecoveryTests: ParserTestCase {
struct Foo19605164 {
func a(s: S)
}[{{g) -> Int {}
}}]}
}},]}
#endif
"""
)
Expand Down