8025213: Assignment marks variable as defined too early
Reviewed-by: jlaskey, lagergren, sundar
--- a/nashorn/src/jdk/nashorn/internal/codegen/Attr.java Fri Oct 04 16:21:29 2013 +0530
+++ b/nashorn/src/jdk/nashorn/internal/codegen/Attr.java Tue Oct 08 11:55:19 2013 +0200
@@ -1082,24 +1082,6 @@
private boolean enterAssignmentNode(final BinaryNode binaryNode) {
start(binaryNode);
- final Node lhs = binaryNode.lhs();
-
- if (lhs instanceof IdentNode) {
- final Block block = lc.getCurrentBlock();
- final IdentNode ident = (IdentNode)lhs;
- final String name = ident.getName();
-
- Symbol symbol = findSymbol(block, name);
-
- if (symbol == null) {
- symbol = defineSymbol(block, name, IS_GLOBAL);
- } else {
- maybeForceScope(symbol);
- }
-
- addLocalDef(name);
- }
-
return true;
}
@@ -1112,20 +1094,33 @@
* @param binaryNode assignment node
*/
private Node leaveAssignmentNode(final BinaryNode binaryNode) {
- BinaryNode newBinaryNode = binaryNode;
-
final Expression lhs = binaryNode.lhs();
final Expression rhs = binaryNode.rhs();
final Type type;
+ if (lhs instanceof IdentNode) {
+ final Block block = lc.getCurrentBlock();
+ final IdentNode ident = (IdentNode)lhs;
+ final String name = ident.getName();
+ final Symbol symbol = findSymbol(block, name);
+
+ if (symbol == null) {
+ defineSymbol(block, name, IS_GLOBAL);
+ } else {
+ maybeForceScope(symbol);
+ }
+
+ addLocalDef(name);
+ }
+
if (rhs.getType().isNumeric()) {
- type = Type.widest(binaryNode.lhs().getType(), binaryNode.rhs().getType());
+ type = Type.widest(lhs.getType(), rhs.getType());
} else {
type = Type.OBJECT; //force lhs to be an object if not numeric assignment, e.g. strings too.
}
newType(lhs.getSymbol(), type);
- return end(ensureSymbol(type, newBinaryNode));
+ return end(ensureSymbol(type, binaryNode));
}
private boolean isLocal(FunctionNode function, Symbol symbol) {
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8025213.js Tue Oct 08 11:55:19 2013 +0200
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2010, 2013, 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.
+ */
+
+/**
+ * JDK-8025213: Assignment marks variable as defined too early
+ *
+ * @test
+ * @run
+ */
+
+function test() {
+ if (String("")) {
+ var foo = 42;
+ }
+ foo = foo + 1;
+ print(foo);
+}
+
+test();
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8025213.js.EXPECTED Tue Oct 08 11:55:19 2013 +0200
@@ -0,0 +1,1 @@
+NaN