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

Archie Cobbs acobbs at openjdk.org
Tue Oct 8 22:19:01 UTC 2024


On Tue, 8 Oct 2024 21:19:06 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:

> could we simply re-force the effectively final flag of a loop variable before the loop body?

That's perfectly natural idea and at first I even tried it :)

The problem is that `AssignAnalyzer` is where DA/DU analysis happens, "effectively final" is computed, and where `letInit()` lives, but `CaptureAnalysis` is where we actually test for whether a lambda may capture a variable, and those are currently two independent steps, so it can't all be done "inline" in one pass.

Since "effectively final" is a global property of a variable that is independent of location, whereas the property we want to check is more like "effectively final in the associated loop body" (aka. "not reassigned in the loop body"), we need a new flag in order to pass along this new information.

Side note: flags seem in short supply, and both of these flags are only used within `Flow.java`, so instead of using flags we could keep this information private to `Flow.java`, e.g.:

    AssignAnalyzer aa = new AssignAnalyzer();
    aa.analyzeTree(env, make);
    ...
    new CaptureAnalyzer(aa).analyzeTree(env, make);
    ...

This would better follow the good housekeeping principle of limiting state to the minimum possible scope.

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

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


More information about the compiler-dev mailing list