8160156: javac is generating let expressions unnecessarily
authorvromero
Wed, 10 Aug 2016 13:52:02 -0700
changeset 40306 1a0fcaf3f2ed
parent 40305 4262ef5fc5f0
child 40307 3ad1ad0e7bba
child 40308 274367a99f98
8160156: javac is generating let expressions unnecessarily Reviewed-by: mcimadamore Contributed-by: vicente.romero@oracle.com, maurizio.cimadamore@oracle.com
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java
langtools/test/tools/javac/T8160156/LetExpressionsAreUnnecessarilyGeneratedTest.java
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java	Wed Aug 10 10:47:43 2016 -0700
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java	Wed Aug 10 13:52:02 2016 -0700
@@ -3184,11 +3184,13 @@
     }
 
     public void visitAssignop(final JCAssignOp tree) {
-        JCTree lhsAccess = access(TreeInfo.skipParens(tree.lhs));
         final boolean boxingReq = !tree.lhs.type.isPrimitive() &&
             tree.operator.type.getReturnType().isPrimitive();
 
-        if (boxingReq || lhsAccess.hasTag(APPLY)) {
+        AssignopDependencyScanner depScanner = new AssignopDependencyScanner(tree);
+        depScanner.scan(tree.rhs);
+
+        if (boxingReq || depScanner.dependencyFound) {
             // boxing required; need to rewrite as x = (unbox typeof x)(x op y);
             // or if x == (typeof x)z then z = (unbox typeof x)((typeof x)z op y)
             // (but without recomputing x)
@@ -3238,6 +3240,41 @@
         }
     }
 
+    class AssignopDependencyScanner extends TreeScanner {
+
+        Symbol sym;
+        boolean dependencyFound = false;
+
+        AssignopDependencyScanner(JCAssignOp tree) {
+            this.sym = TreeInfo.symbol(tree.lhs);
+        }
+
+        @Override
+        public void scan(JCTree tree) {
+            if (tree != null && sym != null) {
+                tree.accept(this);
+            }
+        }
+
+        @Override
+        public void visitAssignop(JCAssignOp tree) {
+            if (TreeInfo.symbol(tree.lhs) == sym) {
+                dependencyFound = true;
+                return;
+            }
+            super.visitAssignop(tree);
+        }
+
+        @Override
+        public void visitUnary(JCUnary tree) {
+            if (TreeInfo.symbol(tree.arg) == sym) {
+                dependencyFound = true;
+                return;
+            }
+            super.visitUnary(tree);
+        }
+    }
+
     /** Lower a tree of the form e++ or e-- where e is an object type */
     JCExpression lowerBoxedPostop(final JCUnary tree) {
         // translate to tmp1=lval(e); tmp2=tmp1; tmp1 OP 1; tmp2
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/T8160156/LetExpressionsAreUnnecessarilyGeneratedTest.java	Wed Aug 10 13:52:02 2016 -0700
@@ -0,0 +1,98 @@
+/*
+ * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * @test 8160156
+ * @summary javac is generating let expressions unnecessarily
+ * @library /tools/lib
+ * @modules
+ *      jdk.compiler/com.sun.tools.javac.api
+ *      jdk.compiler/com.sun.tools.javac.main
+ * @build toolbox.ToolBox toolbox.JavacTask
+ * @run main LetExpressionsAreUnnecessarilyGeneratedTest
+ */
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import toolbox.JavacTask;
+import toolbox.Task;
+import toolbox.TestRunner;
+import toolbox.ToolBox;
+
+public class LetExpressionsAreUnnecessarilyGeneratedTest extends TestRunner {
+    ToolBox tb;
+
+    public static void main(String... args) throws Exception {
+        new LetExpressionsAreUnnecessarilyGeneratedTest().runTests();
+    }
+
+    public LetExpressionsAreUnnecessarilyGeneratedTest() {
+        super(System.err);
+        tb = new ToolBox();
+    }
+
+    protected void runTests() throws Exception {
+        runTests(m -> new Object[] { Paths.get(m.getName()) });
+    }
+
+    @Test
+    public void testDontGenerateLetExpr(Path testBase) throws Exception {
+        Path src = testBase.resolve("src");
+        tb.writeJavaFiles(src,
+                "package base;\n" +
+                "public abstract class Base {\n" +
+                "    protected int i = 1;\n" +
+                "}",
+
+                "package sub;\n" +
+                "import base.Base;\n" +
+                "public class Sub extends Base {\n" +
+                "    private int i = 4;\n" +
+                "    void m() {\n" +
+                "        new Runnable() {\n" +
+                "            public void run() {\n" +
+                "                Sub.super.i += 10;\n" +
+                "            }\n" +
+                "        };\n" +
+                "    }\n" +
+                "}");
+
+        Path out = testBase.resolve("out");
+        Files.createDirectories(out);
+        Path base = src.resolve("base");
+        Path sub = src.resolve("sub");
+
+        new JavacTask(tb)
+            .outdir(out)
+            .files(tb.findJavaFiles(base))
+            .run(Task.Expect.SUCCESS);
+
+        new JavacTask(tb)
+            .classpath(out)
+            .outdir(out)
+            .files(tb.findJavaFiles(sub))
+            .run(Task.Expect.SUCCESS);
+    }
+}