8057647: javac parser needs updates to have better error recovery for error cases of new array creation with dimensions
authorpmuthuswamy
Wed, 19 Jul 2017 11:44:08 +0530
changeset 45915 1afba647cd44
parent 45914 0c6f13526872
child 45916 4c0ccce6c76b
8057647: javac parser needs updates to have better error recovery for error cases of new array creation with dimensions Reviewed-by: jlahoda
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties
langtools/test/tools/javac/ExtraneousEquals.java
langtools/test/tools/javac/ExtraneousEquals.out
langtools/test/tools/javac/diags/examples/IllegalArrayCreation.java
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java	Tue Jul 18 15:50:14 2017 -0700
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java	Wed Jul 19 11:44:08 2017 +0530
@@ -2243,8 +2243,20 @@
                 }
             }
 
-            JCNewArray na = toP(F.at(newpos).NewArray(elemtype, dims.toList(), null));
+            List<JCExpression> elems = null;
+            int errpos = token.pos;
+
+            if (token.kind == LBRACE) {
+                elems = arrayInitializerElements(newpos, elemtype);
+            }
+
+            JCNewArray na = toP(F.at(newpos).NewArray(elemtype, dims.toList(), elems));
             na.dimAnnotations = dimAnnotations.toList();
+
+            if (elems != null) {
+                return syntaxError(errpos, List.of(na), "illegal.array.creation.both.dimension.and.initialization");
+            }
+
             return na;
         }
     }
@@ -2270,6 +2282,11 @@
     /** ArrayInitializer = "{" [VariableInitializer {"," VariableInitializer}] [","] "}"
      */
     JCExpression arrayInitializer(int newpos, JCExpression t) {
+        List<JCExpression> elems = arrayInitializerElements(newpos, t);
+        return toP(F.at(newpos).NewArray(t, List.nil(), elems));
+    }
+
+    List<JCExpression> arrayInitializerElements(int newpos, JCExpression t) {
         accept(LBRACE);
         ListBuffer<JCExpression> elems = new ListBuffer<>();
         if (token.kind == COMMA) {
@@ -2283,7 +2300,7 @@
             }
         }
         accept(RBRACE);
-        return toP(F.at(newpos).NewArray(t, List.nil(), elems.toList()));
+        return elems.toList();
     }
 
     /** VariableInitializer = ArrayInitializer | Expression
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Tue Jul 18 15:50:14 2017 -0700
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Wed Jul 19 11:44:08 2017 +0530
@@ -165,6 +165,9 @@
 compiler.err.array.dimension.missing=\
     array dimension missing
 
+compiler.err.illegal.array.creation.both.dimension.and.initialization=\
+    array creation with both dimension expression and initialization is illegal
+
 # 0: type
 compiler.err.array.req.but.found=\
     array required, but {0} found
--- a/langtools/test/tools/javac/ExtraneousEquals.java	Tue Jul 18 15:50:14 2017 -0700
+++ b/langtools/test/tools/javac/ExtraneousEquals.java	Wed Jul 19 11:44:08 2017 +0530
@@ -1,6 +1,6 @@
 /*
  * @test /nodynamiccopyright/
- * @bug 5019614
+ * @bug 5019614 8057647
  * @summary variance prototype syntax leftover
  *
  * @compile/fail/ref=ExtraneousEquals.out -XDrawDiagnostics ExtraneousEquals.java
--- a/langtools/test/tools/javac/ExtraneousEquals.out	Tue Jul 18 15:50:14 2017 -0700
+++ b/langtools/test/tools/javac/ExtraneousEquals.out	Wed Jul 19 11:44:08 2017 +0530
@@ -1,6 +1,4 @@
 ExtraneousEquals.java:10:23: compiler.err.illegal.start.of.expr
 ExtraneousEquals.java:10:24: compiler.err.illegal.start.of.expr
-ExtraneousEquals.java:10:25: compiler.err.expected: ';'
-ExtraneousEquals.java:10:28: compiler.err.not.stmt
-ExtraneousEquals.java:10:29: compiler.err.expected: ';'
-5 errors
+ExtraneousEquals.java:10:26: compiler.err.illegal.array.creation.both.dimension.and.initialization
+3 errors
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/diags/examples/IllegalArrayCreation.java	Wed Jul 19 11:44:08 2017 +0530
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2017, 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.
+ */
+
+// key: compiler.err.illegal.array.creation.both.dimension.and.initialization
+
+class IllegalArrayCreation {
+    int[] foo = new int[10] { 1, 2, 3 };
+}