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

Archie L. Cobbs duke at openjdk.org
Thu Nov 17 19:11:29 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 incremental webrev excludes the unrelated changes brought in by the merge/rebase. The pull request contains ten additional commits since the last revision:

 - 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

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

Changes:
  - all: https://git.openjdk.org/jdk/pull/10381/files
  - new: https://git.openjdk.org/jdk/pull/10381/files/0081d190..1e38c30f

Webrevs:
 - full: https://webrevs.openjdk.org/?repo=jdk&pr=10381&range=10
 - incr: https://webrevs.openjdk.org/?repo=jdk&pr=10381&range=09-10

  Stats: 243197 lines in 3049 files changed: 117420 ins; 86685 del; 39092 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