-
Notifications
You must be signed in to change notification settings - Fork 439
Sanitize dollar identifiers with leading zeros in Identifier
.
#2891
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
Conversation
@swift-ci Please test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How did you find that, Jakub?
Sources/SwiftSyntax/Identifier.swift
Outdated
/// | ||
/// - Precondition: `staticString` is a canonical identifier i.e. doesn't | ||
/// use backticks and is not a dollar identifier with leading zeros. | ||
public init(staticString: StaticString) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What’s the reason why you added the staticString
argument label?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed this used to produce a compilation error
let identifier = Identifier("smth") // Error: Ambiguous use of 'init(_:)'
Interestingly, these two worked absolutely fine
let x: StaticString = "smth"
let identifier1 = Identifier(x)
let identifier2: Identifier = Identifier("smth")
It seemed to me a bit odd overloading the initializer worked like that in this case. Is there a reason where a string literal could be used with the other init?(_ token: TokenSyntax)
?
This behavior doesn't happen with an explicit argument label so that's why I changed that. Do you think there could be a better way to fix this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I see. TokenSyntax
is expressible by string literal as well, that’s why it’s ambiguous.
I’m not a fan of the staticString
label. How about we use the argument label to convey that the string needs to be canonical? That way a user will get the overload via TokenSyntax
that canonicalizes the name by default and they need to opt into this faster but more restrictive overload.
Ie. change it to init(canonicalName:)
or something of the sort.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh that makes sense why it didn't work with string literals, thanks for explanation. I also like the idea of init(canonicalName:)
. I changed it.
We’ve discovered this with @DougGregor when validating
For a lookup with a particular identifier, it does introduce a specific dollar identifier name ( |
@swift-ci Please test Windows |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks reasonable to me, thank you
…ier.isDollarIdentifier` behavior for backticked identifiers.
@swift-ci Please test |
@swift-ci Please test Windows |
@swift-ci Please test Windows |
1 similar comment
@swift-ci Please test Windows |
This PR introduces sanitization of dollar identifiers with leading zeros to
Identifier
.The compiler doesn't treat dollar identifiers with leading zeros differently than the "canonical" ones, so for the following piece of code:
The result will be:
This PR will also enable
SwiftLexicalLookup
to properly handle this edge case when introducing dollar identifiers as names in closures.