Skip to content

Conversation

@WojciechMazur
Copy link

Backports scala#24109 to LTS 3.3, but also keeps existing offset-based implementation.

VarHandles are used if either explicitly targeting JDK 9+ (using --java-output-version) or when using implicit releae-versions and using JDK 9

When using VarHandles additional static class initializers <clint> might be generated, where previously these were not needed (see MiMa filter added for scala.Tuple) however, this change is likely a binary compatible. The JVM owns class initialization and there is never an explicit call site to in user bytecode, so link/load will still succeed.

@WojciechMazur WojciechMazur changed the title Optionally use VarHandles for Lazy Vals when targeting JDK 9 Optionally use VarHandles for Lazy Vals when targeting JDK 9+ Nov 8, 2025
Comment on lines +734 to +735
@tu lazy val MethodHandlesClass: TermSymbol = requiredModule("java.lang.invoke.MethodHandles")
@tu lazy val MethodHandles_lookup: Symbol = MethodHandlesClass.requiredMethod("lookup")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to explicitlly guard these on JDK8: their usage is not reachable on JDK8

Comment on lines +298 to +301
// Check if environment allows VarHandles, cached per run
private var canUseVarHandles: Boolean = compiletime.uninitialized
private var cachedContext: Context = compiletime.uninitialized
def useVarHandles(using Context) = {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's plenty of usages, the check might allocate on each usage mostly due to .toInt or .toIntOption conversions, so let's skip redundant calculations - unlikely to change

@WojciechMazur
Copy link
Author

@jchyb @sjrd what do you think about it? It would not get rid of all unsafe usages (due to dependencies), but we'd at least allow Scala 3.3 LTS users to migrate if they've already dropped JDK 8.

Beside potential extra<clinit> there are no new other visible, binary nor source incompatibilities.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant