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

Improvements to integer literal utilities used in SourceKit-LSP #2626

Merged
merged 3 commits into from
Apr 26, 2024

Conversation

DougGregor
Copy link
Member

SourceKit-LSP's refactoring action has minor extensions for the integer literal utilities. Sink them down here for more general use:

  • IntegerLiteralExprSyntax.Radix becomes CaseIterable
  • IntegerLiteralExprSyntax.Radix gains a "literalPrefix" property to get the string needed to prefix a literal with this radix.

SourceKit-LSP's refactoring action has minor extensions for the integer
literal utilities. Sink them down here for more general use:
* IntegerLiteralExprSyntax.Radix becomes CaseIterable
* IntegerLiteralExprSyntax.Radix gains a "literalPrefix" property to get
  the string needed to prefix a literal with this radix.
@DougGregor
Copy link
Member Author

@swift-ci please test

@DougGregor
Copy link
Member Author

@swift-ci please test

@DougGregor
Copy link
Member Author

@swift-ci please test Windows

1 similar comment
@DougGregor
Copy link
Member Author

@swift-ci please test Windows

@@ -17,7 +17,7 @@ import SwiftSyntax
#endif

extension IntegerLiteralExprSyntax {
public enum Radix {
public enum Radix: CaseIterable {
Copy link
Member

Choose a reason for hiding this comment

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

As for the use-case in SourceKit-LSP, I'd just do:

for radix: IntegerLiteralExprSyntax.Radix in [.binary, .octal, .hex, .decimal] { ... }

This is clearer and more deterministic what it will do, compared to .allCases.

I personally avoid CaseIterable unless there's a strong reason to do. Users cannot control the order (maybe it might not matter in this specific use-case)

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, I'm doing this based on the discussion in swiftlang/sourcekit-lsp#1179 (comment). Code that is working with radix values via the APIs here can do so without having to reason about the particulars of the radix; it can handle all of them. That seems like it makes this a reasonable API to provide.

Copy link
Member

@rintaro rintaro Apr 25, 2024

Choose a reason for hiding this comment

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

Yes I know the discussion. But,

it can handle all of them.

All of current cases. Perhaps in the future, we might introduce a case that has "suffix". e.g. 0nA24nG_r62 (whatever it is). If we only handle known cases [.binary, .octal, .hex, .decimal], the code would probably still work correctly because we'd not change the syntax of the current radix cases. But using .allCases can cause misbehavior.

FWIW #2292, we have a history of removing CaseIterable. I want to be really cautious about re-adding it. IMO, adding CaseIterable as public API should have a super strong reasoning.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it's unlikely to ever cause a problem, given that the literals have been stable for a decade, but neither is it more than an inconvenience if we don't have this. So I've removed it.

@DougGregor
Copy link
Member Author

@swift-ci please test

@DougGregor
Copy link
Member Author

@swift-ci please test Windows

2 similar comments
@DougGregor
Copy link
Member Author

@swift-ci please test Windows

@DougGregor
Copy link
Member Author

@swift-ci please test Windows

@DougGregor DougGregor merged commit df88f3f into swiftlang:main Apr 26, 2024
3 checks passed
@DougGregor DougGregor deleted the integer-literal-utils branch April 26, 2024 16:01
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