# HG changeset patch # User vromero # Date 1536237887 25200 # Node ID 805807f15830ee3a75d50c076b72683bf3aa6e77 # Parent 0fd48caf8243b7586c332f9d660b31396a11991c 8210435: don't add local variable spots if they are DCE'ed by the compiler Reviewed-by: mcimadamore diff -r 0fd48caf8243 -r 805807f15830 src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Gen.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); } diff -r 0fd48caf8243 -r 805807f15830 test/langtools/tools/javac/T8210435/NoLocalsMustBeReservedForDCEedVarsTest.java --- /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); + } + } + } +}