8025213: Assignment marks variable as defined too early

Tue, 08 Oct 2013 11:55:19 +0200

author
hannesw
date
Tue, 08 Oct 2013 11:55:19 +0200
changeset 591
6345d08fd5de
parent 590
3470bc26128f
child 592
8c326f8c6799

8025213: Assignment marks variable as defined too early
Reviewed-by: jlaskey, lagergren, sundar

src/jdk/nashorn/internal/codegen/Attr.java file | annotate | diff | comparison | revisions
test/script/basic/JDK-8025213.js file | annotate | diff | comparison | revisions
test/script/basic/JDK-8025213.js.EXPECTED file | annotate | diff | comparison | revisions
     1.1 --- a/src/jdk/nashorn/internal/codegen/Attr.java	Fri Oct 04 16:21:29 2013 +0530
     1.2 +++ b/src/jdk/nashorn/internal/codegen/Attr.java	Tue Oct 08 11:55:19 2013 +0200
     1.3 @@ -1082,24 +1082,6 @@
     1.4      private boolean enterAssignmentNode(final BinaryNode binaryNode) {
     1.5          start(binaryNode);
     1.6  
     1.7 -        final Node lhs = binaryNode.lhs();
     1.8 -
     1.9 -        if (lhs instanceof IdentNode) {
    1.10 -            final Block     block = lc.getCurrentBlock();
    1.11 -            final IdentNode ident = (IdentNode)lhs;
    1.12 -            final String    name  = ident.getName();
    1.13 -
    1.14 -            Symbol symbol = findSymbol(block, name);
    1.15 -
    1.16 -            if (symbol == null) {
    1.17 -                symbol = defineSymbol(block, name, IS_GLOBAL);
    1.18 -            } else {
    1.19 -                maybeForceScope(symbol);
    1.20 -            }
    1.21 -
    1.22 -            addLocalDef(name);
    1.23 -        }
    1.24 -
    1.25          return true;
    1.26      }
    1.27  
    1.28 @@ -1112,20 +1094,33 @@
    1.29       * @param binaryNode assignment node
    1.30       */
    1.31      private Node leaveAssignmentNode(final BinaryNode binaryNode) {
    1.32 -        BinaryNode newBinaryNode = binaryNode;
    1.33 -
    1.34          final Expression lhs = binaryNode.lhs();
    1.35          final Expression rhs = binaryNode.rhs();
    1.36          final Type type;
    1.37  
    1.38 +        if (lhs instanceof IdentNode) {
    1.39 +            final Block     block = lc.getCurrentBlock();
    1.40 +            final IdentNode ident = (IdentNode)lhs;
    1.41 +            final String    name  = ident.getName();
    1.42 +            final Symbol symbol = findSymbol(block, name);
    1.43 +
    1.44 +            if (symbol == null) {
    1.45 +                defineSymbol(block, name, IS_GLOBAL);
    1.46 +            } else {
    1.47 +                maybeForceScope(symbol);
    1.48 +            }
    1.49 +
    1.50 +            addLocalDef(name);
    1.51 +        }
    1.52 +
    1.53          if (rhs.getType().isNumeric()) {
    1.54 -            type = Type.widest(binaryNode.lhs().getType(), binaryNode.rhs().getType());
    1.55 +            type = Type.widest(lhs.getType(), rhs.getType());
    1.56          } else {
    1.57              type = Type.OBJECT; //force lhs to be an object if not numeric assignment, e.g. strings too.
    1.58          }
    1.59  
    1.60          newType(lhs.getSymbol(), type);
    1.61 -        return end(ensureSymbol(type, newBinaryNode));
    1.62 +        return end(ensureSymbol(type, binaryNode));
    1.63      }
    1.64  
    1.65      private boolean isLocal(FunctionNode function, Symbol symbol) {
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/script/basic/JDK-8025213.js	Tue Oct 08 11:55:19 2013 +0200
     2.3 @@ -0,0 +1,39 @@
     2.4 +/*
     2.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
     2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     2.7 + * 
     2.8 + * This code is free software; you can redistribute it and/or modify it
     2.9 + * under the terms of the GNU General Public License version 2 only, as
    2.10 + * published by the Free Software Foundation.
    2.11 + * 
    2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    2.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    2.15 + * version 2 for more details (a copy is included in the LICENSE file that
    2.16 + * accompanied this code).
    2.17 + * 
    2.18 + * You should have received a copy of the GNU General Public License version
    2.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    2.21 + * 
    2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    2.23 + * or visit www.oracle.com if you need additional information or have any
    2.24 + * questions.
    2.25 + */
    2.26 +
    2.27 +/**
    2.28 + * JDK-8025213: Assignment marks variable as defined too early
    2.29 + *
    2.30 + * @test
    2.31 + * @run
    2.32 + */
    2.33 +
    2.34 +function test() {
    2.35 +    if (String("")) {
    2.36 +        var foo = 42;
    2.37 +    }
    2.38 +    foo = foo + 1;
    2.39 +    print(foo);
    2.40 +}
    2.41 +
    2.42 +test();
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/script/basic/JDK-8025213.js.EXPECTED	Tue Oct 08 11:55:19 2013 +0200
     3.3 @@ -0,0 +1,1 @@
     3.4 +NaN

mercurial