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

Add --enable-experimental-feature to enable those features in the parser. #876

Merged
merged 1 commit into from
Nov 12, 2024

Conversation

allevato
Copy link
Member

@allevato allevato commented Nov 8, 2024

Also add a couple small tests for value generics to exercise the capability in tests, and I took the opportunity to clean up some error handling with the addition of this new user-facing error when an experimental feature isn't recognized.

I'm passing the experimental features through as strings deeper than I'd like, but trying to resolve them to the Parser.ExperimentalFeatures type higher in the stack causes more problems since that type is behind an @_spi. To prevent SPI leakage in swift-format's public interfaces (and thus require those interfaces to also have their own @_spi-restricted overloads), just leave them as a Set<String> until we call Parser.parse directly.

This PR requires swiftlang/swift-syntax#2895.

Fixes #875.

Comment on lines 54 to 56
operatorTable.foldAll(Parser.parse(source: sourceBytes, experimentalFeatures: experimentalFeaturesSet)) { _ in }.as(
SourceFileSyntax.self
)!
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
operatorTable.foldAll(Parser.parse(source: sourceBytes, experimentalFeatures: experimentalFeaturesSet)) { _ in }.as(
SourceFileSyntax.self
)!
operatorTable.foldAll(Parser.parse(source: sourceBytes, experimentalFeatures: experimentalFeaturesSet)) { _ in }
.as(SourceFileSyntax.self)!

Reads better, I think.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

input: input,
expected: expected,
linelength: 20,
experimentalFeatures: ["valueGenerics"]
Copy link
Member

Choose a reason for hiding this comment

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

Should we use ExperimentalFeatures enum cases here instead of spelling out the feature names?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I guess that's fine for tests since we don't have to be worried about SPI leakage there. Updated.

@allevato allevato force-pushed the experimental-features branch 2 times, most recently from cd992a8 to e0009ed Compare November 11, 2024 13:39
Comment on lines 23 to 26
public func parseForTesting(
source: String,
experimentalFeatures: Parser.ExperimentalFeatures
) -> SourceFileSyntax {
Copy link
Member

Choose a reason for hiding this comment

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

Do we need this still? I think there’s a Parser.parse function that does exactly this now that we don’t need to map experimental features anymore.

Copy link
Member Author

Choose a reason for hiding this comment

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

Since the Parser.parse overload that takes experimental features only takes a UTF-8 pointer, I mainly kept it to tidy up the handful of call sites in tests where we would otherwise have to repeat the "make a mutable copy of the string, convert it to a UTF-8 pointer, and then parse it" dance.

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 see. I would make it an extension of Parser then.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

@allevato allevato force-pushed the experimental-features branch from e0009ed to ebc8bce Compare November 12, 2024 16:11
…arser.

Also add a couple small tests for value generics to exercise the capability in
tests.

Fixes swiftlang#875.
@allevato allevato force-pushed the experimental-features branch from ebc8bce to fee42c9 Compare November 12, 2024 16:28
@ahoppen ahoppen merged commit 2830399 into swiftlang:main Nov 12, 2024
18 of 19 checks passed
@@ -98,6 +98,15 @@ struct LintFormatOptions: ParsableArguments {
)
var followSymlinks: Bool = false

@Option(
Copy link
Member

Choose a reason for hiding this comment

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

Sorry I didn't see hit PR go by, using String here is not great because the user has no idea what options are valid. Instead using a custom type and conforming it to ExpressibleByArgument and CaseIterable will allow ArgumentParser to provide a list of valid options in the help cli and error output.

Copy link
Member Author

Choose a reason for hiding this comment

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

As mentioned in the PR description, we would need Parser.ExperimentalFeatures to become non-SPI for that to be feasible, first. Otherwise, we can't pass the values down through the various API layers because they're public methods and thus would also need to be hidden behind their own SPI.

Copy link
Member

Choose a reason for hiding this comment

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

Ah thats unfortunate, but good to know!

@allevato allevato deleted the experimental-features branch November 12, 2024 18:00
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.

Add cli options for passing experimental features to swift-syntax
3 participants