RFR: 8043179: Lambda expression can mutate final field

Archie L. Cobbs duke at openjdk.org
Sun Sep 25 00:30:08 UTC 2022


On Wed, 21 Sep 2022 23:03:45 GMT, Archie L. Cobbs <duke at openjdk.org> wrote:

> This patch causes lambdas to be treated like non-constructor methods with respect to field initialization in `Flow.AssignAnalyzer`.
> 
> This fixes [JDK-8043179](https://bugs.openjdk.org/browse/JDK-8043179); the test program in the bug now gives this:
> 
> LambdaFieldInit.java:6: error: variable x might already have been assigned
>         Runnable r1 = () -> x = "hi";
>                             ^
> 
> 
> This also aligns the compiler with the proposed spec fix in [JDK-8043176](https://bugs.openjdk.org/browse/JDK-8043176) and fixes the inconsistency between lambdas and inner class methods shown [here](https://stackoverflow.com/questions/39881295/why-are-java-lambdas-treated-differently-from-nested-classes-with-respect-to-ins).

I've updated this pull request to include a new unit test (apologies for omitting it previously).

Also there are a couple of changes to other unit tests which get broken because this change reflects changes to the JLS spec per [JDK-8043176](https://bugs.openjdk.org/browse/JDK-8043176). I'm a little unclear on precisely what the new behavior is supposed to be, but in any case the actual behavior now makes a lot more sense: basically, everything that happens in a lambda is assumed to happen some indefinite time later in the future (same as if you were instantiating the equivalent local class), so the previous errors expected by `T8024809/SelfInitializerInLambdaTestb.java` regarding instance field initialization are no longer errors.

In summary:
* Added new unit test `LambdaMutateFinalField.java` testing [JDK-8043179](https://bugs.openjdk.org/browse/JDK-8043179) directly.
* Unit test [`lambda/self_initializer/T8024809/SelfInitializerInLambdaTestb.java`](https://github.com/openjdk/jdk/blob/master/test/langtools/tools/javac/lambda/self_initializer/T8024809/SelfInitializerInLambdaTestb.java) is now obsolete. Now it compiles successfully.
* Unit test [`lambda/lambdaExpression/EffectivelyFinal_neg.java`](https://github.com/openjdk/jdk/blob/master/test/langtools/tools/javac/lambda/lambdaExpression/EffectivelyFinal_neg.java) still works, but no longer generates a duplicate error. I think this is a good thing? The expected test output has been updated.

With the above changes now all javac tests should succeed.

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

PR: https://git.openjdk.org/jdk/pull/10381


More information about the compiler-dev mailing list