Skip to content

Warn about dollars in names of definitions unless backticked#24690

Open
som-snytt wants to merge 4 commits intoscala:mainfrom
som-snytt:issue/18234-dollar-idents
Open

Warn about dollars in names of definitions unless backticked#24690
som-snytt wants to merge 4 commits intoscala:mainfrom
som-snytt:issue/18234-dollar-idents

Conversation

@som-snytt
Copy link
Contributor

@som-snytt som-snytt commented Dec 7, 2025

Warns on definitions named with embedded dollars, which can be written in backquotes to be accepted quietly.

Fixes #18234

Test from #18563

@sjrd
Copy link
Member

sjrd commented Dec 7, 2025

which should be written in backquotes

They should not be written at all, in fact. ;)

@som-snytt som-snytt force-pushed the issue/18234-dollar-idents branch from 965c5f3 to 3690e96 Compare December 7, 2025 13:32
@som-snytt
Copy link
Contributor Author

som-snytt commented Dec 7, 2025

This proposal aims to accommodate the two views, which are not entirely incompatible, that strict enforcement is required and yet Java doesn't need it so why does Scala?, where the question is posed with a large dosage of skepticism about warnings.

Here, there is no forking compiler option. Definitions with suspect names are disallowed, with a migration warning now and an error later. However, the workaround, to permit the identifier in backquotes, is quite natural and pleasant, as we have all come to savor backquotes in new syntactic contexts. Backquotes are not required for references (usages).

CB c includes the original izumi-reflect example.

A rewrite will be offered in a follow-up. The corner case is pattern variables, where it's not sufficient to simply wrap the identifier in backquotes.

@som-snytt som-snytt force-pushed the issue/18234-dollar-idents branch 2 times, most recently from 3fe96a5 to 9754e00 Compare December 7, 2025 20:30
@som-snytt som-snytt marked this pull request as ready for review December 8, 2025 18:13
@som-snytt som-snytt requested a review from a team as a code owner December 8, 2025 18:13
@Gedochao Gedochao requested a review from sjrd December 10, 2025 10:05
case GivenSyntax extends MigrationVersion(future, future)
case ImplicitParamsWithoutUsing extends MigrationVersion(`3.7`, future)
case Scala2Implicits extends MigrationVersion(future, future)
case IdentifierDollars extends MigrationVersion(`3.8`, `3.9`)
Copy link
Member

Choose a reason for hiding this comment

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

@Gedochao What's the warning policy for the feature freeze?

Should this be

Suggested change
case IdentifierDollars extends MigrationVersion(`3.8`, `3.9`)
case IdentifierDollars extends MigrationVersion(`3.10`, `3.11`)

at this point?

Or can we get away with

Suggested change
case IdentifierDollars extends MigrationVersion(`3.8`, `3.9`)
case IdentifierDollars extends MigrationVersion(`3.8`, `3.10`)

even though the warning would then be introduced in a patch version?

Copy link
Contributor

Choose a reason for hiding this comment

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

my gut says the former (3.10, 3.11), but I probably could be convinced otherwise.
We haven't had a precedent for this, I guess.
let's discuss it on today's Scala Core.

@Gedochao Gedochao added the stat:needs decision Some aspects of this issue need a decision from the maintainance team. label Dec 10, 2025
@som-snytt som-snytt force-pushed the issue/18234-dollar-idents branch from 9754e00 to 8891961 Compare December 22, 2025 18:07
@som-snytt
Copy link
Contributor Author

My favorite of the tests

class Cla$$( // error

@prolativ
Copy link
Contributor

How would that work for $ as the name of an interpolator, e.g. for expressions like $"foo" in Apache Spark DSL? Usage of backticks doesn't even seem to be allowed by the parser.
Also asking from my personal interest about how this would affect users of https://github.com/VirtusLab/iskra (and potentially other DSL libraries using a similar syntactical pattern). Writing `$` would make the usage very inconvenient, killing productivity and making code extremely cluttered. Using a different placeholder character instead of $ would be technically doable but would destroy the visual/conceptual link with the original Spark syntax

@sjrd
Copy link
Member

sjrd commented Dec 30, 2025

All of those were never supposed to use $ in identifiers.

The only valid use cases are specifically to reproduce an internal name generated by the compiler for obscure binary compatibility reasons. Using backticks is not the "blessed" way of using $. It's the way to silence the warning when you know 100% what you're doing.

@prolativ
Copy link
Contributor

Or was I too hasty to complain? The spec says User programs should not **define** identifiers which contain ‘$’ characters
So I would just need to use backticks when defining $ and the users will not get any warning/errors on usages, right?

@sjrd
Copy link
Member

sjrd commented Dec 30, 2025

They won't get any errors, but you still shouldn't define such identifiers. Unless you emulate the compiler's binary encoding and you know what you are doing.

@som-snytt
Copy link
Contributor Author

som-snytt commented Dec 30, 2025

@prolativ That is the intention here.

Warns on definitions named with embedded dollars

Backticks are not an absolute escape hatch because (per spec) it's implementation- or platform-dependent what can be permitted. In a separate issue, I'm addressing funky package names.

For my benefit, I checked the specese: "host systems may impose some restrictions". (The circumlocution "host system" seems to say the compiler is not required to reject illegal identifiers, but the runtime may crash.)

That said, I am not a hard-liner, through also not an arbiter. I considered special-casing lone dollar, since it encodes nothing. I was too lazy, however.

@som-snytt som-snytt force-pushed the issue/18234-dollar-idents branch from 8891961 to ee3c07d Compare February 9, 2026 18:35
@som-snytt
Copy link
Contributor Author

som-snytt commented Feb 9, 2026

Demoted to "never error" because of the bureaucratic overhead.

Folks who want an error can ask for it in the usual way, viz., -Wconf.

A patch is offered on migration. Maybe it should always emit the patch? since that is a quickfix.

As noted above, edge case case x$1 => is not quickfixable, though it can be rewritten

case `x$1` @ _ =>

@som-snytt som-snytt force-pushed the issue/18234-dollar-idents branch from ee3c07d to ebea5b2 Compare February 9, 2026 19:04
@som-snytt som-snytt marked this pull request as draft February 10, 2026 05:27
@som-snytt
Copy link
Contributor Author

som-snytt commented Feb 10, 2026

Had a good idea to check in Namer, but the completer loses a bit of context.

Edit: the special case is module names, which (it turns out) are a special kind. Avoid warning for case accessors, since it suffices to check the parameter. Don't use namePos, since that is for well-formed names and always drops a dollar suffix; instead, just calculate an error position.

Possible todo is to fix namePos and other usages of dropping the module suffix, which is valid only for ModuleClassName. (stripModuleSuffix excludes it but doesn't check for it.)

@som-snytt som-snytt force-pushed the issue/18234-dollar-idents branch from 42f43fa to 1716924 Compare February 14, 2026 22:38
@som-snytt som-snytt removed the stat:needs decision Some aspects of this issue need a decision from the maintainance team. label Feb 14, 2026
@som-snytt som-snytt changed the title Dollars in idents Warn about dollars in names of definitions unless backticked Feb 14, 2026
@som-snytt som-snytt force-pushed the issue/18234-dollar-idents branch from 1716924 to 5e29ee5 Compare February 18, 2026 04:34
@som-snytt som-snytt force-pushed the issue/18234-dollar-idents branch from 5e29ee5 to a3e2e97 Compare February 19, 2026 00:24
@som-snytt som-snytt marked this pull request as ready for review February 19, 2026 00:27
@Gedochao Gedochao requested a review from sjrd February 19, 2026 06:36
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

4 participants

Comments