Skip to content

Wrap all names containing $ reserved character in backticks for all script wrappers - #4378

Merged
Gedochao merged 1 commit into
VirtusLab:mainfrom
Gedochao:maintenance/dollar-script-wrapper-backticks
Jul 17, 2026
Merged

Wrap all names containing $ reserved character in backticks for all script wrappers#4378
Gedochao merged 1 commit into
VirtusLab:mainfrom
Gedochao:maintenance/dollar-script-wrapper-backticks

Conversation

@Gedochao

@Gedochao Gedochao commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Fixes #4374
This is an alternative to #4375, as per #4375 (comment)

What's this about

We no longer can just use $ in field names in the *.sc script wrapper, as otherwise on Scala 3.9+ (currently rc/nightly) we get:

[warn] .../snippet.scala:12:15
[warn] The identifier `args$opt0` should not contain `$`, which is reserved for internal compiler use.
[warn]   private var args$opt0 = Option.empty[Array[String]]
[warn]               ^^^^^^^^^
[warn] .../snippet.scala:3:13
[warn] The identifier `snippet$_` should not contain `$`, which is reserved for internal compiler use.
[warn] final class snippet$_ {
[warn]             ^^^^^^^^^
[warn] .../snippet.scala:13:7
[warn] The identifier `args$set` should not contain `$`, which is reserved for internal compiler use.
[warn]   def args$set(args: Array[String]): Unit = {
[warn]       ^^^^^^^^
[warn] .../snippet.scala:16:7
[warn] The identifier `args$opt` should not contain `$`, which is reserved for internal compiler use.
[warn]   def args$opt: Option[Array[String]] = args$opt0
[warn]       ^^^^^^^^
[warn] .../snippet.scala:17:7
[warn] The identifier `args` should not contain `$`, which is reserved for internal compiler use.
[warn]   def args$: Array[String] = args$opt.getOrElse {
[warn]       ^^^^^

What changes

Rather than migrating the wrapper off-$, this approach wraps everything in backticks.

For a given script:

// hello.sc
println("Hello")
println(args.head)

The under-the-hood generated code wrapper looked roughly like this:

final class hello$_ {
  def args = hello_sc.args$
  def scriptPath = """hello.sc"""
  // ... your script code here ...
}
object hello_sc {
  private var args$opt0 = Option.empty[Array[String]]
  def args$set(args: Array[String]): Unit = { args$opt0 = Some(args) }
  def args$opt: Option[Array[String]] = args$opt0
  def args$: Array[String] = args$opt.getOrElse {
    sys.error("No arguments passed to this script")
  }
  lazy val script = new hello$_
  def main(args: Array[String]): Unit = {
    args$set(args)
    val _ = script.hashCode()
  }
}
export hello_sc.script as `hello`

After this change, this migrates to:

final class `hello$_` {
  def args = `hello_sc.args$`
  def scriptPath = """hello.sc"""
  // ... your script code here ...
}
object hello_sc {
  private var `args$opt0` = Option.empty[Array[String]]
  def `args$set`(args: Array[String]): Unit = { `args$opt0` = Some(args) }
  def `args$opt`: Option[Array[String]] = `args$opt0`
  def `args$`: Array[String] = `args$opt`.getOrElse {
    sys.error("No arguments passed to this script")
  }
  lazy val script = new `hello$_`
  def main(args: Array[String]): Unit = {
    `args$set`(args)
    val _ = script.hashCode()
  }
}
export hello_sc.script as `hello`

Checklist

  • tested the solution locally and it works
  • ran the code formatter (scala-cli fmt .)
  • ran scalafix (./mill -i __.fix)

How much have your relied on LLM-based tools in this contribution?

extensively, Cursor + Claude

How was the solution tested?

Included new automated test ensuring the warning is not there

@Gedochao
Gedochao force-pushed the maintenance/dollar-script-wrapper-backticks branch from 34be87c to 627eaf5 Compare July 16, 2026 13:33
@Gedochao
Gedochao marked this pull request as ready for review July 17, 2026 09:01
@Gedochao
Gedochao requested review from tgodzik and zielinsky July 17, 2026 09:01
@Gedochao
Gedochao merged commit 47498cf into VirtusLab:main Jul 17, 2026
153 of 156 checks passed
@Gedochao
Gedochao deleted the maintenance/dollar-script-wrapper-backticks branch July 17, 2026 13:17
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.

Scala 3.9+ warns about $ in identifiers generated by *.sc script wrappers

2 participants