Skip to content

Expand trailing comma implementation #2689

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 2 commits into from
Aug 16, 2024

Conversation

mateusrodriguesxyz
Copy link
Contributor

This continues the work that I began in #2515 (comment). After feedback from the language steering group I have expanded the original proposal to allow trailing comma in more places. See the updated proposal text in swiftlang/swift-evolution#2344.

Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

Thanks for your continued work on this @mateusrodriguesxyz!

@@ -700,7 +704,7 @@ extension Parser {
arena: self.arena
)
)
} while keepGoing != nil && self.hasProgressed(&loopProgress)
} while keepGoing != nil && self.hasProgressed(&loopProgress) && !self.atWhereClauseListTerminator()
Copy link
Member

Choose a reason for hiding this comment

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

Generic where clauses don’t necessarily need to be followed by a brace. For example you can have the following, which is valid.

protocol Foo {
  func test<T>(abc: T) where T: Comparable
}

Because of the lack of a proper terminated for the where classes, I’m actually not sure if we should allow trailing commas here. Checking if the next token is a keyword also doesn’t work because it could be a contextual keyword like nonisolated on the following function declaration.

Comment on lines 891 to 895
mutating func atEnumCaseListTerminator() -> Bool {
return self.experimentalFeatures.contains(.trailingComma)
&& (self.atStartOfLine || self.at(.rightBrace) || self.at(.semicolon))
}

Copy link
Member

Choose a reason for hiding this comment

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

Wouldn’t this mean that we’d stop parsing the following as valid because we classify the comma as a trailing comma and then mark bar as unexpected?

enum A {
  case foo,
       bar
}

Similar to the where clause, I’m not sure if we want to allow a trailing comma here because there is no list terminator.

Copy link
Contributor Author

@mateusrodriguesxyz mateusrodriguesxyz Jun 19, 2024

Choose a reason for hiding this comment

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

Completely missed this, which is ironic because that's exactly how one would use a list with trailing comma 😅 I will double check all comma-separated list to make sure there is a clear terminator and remove the one without it.

Comment on lines +342 to +345
mutating func atInheritanceListTerminator() -> Bool {
return self.experimentalFeatures.contains(.trailingComma) && (self.at(.leftBrace) || self.at(.keyword(.where)))
}

Copy link
Member

Choose a reason for hiding this comment

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

I think this doesn’t work in general because associatedtype can also have an inheritance clause and it is not followed by { or where.

Eg.

associatedtype Iterator: IteratorProtocol, 

would probably fail to parse wit this change.

I think we should limit the trailing comma to the types where it is guaranteed to be followed by where or { (ie. everything except associatedtype).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ahoppen I believe it's just a matter of update the proposal text mentioning that trailing comma won't work for inheritance clause of an associatedtype, right? The same for where clauses for protocol definition. In general, make it clear that trailing comma will only be allowed when there's a clear terminator.

Copy link
Member

Choose a reason for hiding this comment

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

I’m only speaking as a reviewer of this PR here but I think it would make sense to exclude trailing commas for these situations without a clear terminator from the evolution proposal for now.

There might be ways of getting them right, but doing so is more complicated than the cases that have a clear terminator. Highlighting these situations in a Future directions section would make sense, I think. That way your proposal can focus on the majority of cases and if we do choose to extend trailing commas eg. case lists, the corner cases will get the attention they deserve.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've updated the proposal text highlighting these cases. Thanks for catching them.

Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

Looking good, just two discussions from my last review that I would like to continue.

@mateusrodriguesxyz
Copy link
Contributor Author

@ahoppen I've updated the PR to match swiftlang/swift#74522.

@ahoppen
Copy link
Member

ahoppen commented Aug 15, 2024

@swift-ci Please test

@ahoppen ahoppen enabled auto-merge August 15, 2024 00:09
@mateusrodriguesxyz
Copy link
Contributor Author

mateusrodriguesxyz commented Aug 15, 2024

@ahoppen Don't you prefer that I squash the commits before merging?
I went ahead and squashed the commits to stop the auto-merge as a precaution. Sorry for stopping the CI.

auto-merge was automatically disabled August 15, 2024 00:49

Head branch was pushed to by a user without write access

@ahoppen
Copy link
Member

ahoppen commented Aug 15, 2024

@swift-ci Please test

@ahoppen ahoppen enabled auto-merge August 15, 2024 03:18
@ahoppen
Copy link
Member

ahoppen commented Aug 15, 2024

@swift-ci Please test Windows

@ahoppen
Copy link
Member

ahoppen commented Aug 15, 2024

Could you run swift-format on your changes?

auto-merge was automatically disabled August 15, 2024 18:22

Head branch was pushed to by a user without write access

@ahoppen
Copy link
Member

ahoppen commented Aug 15, 2024

@swift-ci Please test

1 similar comment
@ahoppen
Copy link
Member

ahoppen commented Aug 15, 2024

@swift-ci Please test

@ahoppen ahoppen merged commit 6d9acfc into swiftlang:main Aug 16, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants