Summary
The Scala specification states
The
$
character is reserved for compiler-synthesized identifiers. User programs should not define identifiers which contain$
characters.
Unfortunately, the use of such identifier in user code can lead to compiler crashes, runtime crashes, strange compilation error, or at worst an unexpected change in behavior.
Only a handful of users know about this restriction. Many libraries have used it in the past without knowing the dangers it poses.
This Pre-SIP proposes a definite solution to this problem.
Motivation
-
Ensure that users do not defined identifiers with undefined behavior without their knowledge.
-
Remove add-hoc (incomplete) patches from the compiler.
Proposed solution
Disallow user-defined identifiers containing $
and give an escape hatch for the few cases where they are needed.
High-level overview
Given that libraries have accidentally used identifiers with $
we need to have a migration path. We should first emit a migration warning when such an identifier is defined. Then in some following version we can disallow them by emitting an error.
At the same time we need to introduce a way to disable there warnings/errors. As there are few cases where we can define them soundly (i.e. in the Scala 2 standard library), we should make this a -Y
compiler flag (-YallowRestrictedCompilerIdentifiers
).
Specification
When parsing the name of a definition, the parser should check the identifiers for $
and emit the warning/error unless -YallowRestrictedCompilerIdentifiers
is enabled.
Compatibility
This will only affect source compatibility for projects that misused identifiers with $
. It will not affect binary compatibility.
Alternatives
In dotty/issues/#18234/comment it was proposed to add this as a linting option (also see dotty/pull/#18563). This would provide the necessary warning, but it does not address the source of the problem. Users that don’t know about this restriction will also not know about the linting option, this will only help experienced users that already know the problem.
Related work
Issue describing this problem: Enforce spec on identifiers containing `$`s · Issue #18234 · lampepfl/dotty · GitHub
A small and incomplete list of issues that this has causes. It is quite difficult to search for these issue in GitHub or Google due the the $
character.