RFR: 8341782: Allow lamba capture of basic for() loop variables as with enhanced for()

Archie Cobbs acobbs at openjdk.org
Tue Oct 8 20:00:39 UTC 2024


This PR changes the compiler to allow basic `for()` loop iteration variables to be captured by lambdas in the loop body, even when their "effectively final" status is invalidated by modifications in the header of the loop.

This allows code like this to compile:

for (int i = 1; i <= 3; i++) {
    Runnable r = () -> System.out.println(i);
}

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

Commit messages:
 - Loosen conditions required for basic for() loop iteration variables to be captured in the loop body.

Changes: https://git.openjdk.org/jdk/pull/21415/files
  Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=21415&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8341782
  Stats: 258 lines in 5 files changed: 256 ins; 0 del; 2 mod
  Patch: https://git.openjdk.org/jdk/pull/21415.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/21415/head:pull/21415

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


More information about the compiler-dev mailing list