JDK-8300691 - final variables in for loop headers should accept updates
Maurizio Cimadamore
maurizio.cimadamore at oracle.com
Wed Oct 16 20:56:09 UTC 2024
Hi Archie,
looks very good. Only one comment. Here:
|// Check for a possible "dummy variable" declaration if (tree.sym.pos
>= startPos && (tree.sym.owner.kind == MTH || tree.sym.owner.kind ==
VAR) && tree.init instanceof JCIdent) dummyVariables.put(tree.sym, new
DummyVariable(tree.pos(), tree.sym)); |
I think we have to check whether the symbol under tree.init is that of a
local variable. E.g. we don’t want the finder to pick up stuff like:
|String a = b |
Where |b| is a field in the current class.
Maurizio
On 16/10/2024 21:44, Archie Cobbs wrote:
> Hi Maurizio,
>
> Please see
> https://github.com/openjdk/jdk/compare/master...archiecobbs:jdk:dummy-variable-detector?expand=1
> <https://urldefense.com/v3/__https://github.com/openjdk/jdk/compare/master...archiecobbs:jdk:dummy-variable-detector?expand=1__;!!ACWV5N9M2RV99hQ!PvoKchzkcMwwmqdsV9XWt5JA-wk3qU-XscUSdoueEGvgGTk7vKFqEU1JV0jYo3wMu-GvkpqF9U3pblph5Zn2BnccfYMOdQ$>
>
> I made it a note instead of a warning so that builds would not be
> artificially halted.
>
> Here's an example:
>
> $ cat Test.java
> class Test {
> {
> for (int i = 1; i <= 3; i++) {
> int i2 = i;
> Runnable r = () -> System.out.println(i2);
> }
> }
> }
> $ javac Test.java
> flex-test/Test.java:4: Note: detected effectively final "dummy
> variable" i2
> int i2 = i;
> ^
> I *think* the logic is accurate but it could probably use a quick review.
>
> -Archie
>
> On Wed, Oct 16, 2024 at 9:11 AM Archie Cobbs <archie.cobbs at gmail.com>
> wrote:
>
> Hi Maurizio,
>
> On Wed, Oct 16, 2024 at 8:56 AM Maurizio Cimadamore
> <maurizio.cimadamore at oracle.com> wrote:
>
> Could you come up with a javac patch that detects such “dummy”
> variable declarations and prints a note/warning in the
> compiler output?
>
> Excellent idea - I agree this is something worth further
> exploration to better understand.
>
> I will work on it and report back.
>
> -Archie
>
> --
> Archie L. Cobbs
>
>
>
> --
> Archie L. Cobbs
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20241016/b2b58f96/attachment.htm>
More information about the amber-dev
mailing list