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

Maurizio Cimadamore mcimadamore at openjdk.org
Wed Oct 9 09:23:56 UTC 2024


On Tue, 8 Oct 2024 19:53:56 GMT, Archie Cobbs <acobbs at openjdk.org> wrote:

> 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);
> }

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java line 3486:

> 3484:             // Collect the for() loop iteration variables with the FOR_LOOP_BODY_MAY_CAPTURE flag
> 3485:             HashSet<VarSymbol> capturableForLoopVars = null;
> 3486:             if (tree.init != null) {

This is a bit unfortunate. In principle, a variable has `FOR_LOOP_BODY_MAY_CAPTURE` should just be  `EFFECTIVELY_FINAL` - at least in the context of the for loop body. I suppose there's still a problem that we'd have to unset `EFFECTIVELY_FINAL` after the loop body... but I wonder, do we care? After all, we have already flow-analyzed the other for-loop parts (condition and step), so perhaps we just set `EFFECTIVELY_FINAL` before scanning the loop body, and leave it there?

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

PR Review Comment: https://git.openjdk.org/jdk/pull/21415#discussion_r1793174708


More information about the compiler-dev mailing list