Skip to content

Runtime code implementing lazy val should not use sun.misc.Unsafe on Java 9+ (JEP-471) #9013

Open
@smarter

Description

@smarter

Currently, any usage of a lazy val requires getitng an instance of sun.misc.Unsafe at runtime: https://github.com/lampepfl/dotty/blob/43e4bfa5598e9cdbebb1dc56ed25a319d5aa8fbe/library/src/dotty/runtime/LazyVals.scala#L7 which is problematic for various reasons (e.g. usage of a security manager, using Graal Native (#13985)).

On Java 8, there's no good alternative, but on Java 9+ we should be able to replace that using VarHandle.

We should be able to use the same trick used in scala.runtime.Statics to check once at runtime if VarHandle is available to stay compatible with Java 8: https://github.com/scala/scala/blob/a8a726118d06c90b5506f907b1524457c0d401a7/src/library/scala/runtime/Statics.java#L158-L173 (EDIT: actually I don't think this is good enough: using VarHandle would require adding static fields in any class that has a lazy val, so we need to know at compile-time the version of Java we support (via -release/-Xtarget)

Metadata

Metadata

Assignees

Labels

area:transformcompat:javacompat:java:jepIssues corresponding to a JEP (JDK Enhancement Proposal)compat:java:jep:stableIssues corresponding to a JEP which got already delivered as a stable feature in a JDK releaseitype:enhancementneeds-minor-releaseThis PR cannot be merged until the next minor release

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions