Skip to content

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

Merged
merged 2 commits into from
Nov 12, 2024

Conversation

MAJKFL
Copy link
Contributor

@MAJKFL MAJKFL commented Nov 6, 2024

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:

var foo = {
  print($0, $00000000, $1, $00001)
}

foo(4, 2)

The result will be:

4 4 2 2

This PR will also enable SwiftLexicalLookup to properly handle this edge case when introducing dollar identifiers as names in closures.

@MAJKFL
Copy link
Contributor Author

MAJKFL commented Nov 6, 2024

@swift-ci Please test

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.

How did you find that, Jakub?

///
/// - 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) {
Copy link
Member

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?

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 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?

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. 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.

Copy link
Contributor Author

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.

@MAJKFL
Copy link
Contributor Author

MAJKFL commented Nov 6, 2024

How did you find that, Jakub?

We’ve discovered this with @DougGregor when validating SwiftLexicalLookup against ASTScope. We were trying out different edge cases and noticed this behavior.

SwiftLexicalLookup handles dollar identifiers slightly differently for anonymous lookup than the compiler. When encountering a closure without parameters, the lookup just prompts clients with LookupResult.mightIntroduceDollarIdentifiers result flag.

For a lookup with a particular identifier, it does introduce a specific dollar identifier name (LookupName.dollarIdentifier), and that’s where we need its canonical representation to not introduce a monstrosity such as $000000000042.

@MAJKFL
Copy link
Contributor Author

MAJKFL commented Nov 6, 2024

@swift-ci Please test Windows

Copy link
Member

@DougGregor DougGregor left a 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.
@MAJKFL
Copy link
Contributor Author

MAJKFL commented Nov 12, 2024

@swift-ci Please test

@MAJKFL
Copy link
Contributor Author

MAJKFL commented Nov 12, 2024

@swift-ci Please test Windows

@ahoppen
Copy link
Member

ahoppen commented Nov 12, 2024

@swift-ci Please test Windows

1 similar comment
@DougGregor
Copy link
Member

@swift-ci Please test Windows

@DougGregor DougGregor merged commit 36f57a4 into swiftlang:main Nov 12, 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.

3 participants