Using Scala 3.6.4 with Java 24 gives warnings

❯ 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> 

Is it known, should I report it?

Yes, it’s known issue, it’s going to be fixed in Scala 3.8 which would also increment the minimal version of JDK required by the compiler

6 Likes

To avoid warnings, add --sun-misc-unsafe-memory-access=allow to your jvm launch options.

for scala-cli:
//> using javaOpt --sun-misc-unsafe-memory-access=allow

for mill:
override def forkArgs: T[Seq[String]] = Seq(
“–sun-misc-unsafe-memory-access=allow”,
)

2 Likes

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.

3 Likes

Yes, that’s all correct, we’re working on tool to address this problem

1 Like