Skip to content
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

Ensure there are no optional syntax collection in the syntax tree #2043

Merged
merged 5 commits into from
Aug 11, 2023

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented Aug 9, 2023

All syntax collections should be non-optional, which makes it easier to work with them. Also, the semantics of what an empty collection vs. a nil collection have not been clear.

The children that were modified were:

  • additionalTrailingClosures
  • modifiers
  • attributes
  • VersionTupleSyntax.components (whose parameter I also changed from defaulting to nil to not having a default value because most of the time you want to specify minor and patch components of a version)
  • ClosureCaptureClauseSyntax.items (also doesn’t have a default value anymore because it doesn’t make sense to define an empty capture clause)
  • CatchClauseSyntax.catchItems
  • DoStmt.catchClauses

This has API compatibility impact because we don’t have an API-compatible way to change the optionality of variables with deprecation warnings. That’s why I want to get these changes in before the 509 release.


To make the construction of syntax collections easier, I hoisted the conformance of syntax collections to ExpressibleByArrayLiteral from SwiftSyntaxBuilder to the SwiftSyntax module. This

Fixes #2040

@ahoppen ahoppen force-pushed the ahoppen/no-optional-collections branch from b3f3692 to 47d09ff Compare August 9, 2023 19:38
@ahoppen ahoppen force-pushed the ahoppen/no-optional-collections branch from 47d09ff to b379078 Compare August 9, 2023 19:38
@ahoppen
Copy link
Member Author

ahoppen commented Aug 9, 2023

@ahoppen ahoppen force-pushed the ahoppen/no-optional-collections branch 2 times, most recently from fedb281 to c1aa2e9 Compare August 10, 2023 01:36
@ahoppen
Copy link
Member Author

ahoppen commented Aug 10, 2023

Copy link
Contributor

@bnbarham bnbarham left a comment

Choose a reason for hiding this comment

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

whose parameter I also changed from defaulting to nil to not having a default value because most of the time you want to specify minor and patch components of a version

I don't know how true that actually is, major is fine for most availability cases for example (though maybe .0 is common to add)

@@ -58,7 +58,7 @@ extension AccessorDeclListSyntax: SyntaxParseable {
return parser.parseAccessorList() ?? RawAccessorDeclListSyntax(elements: [], arena: parser.arena)
} makeMissing: { remainingTokens, arena in
return RawAccessorDeclSyntax(
attributes: nil,
attributes: RawAttributeListSyntax(elements: [], arena: arena),
Copy link
Contributor

Choose a reason for hiding this comment

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

Did you make this not emptyCollection because it's an entry function?

Copy link
Member Author

Choose a reason for hiding this comment

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

Because that would require access to the parser, which would be an overlapping access to parser. So to actually use emptyCollection, we would need to pass the parser into makeMissing and I didn’t want to change that since creation of an empty collection is sufficiently simple here and shouldn’t happen in normal code.

This hoists the ability to create syntax collections from `SwiftSyntaxBuilder` to the `SwiftSyntax` module.

Impact on API compatibility: Initializing an `ExprListSyntax` or `UnexpectedNodesSyntax` now requires the nodes to be `ExprSyntax` and `Syntax` opposed to `ExprSyntaxProtocol` or `SyntaxProtocol`, respectively. `ExprListSyntax` is used as the child of a `SequenceExprSyntax`. I think this is OK because
- Users don’t genrally construct `UnexpectedNodesSyntax`
- To construct a `SequenceExprSyntax`, you can use the result builder on `SequenceExprSyntax`, which takes `ExprSyntaxProtocol`
All syntax collections should be non-optional, which makes it easier to work with them. Also, the semantics of what an empty collection vs. a `nil` collection have not been clear.
@ahoppen ahoppen force-pushed the ahoppen/no-optional-collections branch from c1aa2e9 to b6f4595 Compare August 11, 2023 00:31
@ahoppen
Copy link
Member Author

ahoppen commented Aug 11, 2023

@ahoppen
Copy link
Member Author

ahoppen commented Aug 11, 2023

@ahoppen
Copy link
Member Author

ahoppen commented Aug 11, 2023

@ahoppen
Copy link
Member Author

ahoppen commented Aug 11, 2023

@ahoppen ahoppen merged commit 91b4481 into swiftlang:main Aug 11, 2023
3 checks passed
@ahoppen ahoppen deleted the ahoppen/no-optional-collections branch August 11, 2023 22:27
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.

Make all syntax collections non-optional
3 participants