RFR: 8336492: Regression in lambda serialization [v4]

Maurizio Cimadamore mcimadamore at openjdk.org
Mon Jul 29 13:21:35 UTC 2024


On Mon, 29 Jul 2024 12:35:55 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:

>> This PR consolidates the code for dealing with local captures in both `Lower` and `LambdaToMethod`. It does so by adding a new tree scanner, namely `CaptureScanner`, which is then subclassed by `Lower.FreeVarCollector` and also by the new `LambdaToMethod.LambdaCaptureScanner`.
>> 
>> The main idea behind the new visitor is that we can compute the set of (most) captured locals w/o relying on complex state from `Lower`, and make the logic general enough to work on *any* tree. This can be done by keeping track of all local variable declarations occurring in a given subtree `T`. Then, if `T` contains a reference to a local variable that has not been seen, we can assume that this variable is a captured variable. This simple logic works rather well, and doesn't rely on checking e.g. that the accessed variable has the same owner of the method that owns a local class (which then caused other artifacts such as `Lower::ownerToCopyFreeVarsFrom`, now removed).
>> 
>> The bigger rewrite is the one in `LambdaToMethod`. That class featured a custom visitor called `LambdaAnalyzerPreprocessor` (now removed) which maintains a stack of frames encountered during a visit, and tries to establish which references to local variables needs to be captured by the lambda, as well as whether `this` needs to be referenced by the lambda. Thanks to the new `CaptureScanner` visitor, all this logic can be achieved in a very small subclass of `CaptureScanner`, namely `LambdaCaptureScanner`.
>> 
>> This removes the need to keep track of frames, and other ancillary data structures. `LambdaTranslationContext` is now significantly simpler, and its most important field is a `Map<VarSymbol, VarSymbol>` which maps logical lambda symbols to translated ones (in the generated lambda method). We no longer need to maintain different maps for different kinds of captured symbols.
>> 
>> The code for patching identifiers in a lambda expression also became a lot simpler: we just check if we have pending lambda translation context and, if so, we ask if the identifier symbol is contained in the translation map. Otherwise, we leave the identifier as is.
>> 
>> Fixing the issue referenced by this PR is now very simple: inside `LambdaCaptureScanner`, we just need to make sure that any identifier referring to a captured local field generated by `Lower` is re-captured, and the rest just works. To make the code more robust I've added a new variable flag to denote synthetic captured fields generated by `Lower`.
>> 
>> #### C...
>
> Maurizio Cimadamore has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Simplify and consolidate code to keep track of lambda owners

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java line 1425:

> 1423:             Symbol fakeOwner =
> 1424:                 new MethodSymbol(tree.flags | BLOCK |
> 1425:                     env.info.scope.owner.flags() & STRICTFP, names.empty, fakeOwnerType,

the type was left null here, which caused some crashes in `LambdaToMethod`. There was really no reason for this to be null, except that `ClassWriter` looks at type being null as a test to see if owner is a static/instance init block. But we can use `BLOCK` for that (as `TypeAnnotations` does)

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java line 3576:

> 3574:                         Type clinitType = new MethodType(List.nil(),
> 3575:                                 syms.voidType, List.nil(), syms.methodClass);
> 3576:                         clinit = new MethodSymbol(BLOCK | STATIC | SYNTHETIC | PRIVATE,

Added missing `BLOCK`, so that we're now consistent with what the rest of the code (not only `LambdaToMethod`) expects.

src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassWriter.java line 332:

> 330:         ClassSymbol enclClass = c.owner.enclClass();
> 331:         MethodSymbol enclMethod =
> 332:             ((c.owner.flags() & BLOCK) != 0 // local to init block

new test, which looks at the `BLOCK` flag

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/20349#discussion_r1695218946
PR Review Comment: https://git.openjdk.org/jdk/pull/20349#discussion_r1695220032
PR Review Comment: https://git.openjdk.org/jdk/pull/20349#discussion_r1695221145


More information about the compiler-dev mailing list