Skip to content

Conversation

@fernandofpd
Copy link

@fernandofpdfernandofpd commented Sep 17, 2023

The Scala spect states:

The ‘$’ character is reserved for compiler-synthesized identifiers. User programs should not define identifiers which contain ‘$’ characters.

We should warn the users if the use an identifier with $s. We should not make it an error as there are some legitimate corner cases where $ in the source code is used (such as in the stdlib and for backward binary compact patches).

This PR add

  • A new flag Wdollar-check to enable this warning
  • A new phase that runs when the above flag is present that produces warnings when identifiers contain $

Fixes#18234

@fernandofpd
Copy link
Author

@szymon-rd here's my PR from the spree

List(newParser) ::// Compiler frontend: scanner, parser
List(newTyperPhase) ::// Compiler frontend: namer, typer
List(newCheckUnused.PostTyper) ::// Check for unused elements
List(newCheckDollarInIdentifier) ::// Warn if identifier contains a dollar sign $
Copy link
Contributor

Choose a reason for hiding this comment

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

Please try putting it in the same list as CheckUnused.PostTyper. That way it will run in the same megaphase.

Copy link
Author

Choose a reason for hiding this comment

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

Added it to the same list

classCheckDollarInIdentifierextendsMiniPhase:
importCheckDollarInIdentifier.ShouldNotContainDollarSignError

overridedefphaseName:String=CheckUnused.phaseNamePrefix
Copy link
Contributor

Choose a reason for hiding this comment

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

The phase name should be declared in the companion object of this phase. it could be just checkDollarInIdentifier. And it's prefix in CheckUnused because there are two different check unused in the pipeline, there is just one of checkDollar, so it can be just phaseName val.

Copy link
Author

Choose a reason for hiding this comment

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

fixed


overridedefphaseName:String=CheckUnused.phaseNamePrefix

overridedefdescription:String=CheckUnused.description
Copy link
Contributor

Choose a reason for hiding this comment

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

Similarly, CheckDollarInIdentifier should have its own description. It can be something really short and simple.

Copy link
Author

Choose a reason for hiding this comment

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

Sorry, I had created those in the companion object, but left these as it was from copying from CheckUnused. I have fixed this now

overridedefdescription:String=CheckUnused.description

overridedefisRunnable(usingContext):Boolean=
super.isRunnable && ctx.settings.WdollarCheck.value
Copy link
Contributor

Choose a reason for hiding this comment

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

Better to add a check if it's not java source (like in CheckUnused)

Copy link
Author

Choose a reason for hiding this comment

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

Added

@@ -0,0 +1,38 @@
//>usingoptions-Xfatal-warnings-Wdollar-check

valgoodVal=1
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add pos tests for unnamed givens (e.g. given String = "foo"), lazy vals, implicit params coming from : syntax (e.g. def foo[T: Showable]), partial function definition

Copy link
Contributor

@szymon-rdszymon-rdSep 19, 2023

Choose a reason for hiding this comment

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

Also some anonymous functions and a companion object. We have to make sure that no synthetic symbols are catched by linting. A good thing is that we may be able to access the sources to confirm if the symbol is present, but we will do it only if necessary (however, I suspect it may be).

Copy link
Author

Choose a reason for hiding this comment

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

I think I have covered all the cases you mentioned now. It actually helped catch a case for anonymous functions. Which I added a fix for.

Copy link
Author

Choose a reason for hiding this comment

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

actually, after adding this phase to the same list as CheckUnused.PostTyper made it be triggered on compilation of the project, and in turn revealed a lot of false positives. I'll try and look into them this week

@szymon-rd
Copy link
Contributor

Thanks for creating this PR! I left some feedback in the comments, overall looks good, but we have to make sure that these synthetic symbols won't cause false positives.

@fernandofpd
Copy link
Author

Thanks for creating this PR! I left some feedback in the comments, overall looks good, but we have to make sure that these synthetic symbols won't cause false positives.

Thanks for reviewing. And sorry it took so long to implement the suggestions

@mbovel
Copy link
Member

@fernandofpd do you remember why the CI was failing? Could you try to re-run it?

@nicolasstucki
Copy link
Contributor

The warning is not enough to fully fix the issue. We might need something stronger. See https://contributors.scala-lang.org/t/pre-sip-disallow-restricted-compiler-identifiers/6553

@som-snyttsom-snytt mentioned this pull request Dec 7, 2025
@som-snytt
Copy link
Contributor

Closing as stale or superseded, but if the other effort is rejected, this lint could be revived.

#24690

I'm sorry I missed the first miniphase to run with CheckUnused.PostTyper.

Sign up for freeto 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.

Enforce spec on identifiers containing $s

5 participants

@fernandofpd@szymon-rd@mbovel@nicolasstucki@som-snytt