8019805: for each (init; test; modify) is invalid
authorsundar
Wed, 03 Jul 2013 17:26:31 +0530
changeset 18843 eea6d1b8263e
parent 18842 3c3be808b593
child 18844 f85cfbfb4d11
8019805: for each (init; test; modify) is invalid Reviewed-by: lagergren, jlaskey
nashorn/src/jdk/nashorn/internal/parser/Parser.java
nashorn/src/jdk/nashorn/internal/runtime/resources/Messages.properties
nashorn/test/script/basic/JDK-8019805.js
nashorn/test/script/basic/JDK-8019805.js.EXPECTED
nashorn/test/script/basic/forin.js
nashorn/test/script/basic/forin.js.EXPECTED
--- a/nashorn/src/jdk/nashorn/internal/parser/Parser.java	Wed Jul 03 13:03:36 2013 +0200
+++ b/nashorn/src/jdk/nashorn/internal/parser/Parser.java	Wed Jul 03 17:26:31 2013 +0530
@@ -1084,6 +1084,12 @@
             switch (type) {
             case SEMICOLON:
                 // for (init; test; modify)
+
+                // for each (init; test; modify) is invalid
+                if (forNode.isForEach()) {
+                    throw error(AbstractParser.message("for.each.without.in"), token);
+                }
+
                 expect(SEMICOLON);
                 if (type != SEMICOLON) {
                     forNode = forNode.setTest(lc, expression());
--- a/nashorn/src/jdk/nashorn/internal/runtime/resources/Messages.properties	Wed Jul 03 13:03:36 2013 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/resources/Messages.properties	Wed Jul 03 17:26:31 2013 +0530
@@ -50,6 +50,7 @@
 parser.error.no.func.decl.here.warn=Function declarations should only occur at program or function body level. Function declaration in nested block was converted to a function expression.
 parser.error.property.redefinition=Property "{0}" already defined
 parser.error.unexpected.token=Unexpected token: {0}
+parser.error.for.each.without.in=for each can only be used with for..in
 parser.error.many.vars.in.for.in.loop=Only one variable allowed in for..in loop
 parser.error.not.lvalue.for.in.loop=Invalid left side value of for..in loop
 parser.error.missing.catch.or.finally=Missing catch or finally after try
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8019805.js	Wed Jul 03 17:26:31 2013 +0530
@@ -0,0 +1,36 @@
+/*
+ * 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-8019805: for each (init; test; modify) is invalid
+ *
+ * @test
+ * @run
+ */
+
+try {
+    eval("for each(var v=0;false;);");
+    print("FAILED: for each(var v=0; false;); should have thrown error");
+} catch (e) {
+    print(e.toString().replace(/\\/g, '/'));
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8019805.js.EXPECTED	Wed Jul 03 17:26:31 2013 +0530
@@ -0,0 +1,3 @@
+SyntaxError: test/script/basic/JDK-8019805.js#32<eval>:1:16 for each can only be used with for..in
+for each(var v=0;false;);
+                ^
--- a/nashorn/test/script/basic/forin.js	Wed Jul 03 13:03:36 2013 +0200
+++ b/nashorn/test/script/basic/forin.js	Wed Jul 03 17:26:31 2013 +0530
@@ -49,8 +49,3 @@
 // 'each' is a contextual keyword. Ok to use as identifier elsewhere..
 var each = "This is each";
 print(each);
-
-// it is ok to use "each" is usual for loop. Ignored as noise word.
-for each (var i = 0; i < 10; i++) {
-    print(i);
-}
--- a/nashorn/test/script/basic/forin.js.EXPECTED	Wed Jul 03 13:03:36 2013 +0200
+++ b/nashorn/test/script/basic/forin.js.EXPECTED	Wed Jul 03 17:26:31 2013 +0530
@@ -25,13 +25,3 @@
 bear
 car
 This is each
-0
-1
-2
-3
-4
-5
-6
-7
-8
-9