Question About unfamiliar behavior

Hi, i started learning Scala and i was enjoying the theory behind it, then i encountered this Warning about LazyVals$:

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/haitam/.cache/cours
ier/arc/https/github.com/scala/scala3/releases/download/3.8
.3/scala3-3.8.3-x86_64-pc-linux.tar.gz/scala3-3.8.3-x86_64-
pc-linux/maven2/org/scala-lang/scala-library/3.8.3/scala-li
brary-3.8.3.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

Note : it’s not a problem for me, but im just trying to understand the reason behind it

Thanks.

Hmm. Not my area of expertise, but this smells like a JVM compatibility issue. What version of the JVM are you running on?

1 Like

It’s a known thing, don’t worry about it. Happens to me too, any time I use the REPL (I have JVM 24). There is some info here. Basically newer JVM versions restrict sun.misc.unsafe and Scala 3.9 onward will have a new lazy val implementation that won’t use it. Until then we’ll keep seeing the annoying warning :smiley:

2 Likes

I can confirm that the warning is harmless and can be ignored. The Scala 3 team is working on eliminating it.

This is already an FAQ and we’re overdue to properly document the situation. (In addition to the 3.8 release notes, there are various tickets about it, such as Runtime code implementing lazy val should not use sun.misc.Unsafe on Java 9+ (JEP-471) · Issue #9013 · scala/scala3 · GitHub, but it’s too much to expect users to find those tickets and read the discussions on them.)

I will add some remarks about it to the JDK Compatibility page (JDK Compatibility | Scala Documentation) and also to the Scala FAQ (Scala FAQ | Scala Documentation), and I’ll report back here when I’ve done so.

3 Likes

That seems good, but possibly not enough

I think it would be good to either hide this error, or wrap it in “You are using Scala X on JVM Y, this can lead to the following error appearing, if you do not use sun.misc.Unsafe, please ignore it. < link to some information page >”

2 Likes

@Sporarum It isn’t an exception we can catch and customize. The JVM is printing the warning itself, directly, and we have no power over it, to my knowledge, except by avoiding the usage of Unsafe.

As of Scala 3.8.4 we have already eliminated usage of Unsafe in the Scala 3 standard library, but it hasn’t yet been eliminated from a library that the REPL uses. The ticket on that is REPL still prints LazyVal warnings (because of fansi+pprint) · Issue #25508 · scala/scala3 · GitHub and it’s milestoned to be fixed for 3.9.0. (If a fix doesn’t make 3.9.0, it will surely make some 3.9.x release.)

Users may still see the warning if they are using third-party libraries that were built with Scala 3.3 LTS. So this story is going to continue until the entire Scala 3 library ecosystem has been re-published using Scala 3.9 LTS.

Whether the warning @gwynn33 saw happened in the REPL and was caused by 25508, or was caused by a third-party library, I don’t know; we don’t have enough information to determine that.

2 Likes

But that part we could do, no ?
We know at compile-time that outdated lazy vals are used (although potentially not reachable)
So we could have the compiler emit a warning like “It might print that error message, but don’t worry about it”

To add a bit more information - this is the rare situation where JVM itself is going to introduce a breaking change in a new version of the platform. This change is years if not decades in making and given how leveraged the general JVM ecosystem (including loads of Java projects!) is on sun.misc.Unsafe, they are still pushing it forward in time while spooking users with these warnings that can’t be suppressed in any way. We are well-prepared for the moment this change actually arrives in the Scala ecosystem (provided your project does not depend on a Java dependency that needs sun.misc.Unsafe). As @SethTisue correctly stated - to make this issue truly go away we will need to republish everything in the ecosystem on Scala 3.9. As this will take some time we (VirtusLab) have prepared a bytecode patcher utility that can migrate the artifacts of any Scala 3 project (as Scala 2 is unaffected) to Scala 3.8+ lazy val encoding.

2 Likes

Also, if you maintain a library consider using `-Yfuture-lazy-vals` on LTS 3.3, which already migrates to the new lazy val implementation and would avoid the warning.

5 Likes