RFR: 8043179: Lambda expression can mutate final field [v12]

Vicente Romero vromero at openjdk.org
Tue Mar 21 14:51:52 UTC 2023


On Mon, 19 Dec 2022 15:44:49 GMT, Archie L. Cobbs <duke at openjdk.org> wrote:

>> (Description revised)
>> 
>> JDK-8043176 updated the JLS 16.1.10 to describe how within a lambda body, any variables that were DU before the lambda expression must not be considered DU at the start of the lambda block, because arbitrary code could have executed in the meantime:
>> 
>>>  _No rule allows V to be definitely unassigned before a lambda body. This is by design: a variable that was definitely unassigned before the lambda body may end up being assigned to later on, so we cannot conclude that the variable will be unassigned when the body is executed._
>> 
>> This fix to `Flow.AssignAnalyzer` follows in a straightforward way: mark all variables as not DU before recursing into the lambda body.
>
> Archie L. Cobbs has updated the pull request with a new target base due to a merge or a rebase. The incremental webrev excludes the unrelated changes brought in by the merge/rebase. The pull request contains 10 additional commits since the last revision:
> 
>  - Merge branch 'master' into JDK-8043179
>  - Merge branch 'master' into JDK-8043179
>  - Update failed test output line numbers after removing @author tags.
>  - Remove @author tags from tests.
>  - Add a unit test verifying a change in reported error for final variables.
>    
>    Before this change, compiling this class:
>    
>        class LambdaMutateFinalVar {
>            LambdaMutateFinalVar() {
>                final String x;
>                Runnable r1 = () -> x = "not ok";
>                x = "ok";
>            }
>        }
>    
>    would report this error:
>    
>      local variables referenced from a lambda expression must be final or effectively final
>    
>    That error is not really appropriate; after all, the variable IS final. The
>    real problem is that it can't be assigned from within the lambda because it
>    can't be assumed to be DU.
>    
>    After this change, this error is reported instead:
>    
>      variable x might already have been assigned
>  - Fix previously incorrect logic and update unit tests.
>  - Fix @author in test to match github username per instructions.
>  - Move unit test into a more appropriate subdirectory.
>  - Use /nodynamiccopyright/ for "golden file" test per instructions.
>  - 8043179: Lambda expression can mutate final field

looks good

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

Marked as reviewed by vromero (Reviewer).

PR Review: https://git.openjdk.org/jdk/pull/10381#pullrequestreview-1350669716


More information about the compiler-dev mailing list