Fri, 25 Sep 2015 12:46:53 +0200
8134490: Dead var statement evacuation incorrectly descends into nested functions
Reviewed-by: hannesw, mhaupt
1.1 --- a/src/jdk/nashorn/internal/codegen/FindScopeDepths.java Wed Sep 16 14:42:32 2015 +0200 1.2 +++ b/src/jdk/nashorn/internal/codegen/FindScopeDepths.java Fri Sep 25 12:46:53 2015 +0200 1.3 @@ -277,15 +277,11 @@ 1.4 final Set<Symbol> symbols = new HashSet<>(); 1.5 block.accept(new NodeVisitor<LexicalContext>(new LexicalContext()) { 1.6 @Override 1.7 - public final boolean enterDefault(final Node node) { 1.8 - if (!compiler.isOnDemandCompilation()) { 1.9 - if (node instanceof IdentNode) { 1.10 - final Symbol symbol = ((IdentNode)node).getSymbol(); 1.11 - if (symbol != null && symbol.isScope()) { 1.12 - //if this is an internal symbol, skip it. 1.13 - symbols.add(symbol); 1.14 - } 1.15 - } 1.16 + public boolean enterIdentNode(final IdentNode identNode) { 1.17 + final Symbol symbol = identNode.getSymbol(); 1.18 + if (symbol != null && symbol.isScope()) { 1.19 + //if this is an internal symbol, skip it. 1.20 + symbols.add(symbol); 1.21 } 1.22 return true; 1.23 }
2.1 --- a/src/jdk/nashorn/internal/codegen/Lower.java Wed Sep 16 14:42:32 2015 +0200 2.2 +++ b/src/jdk/nashorn/internal/codegen/Lower.java Fri Sep 25 12:46:53 2015 +0200 2.3 @@ -123,9 +123,18 @@ 2.4 statement.accept(new NodeVisitor<LexicalContext>(new LexicalContext()) { 2.5 @Override 2.6 public boolean enterVarNode(final VarNode varNode) { 2.7 + // We can't entirely eliminate dead statements, as var declarations are scoped 2.8 + // to the whole function so we need to preserve them although without 2.9 + // initializers. 2.10 newStatements.add(varNode.setInit(null)); 2.11 return false; 2.12 } 2.13 + 2.14 + @Override 2.15 + public boolean enterFunctionNode(final FunctionNode functionNode) { 2.16 + // Don't descend into nested functions when searching for VarNodes, though. 2.17 + return false; 2.18 + } 2.19 }); 2.20 } 2.21 }
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/script/basic/JDK-8134490.js Fri Sep 25 12:46:53 2015 +0200 3.3 @@ -0,0 +1,41 @@ 3.4 +/* 3.5 + * Copyright (c) 2015 Oracle and/or its affiliates. All rights reserved. 3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3.7 + * 3.8 + * This code is free software; you can redistribute it and/or modify it 3.9 + * under the terms of the GNU General Public License version 2 only, as 3.10 + * published by the Free Software Foundation. 3.11 + * 3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 3.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 3.15 + * version 2 for more details (a copy is included in the LICENSE file that 3.16 + * accompanied this code). 3.17 + * 3.18 + * You should have received a copy of the GNU General Public License version 3.19 + * 2 along with this work; if not, write to the Free Software Foundation, 3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 3.21 + * 3.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 3.23 + * or visit www.oracle.com if you need additional information or have any 3.24 + * questions. 3.25 + */ 3.26 + 3.27 +/** 3.28 + * JDK-8134490: Dead var statement evacuation incorrectly descends into nested functions 3.29 + * 3.30 + * @test 3.31 + * @run 3.32 + */ 3.33 + 3.34 +var v1; 3.35 + 3.36 +function f1() 3.37 +{ 3.38 +v1 = 1; 3.39 +return true; 3.40 +(function () { var v1; })(); 3.41 +} 3.42 + 3.43 +f1(); 3.44 +// If it executes without throwing an exception in code generator, it's working.