8210435: don't add local variable spots if they are DCE'ed by the compiler
authorvromero
Thu, 06 Sep 2018 05:44:47 -0700
changeset 51658 805807f15830
parent 51657 0fd48caf8243
child 51659 ca3003390cf0
8210435: don't add local variable spots if they are DCE'ed by the compiler Reviewed-by: mcimadamore
src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Gen.java
test/langtools/tools/javac/T8210435/NoLocalsMustBeReservedForDCEedVarsTest.java
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Gen.java	Thu Sep 06 13:13:35 2018 +0100
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Gen.java	Thu Sep 06 05:44:47 2018 -0700
@@ -1002,15 +1002,17 @@
 
     public void visitVarDef(JCVariableDecl tree) {
         VarSymbol v = tree.sym;
-        code.newLocal(v);
         if (tree.init != null) {
             checkStringConstant(tree.init.pos(), v.getConstValue());
             if (v.getConstValue() == null || varDebugInfo) {
                 Assert.check(code.isStatementStart());
+                code.newLocal(v);
                 genExpr(tree.init, v.erasure(types)).load();
                 items.makeLocalItem(v).store();
                 Assert.check(code.isStatementStart());
             }
+        } else {
+            code.newLocal(v);
         }
         checkDimension(tree.pos(), v.type);
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/T8210435/NoLocalsMustBeReservedForDCEedVarsTest.java	Thu Sep 06 05:44:47 2018 -0700
@@ -0,0 +1,75 @@
+/*
+ * Copyright (c) 2018, 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
+ * @summary
+ * @library /tools/lib
+ * @modules jdk.jdeps/com.sun.tools.classfile
+ *          jdk.compiler/com.sun.tools.javac.api
+ *          jdk.compiler/com.sun.tools.javac.main
+ *          jdk.compiler/com.sun.tools.javac.util
+ *          jdk.jdeps/com.sun.tools.javap
+ * @build toolbox.ToolBox toolbox.JavacTask
+ * @run main NoLocalsMustBeReservedForDCEedVarsTest
+ */
+
+import java.io.File;
+import java.nio.file.Paths;
+
+import com.sun.tools.classfile.*;
+import com.sun.tools.javac.util.Assert;
+
+import toolbox.JavacTask;
+import toolbox.ToolBox;
+
+public class NoLocalsMustBeReservedForDCEedVarsTest {
+
+    static final String source =
+            "class Test {\n" +
+            "    static String foo() {\n" +
+            "        final String hello = \"Hello!\";\n" +
+            "        return hello;\n" +
+            "    }\n" +
+            "} ";
+
+    public static void main(String[] args) throws Exception {
+        new NoLocalsMustBeReservedForDCEedVarsTest().run();
+    }
+
+    void run() throws Exception {
+        ToolBox tb = new ToolBox();
+        new JavacTask(tb)
+                .sources(source)
+                .run();
+
+        File cfile = new File(Paths.get(System.getProperty("user.dir"), "Test.class").toUri());
+        ClassFile classFile = ClassFile.read(cfile);
+        for (Method method: classFile.methods) {
+            if (method.getName(classFile.constant_pool).equals("foo")) {
+                Code_attribute codeAttr = (Code_attribute)method.attributes.get("Code");
+                Assert.check(codeAttr.max_locals == 0, "max locals found " + codeAttr.max_locals);
+            }
+        }
+    }
+}