JDK 8148716: Variable proxy duplicate

B. Blaser bsrbnd at gmail.com
Wed Feb 8 22:02:39 UTC 2017


Hi,

Javac fails to compile the following example because it creates twice
the same variable proxy (jdk 8148716):

class A {
    public static void main(String... args) {
        new A().a();
    }

    void a() {
        String x = "abc";

        class B {
            public int b() { return x.length(); }
        };

        Runnable r = () -> {
            class C {
                void c() {
                    // free var x in a()
                    System.out.println(new B().b());
                    // free var x in lambda()
                    System.out.println(x.length());
                }
            }
            new C().c();
        };
        r.run();
    }
}

... once for x in a() and once for x in lambda().

The fix below prevents free variable duplicate regardless of the owner
(Lower.FreeVarCollector.addFreeVar()), ensuring no proxy duplication
(see proxies.getSymbolsByName() in Lower.initField()).

Needs to be well tested...

Any comment is welcome!

Thanks,
Bernard

diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java
b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java
@@ -289,7 +289,7 @@
          */
         private void addFreeVar(VarSymbol v) {
             for (List<VarSymbol> l = fvs; l.nonEmpty(); l = l.tail)
-                if (l.head == v) return;
+                if (l.head.name == v.name) return;
             fvs = fvs.prepend(v);
         }


More information about the compiler-dev mailing list