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

Fix #18234: Warn of identifiers with $ in their name #18563

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

fernandofpd
Copy link

@fernandofpd fernandofpd 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

@mbovel mbovel requested a review from szymon-rd September 18, 2023 12:35
@@ -36,6 +36,7 @@ class Compiler {
List(new Parser) :: // Compiler frontend: scanner, parser
List(new TyperPhase) :: // Compiler frontend: namer, typer
List(new CheckUnused.PostTyper) :: // Check for unused elements
List(new CheckDollarInIdentifier) :: // 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

class CheckDollarInIdentifier extends MiniPhase:
import CheckDollarInIdentifier.ShouldNotContainDollarSignError

override def phaseName: 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


override def phaseName: String = CheckUnused.phaseNamePrefix

override def description: 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

override def description: String = CheckUnused.description

override def isRunnable(using Context): 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 @@
//> using options -Xfatal-warnings -Wdollar-check

val goodVal = 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-rd szymon-rd Sep 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

mbovel commented Jan 14, 2024

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

@mbovel mbovel requested a review from szymon-rd January 14, 2024 15:45
@som-snytt
Copy link
Contributor

The "Dollar Check" sounds like the storefront in the strip mall where you can buy things for a dollar with a loan against your paycheck.

@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

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.

Enforce spec on identifiers containing $s
5 participants