mirror of
https://github.com/openjdk/jdk.git
synced 2026-04-29 00:02:34 +00:00
8025213: Assignment marks variable as defined too early
Reviewed-by: jlaskey, lagergren, sundar
This commit is contained in:
parent
be135d2901
commit
3d45463461
@ -1082,24 +1082,6 @@ final class Attr extends NodeOperatorVisitor<LexicalContext> {
|
||||
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 @@ final class Attr extends NodeOperatorVisitor<LexicalContext> {
|
||||
* @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) {
|
||||
|
||||
39
nashorn/test/script/basic/JDK-8025213.js
Normal file
39
nashorn/test/script/basic/JDK-8025213.js
Normal file
@ -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();
|
||||
1
nashorn/test/script/basic/JDK-8025213.js.EXPECTED
Normal file
1
nashorn/test/script/basic/JDK-8025213.js.EXPECTED
Normal file
@ -0,0 +1 @@
|
||||
NaN
|
||||
Loading…
x
Reference in New Issue
Block a user