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

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:

:check_mark:︎ Resource coursier–jar-launcher Verified 167.2KB/167.2KB
:check_mark:︎ Bottle icu4c@78 (78.2) Downloaded 32.0MB/ 32.0MB
:check_mark:︎ Bottle libsodium (1.0.21) Downloaded 366.0KB/366.0KB
:check_mark:︎ Formula coursier (2.1.25-M23) Verified 27.1MB/ 27.1MB
:check_mark:︎ Bottle libtasn1 (4.21.0) Downloaded 167.2KB/167.2KB
:check_mark:︎ Bottle nss (3.120) Downloaded 6.2MB/ 6.2MB
:check_mark:︎ Bottle poppler (26.01.0) Downloaded 9.6MB/ 9.6MB
==> Upgrading libarchive
3.8.4 → 3.8.5
==> Pouring libarchive–3.8.5.arm64_sequoia.bottle.tar.gz
:beer_mug: /opt/homebrew/Cellar/libarchive/3.8.5: 65 files, 4MB
==> Running brew cleanup libarchive
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP=1.
Hide these hints with HOMEBREW_NO_ENV_HINTS=1 (see man brew).
Removing: /opt/homebrew/Cellar/libarchive/3.8.4… (65 files, 4MB)
Removing: /Users/nick/Library/Caches/Homebrew/libarchive_bottle_manifest–3.8.4… (12.0KB)
Removing: /Users/nick/Library/Caches/Homebrew/libarchive–3.8.4… (1.6MB)
==> Upgrading sqlite
3.51.1 → 3.51.2
==> Pouring sqlite–3.51.2.arm64_sequoia.bottle.tar.gz
:beer_mug: /opt/homebrew/Cellar/sqlite/3.51.2: 13 files, 5.3MB
==> Running brew cleanup sqlite
Removing: /opt/homebrew/Cellar/sqlite/3.51.1… (13 files, 5.3MB)
Removing: /Users/nick/Library/Caches/Homebrew/sqlite_bottle_manifest–3.51.1… (11.4KB)
Removing: /Users/nick/Library/Caches/Homebrew/sqlite–3.51.1… (2.4MB)
==> Upgrading icu4c@78
78.1 → 78.2
==> Pouring icu4c@78–78.2.arm64_sequoia.bottle.tar.gz
:beer_mug: /opt/homebrew/Cellar/icu4c@78/78.2: 279 files, 87.9MB
==> Running brew cleanup icu4c@78
Removing: /opt/homebrew/Cellar/icu4c@78/78.1… (279 files, 87.9MB)
Removing: /Users/nick/Library/Caches/Homebrew/icu4c@78_bottle_manifest–78.1… (9.7KB)
Removing: /Users/nick/Library/Caches/Homebrew/icu4c@78–78.1… (32.0MB)
==> Upgrading coursier/formulas/coursier
2.1.25-M22 → 2.1.25-M23
Picked up _JAVA_OPTIONS: -Duser.home=/Users/nick/Library/Caches/Homebrew/java_cache
Downloaded 1 missing file(s) / 72
Downloaded 2 missing file(s) / 72
Downloaded 3 missing file(s) / 72
Downloaded 4 missing file(s) / 72
Downloaded 5 missing file(s) / 72
Downloaded 6 missing file(s) / 72
Downloaded 7 missing file(s) / 72
Downloaded 8 missing file(s) / 72
Downloaded 9 missing file(s) / 72
Downloaded 10 missing file(s) / 72
Downloaded 11 missing file(s) / 72
Downloaded 12 missing file(s) / 72
Downloaded 13 missing file(s) / 72
Downloaded 14 missing file(s) / 72
Downloaded 15 missing file(s) / 72
Downloaded 16 missing file(s) / 72
Downloaded 17 missing file(s) / 72
Downloaded 18 missing file(s) / 72
Downloaded 19 missing file(s) / 72
Downloaded 20 missing file(s) / 72
Downloaded 21 missing file(s) / 72
Downloaded 22 missing file(s) / 72
Downloaded 23 missing file(s) / 72
Downloaded 24 missing file(s) / 72
Downloaded 25 missing file(s) / 72
Downloaded 26 missing file(s) / 72
Downloaded 27 missing file(s) / 72
Downloaded 28 missing file(s) / 72
Downloaded 29 missing file(s) / 72
Downloaded 30 missing file(s) / 72
Downloaded 31 missing file(s) / 72
Downloaded 32 missing file(s) / 72
Downloaded 33 missing file(s) / 72
Downloaded 34 missing file(s) / 72
Downloaded 35 missing file(s) / 72
Downloaded 36 missing file(s) / 72
Downloaded 37 missing file(s) / 72
Downloaded 38 missing file(s) / 72
Downloaded 39 missing file(s) / 72
Downloaded 40 missing file(s) / 72
Downloaded 41 missing file(s) / 72
Downloaded 42 missing file(s) / 72
Downloaded 43 missing file(s) / 72
Downloaded 44 missing file(s) / 72
Downloaded 45 missing file(s) / 72
Downloaded 46 missing file(s) / 72
Downloaded 47 missing file(s) / 72
Downloaded 48 missing file(s) / 72
Downloaded 49 missing file(s) / 72
Downloaded 50 missing file(s) / 72
Downloaded 51 missing file(s) / 72
Downloaded 52 missing file(s) / 72
Downloaded 53 missing file(s) / 72
Downloaded 54 missing file(s) / 72
Downloaded 55 missing file(s) / 72
Downloaded 56 missing file(s) / 72
Downloaded 57 missing file(s) / 72
Downloaded 58 missing file(s) / 72
Downloaded 59 missing file(s) / 72
Downloaded 60 missing file(s) / 72
Downloaded 61 missing file(s) / 72
Downloaded 62 missing file(s) / 72
Downloaded 63 missing file(s) / 72
Downloaded 64 missing file(s) / 72
Downloaded 65 missing file(s) / 72
Downloaded 66 missing file(s) / 72
Downloaded 67 missing file(s) / 72
Downloaded 68 missing file(s) / 72
Downloaded 69 missing file(s) / 72
Downloaded 70 missing file(s) / 72
Downloaded 71 missing file(s) / 72
Downloaded 72 missing file(s) / 72
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:/Users/nick/Library/Caches/Homebrew/java_cache/Library/Caches/Coursier/v1/https/repo1.maven.org/maven2/org/scala-lang/scala3-library_3/3.3.6/scala3-library_3-3.3.6.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
Picked up _JAVA_OPTIONS: -Duser.home=/Users/nick/Library/Caches/Homebrew/java_cache
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:/Users/nick/Library/Caches/Homebrew/java_cache/Library/Caches/Coursier/v1/https/repo1.maven.org/maven2/org/scala-lang/scala3-library_3/3.3.6/scala3-library_3-3.3.6.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
:beer_mug: /opt/homebrew/Cellar/coursier/2.1.25-M23: 7 files, 80.1MB, built in 2 seconds
==> Running brew cleanup coursier
Removing: /opt/homebrew/Cellar/coursier/2.1.25-M22… (7 files, 80.1MB)
Removing: /Users/nick/Library/Caches/Homebrew/coursier–2.1.25-M22.gz… (27MB)

The new implementation of lazy vals will be available in 3.8.0 version and in 3.3.8 (behind a flag). Any previous versions will have the warning.

2 Likes

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

2 Likes

Oh I see, thanks for the explanation.
At least I don’t see it on the REPL anymore :grinning_face_with_smiling_eyes:

❯ scala
Welcome to Scala 3.8.1 (24.0.2, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.
                                                                                                
scala>