❯ scala
WARNING: A terminally deprecated method in sun.misc.Unsafe has been called
WARNING: sun.misc.Unsafe::objectFieldOffset has been called by scala.runtime.LazyVals$ (file:/home/spam/.sdkman/candidates/scala/3.6.4/maven2/org/scala-lang/scala3-library_3/3.6.4/scala3-library_3-3.6.4.jar)
WARNING: Please consider reporting this to the maintainers of class scala.runtime.LazyVals$
WARNING: sun.misc.Unsafe::objectFieldOffset will be removed in a future release
WARNING: A restricted method in java.lang.System has been called
WARNING: java.lang.System::load has been called by org.jline.nativ.JLineNativeLoader in an unnamed module (file:/home/spam/.sdkman/candidates/scala/3.6.4/maven2/org/jline/jline-native/3.27.1/jline-native-3.27.1.jar)
WARNING: Use --enable-native-access=ALL-UNNAMED to avoid a warning for callers in this module
WARNING: Restricted methods will be blocked in a future release unless native access is enabled
Welcome to Scala 3.6.4 (24, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.
scala>
Keep in mind scala.runtime.LazyVals$ also is accessed by libraries that have been compiled with an older Scala version. So even though your project may be using a Scala version which does not longer use sun.misc.Unsafe for LazyVals, your project may use libraries that where compiled with an older Scala version, which still access the legacy API provided by scala.runtime.LazyVals$that is using sun.misc.Unsafe.
To find out the culprit, you could use –sun-misc-unsafe-memory-access=debug (as per JEP 498).
For example, using scala-cli
$ scala-cli --java-opt --sun-misc-unsafe-memory-access=debug foo.sc
...
WARNING: sun.misc.Unsafe::compareAndSwapObject called by scala.runtime.LazyVals$ (file:~/.cache/coursier/v1/https/repo1.maven.org/maven2/org/scala-lang/scala3-library_3/3.7.4/scala3-library_3-3.7.4.jar)
at scala.runtime.LazyVals$.objCAS(LazyVals.scala:105)
at org.rogach.scallop.Scallop.getHelpOption$lzyINIT1(Scallop.scala:528)
at org.rogach.scallop.Scallop.getHelpOption(Scallop.scala:521)
at org.rogach.scallop.ScallopHelpFormatter.$anonfun$3(ScallopHelpFormatter.scala:64)
at scala.Option.getOrElse(Option.scala:201)
at org.rogach.scallop.ScallopHelpFormatter.getHelpLine(ScallopHelpFormatter.scala:64)
at org.rogach.scallop.ScallopHelpFormatter.getOptionLines(ScallopHelpFormatter.scala:35)
at org.rogach.scallop.ScallopHelpFormatter.getOptionsHelp(ScallopHelpFormatter.scala:22)
at org.rogach.scallop.ScallopHelpFormatter.formatHelp(ScallopHelpFormatter.scala:10)
at org.rogach.scallop.ScallopHelpFormatter.getSubcommandOptionsHelp(ScallopHelpFormatter.scala:158)
at org.rogach.scallop.ScallopHelpFormatter.getSubcommandHelp(ScallopHelpFormatter.scala:142)
at org.rogach.scallop.ScallopHelpFormatter.getLongSubcommandsHelp$$anonfun$1(ScallopHelpFormatter.scala:123)
at scala.collection.immutable.List.map(List.scala:251)
at org.rogach.scallop.ScallopHelpFormatter.getLongSubcommandsHelp(ScallopHelpFormatter.scala:122)
at org.rogach.scallop.ScallopHelpFormatter.getSubcommandsHelp(ScallopHelpFormatter.scala:103)
at org.rogach.scallop.ScallopHelpFormatter.formatHelp(ScallopHelpFormatter.scala:12)
at org.rogach.scallop.Scallop.help(Scallop.scala:441)
at org.rogach.scallop.ScallopConfBase.getHelpString(ScallopConfBase.scala:1050)
at foo$_.<init>(foo.sc:90)
at foo$.script$lzyINIT1(foo.sc:164)
at foo_sc$.script(foo.sc:164)
at foo_sc$.main(foo.sc:168)
at foo_sc.main(foo.sc)
In this example, even though the main project uses a fairly modern Scala version (3.7.4), the Scallop library still causes this warning.
I’m using Scala 3.7.4 (21.0.9, Java OpenJDK 64-Bit Server VM) on macOS Sequoia, with the newer Apple M4 (ARM) chip.
When I upgraded my coursier I got this warning output with scala.runtime.LazyVals. Just adding something to this conversation with no personal intent in following up. I am a lightweight Java user and I believe that I’ve only used Scala once on my MacBook Pro. After these warning messages appeared, my system’s homebrew ran “brew cleanup coursier” and therefore I think everything is fine on my end:
I am still getting the warning in Scala 3.8.1, JDK 24
WARNING: A terminally deprecated method in sun.misc.Unsafe has been called
WARNING: sun.misc.Unsafe::objectFieldOffset has been called by scala.runtime.LazyVals$ (file:/home/spam/.sdkman/candidates/scala/3.8.1/maven2/org/scala-lang/scala-library/3.8.1/scala-library-3.8.1.jar)
WARNING: Please consider reporting this to the maintainers of class scala.runtime.LazyVals$
WARNING: sun.misc.Unsafe::objectFieldOffset will be removed in a future release
Does 3.8.1 also need the flag? I think it’s -Yfuture-lazy-vals? Is that right?
//> using scala 3.8.1
//> using toolkit 0.8.0
//> using scalacOption -Yfuture-lazy-vals
No, that’s a flag intended for 3.3 LTS. The warnings are due to usage dependencies using versions older then Scala 3.7. Old lazy vals implementation is still in use by the dependencies on the classpath, eg. toolkit.
See this part of release blogpost related to JDK 26 support Scala 3.8 # Other Notable Changes, especially the linked talk to Łukasz Bialy’s idea and explaining the problem - in long run we’re planing to have a dedicated JVM agent build in into major Scala build tools, that would rewrite usages of old Lazy Vals to make old dependencies usable. The plan is have it before stable before JDK 26 (when it’s starting to deny access to sun misc unsafe) and fully adapted in build tools before next LTS (JDK 29 in 2027)
That’s a complex problem that would most likely have a dedicated blogpost to explain