Emit method reference lambdas without helper method#9022
Merged
dwijnand merged 1 commit intoscala:2.12.xfrom Jul 27, 2020
Merged
Emit method reference lambdas without helper method#9022dwijnand merged 1 commit intoscala:2.12.xfrom
dwijnand merged 1 commit intoscala:2.12.xfrom
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
adriaanm
reviewed
Jun 5, 2020
lrytz
reviewed
Jun 5, 2020
Member
lrytz
left a comment
There was a problem hiding this comment.
maybe add some tests around serialization, ensure that the generated $deserializeLambda$ works as intended?
src/compiler/scala/tools/nsc/backend/jvm/BCodeBodyBuilder.scala
Outdated
Show resolved
Hide resolved
src/compiler/scala/tools/nsc/backend/jvm/BCodeBodyBuilder.scala
Outdated
Show resolved
Hide resolved
a8890ff to
75667a5
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
viktorklang
reviewed
Jul 17, 2020
This comment has been minimized.
This comment has been minimized.
lrytz
approved these changes
Jul 24, 2020
Member
lrytz
left a comment
There was a problem hiding this comment.
LGTM!
I guess t8549.scala failing is OK / expected?
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Under -Ydelambdafy:method-ref, when a lambda is just invoking a method (i.e. is "just a method reference"), such as `foo()` or `x => bar(x)`, then rather than emit an `invokedynamic` to a helper method that invokes the target method, emit an `invokedynamic` to the target method directly.
retronym
reviewed
Aug 25, 2020
| val lambdaTarget = originalTarget.attachments.get[JustMethodReference].map(_.lambdaTarget).getOrElse(originalTarget) | ||
| def asmType(sym: Symbol) = classBTypeFromSymbol(sym).toASMType | ||
|
|
||
| val isInterface = lambdaTarget.owner.isTrait |
Member
There was a problem hiding this comment.
@dwijnand This needs to be generalized to isTraitOrInterface now that lambda implementation methods may be owned by Java interfaces.
Member
|
Removing the release-notes label as this is still experimental and not something that most users would need. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Under -Ydelambdafy:method-ref, when a lambda is just invoking a method
(i.e. is "just a method reference"), such as
foo()orx => bar(x),(and other caveats around specialisation, boxing concerns for primitives
and its value classes, arrays, etc) then rather than emit an
invokedynamicto a helper method that invokes the target method, emitan
invokedynamicto the target method directly.$deserializeLambda$works as intendedExplanation
Note that this is opt-in, with
-Ydelambdafy:method-ref. The reason it's opt-in is because there's an assumption (an "invariant") in serialisation: if you serialise the lambda from one version of some code (v1) then deserialising it in a later version (v2) should run the new (v2) behaviour.For example, if you serialise the lambda
(x => x.toString())then deserialise it when the code is(x => x.toString().toUpperCase()), then the result should be upper-cased. Under-Ydelambdafy:method-refthe deserialised lambda will continue to just be a reference to Object'stoStringmethod. Also worth mentioning that in the same example, but with the versions swapped, the lambda will be a reference an$anonfun$...method that no longer exists in v2... which means that it will throw aLinkageErrorat runtime.