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

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


On Tue, 8 Oct 2024 22:16:46 GMT, Archie Cobbs <acobbs at openjdk.org> wrote:

> 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.

I see the problem - thanks for the explanation. Now that I remember more of the code, there's a reason why effective finality is determined as part of DA/DU - that has to do with some enhancements we did in Java 8 to allow for "blank" effectively final variables. That is a variable like this:


int i;
if (cond) {
   i = 2;
} else {
   i = 3;
}


Is considered effectively final (unless is further mutated), even though it lacks an initializer and there is some control flow. So we settled to do that in the two passes you describe.

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

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


More information about the compiler-dev mailing list