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

Archie L. Cobbs duke at openjdk.org
Tue Oct 18 17:14:49 UTC 2022


> (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 pull request now contains six commits:

 - 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

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

Changes: https://git.openjdk.org/jdk/pull/10381/files
 Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=10381&range=07
  Stats: 37 lines in 5 files changed: 37 ins; 0 del; 0 mod
  Patch: https://git.openjdk.org/jdk/pull/10381.diff
  Fetch: git fetch https://git.openjdk.org/jdk pull/10381/head:pull/10381

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


More information about the compiler-dev mailing list