-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
base: main
Are you sure you want to change the base?
Conversation
@szymon-rd here's my PR from the spree |
@@ -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 $ |
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.
Please try putting it in the same list as CheckUnused.PostTyper
. That way it will run in the same megaphase.
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.
Added it to the same list
class CheckDollarInIdentifier extends MiniPhase: | ||
import CheckDollarInIdentifier.ShouldNotContainDollarSignError | ||
|
||
override def phaseName: String = CheckUnused.phaseNamePrefix |
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.
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.
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.
fixed
|
||
override def phaseName: String = CheckUnused.phaseNamePrefix | ||
|
||
override def description: String = CheckUnused.description |
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.
Similarly, CheckDollarInIdentifier
should have its own description. It can be something really short and simple.
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.
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 |
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.
Better to add a check if it's not java source (like in CheckUnused)
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.
Added
@@ -0,0 +1,38 @@ | |||
//> using options -Xfatal-warnings -Wdollar-check | |||
|
|||
val goodVal = 1 |
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.
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
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.
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).
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 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.
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.
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
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 |
@fernandofpd do you remember why the CI was failing? Could you try to re-run it? |
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. |
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 |
The Scala spect states:
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
Wdollar-check
to enable this warning$
Fixes #18234