Skip to content

Fix loop breaking condition for dictionary literal parsing. #812

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

Closed
Closed
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
4 changes: 2 additions & 2 deletions Sources/SwiftParser/Expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1557,7 +1557,7 @@ extension Parser {
mutating func parseCollectionElement(_ existing: CollectionKind?) -> CollectionKind {
let key = self.parseExpression()
switch existing {
case .array(_):
case .array:
return .array(key)
case nil:
guard self.at(.colon) else {
Expand Down Expand Up @@ -1630,7 +1630,7 @@ extension Parser {
valueExpression: value,
trailingComma: comma,
arena: self.arena)))
if key.is(RawMissingExprSyntax.self), colon.isMissing, value.is(RawMissingExprSyntax.self) {
if key.is(RawMissingExprSyntax.self) || colon.isMissing || value.is(RawMissingExprSyntax.self) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if this is superior in general. IIUC if you have

[
  1: "one",
  2: ,
  3: "three"
]

we wouldn’t be stopping dictionary literal parsing at the missing value for 2 and thus not add 3: "three" to the string literal.

What do key and value contain when we synthesize the missing colon?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the pointer, I'll try to investigate further.

Interestingly, your example produces no diagnostics for me with the original implementation 😅

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I forgot that my example will only start producing diagnostics once #739 is merged.

break COLLECTION_LOOP
}
}
Expand Down
4 changes: 1 addition & 3 deletions Tests/SwiftParserTest/Expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,7 @@ final class ExpressionTests: XCTestCase {
([1:#^DIAG^#)
""",
diagnostics: [
// FIXME: Why is this diagnostic produced?
DiagnosticSpec(message: "Expected ':' in dictionary"),
DiagnosticSpec(message: "Expected ']' to end dictionary"),
DiagnosticSpec(message: "Expected ']' to end dictionary")
]
)
}
Expand Down