7057297: Project Coin: diamond erroneously accepts in array initializer expressions
authormcimadamore
Wed, 27 Jul 2011 19:01:33 +0100
changeset 10199 48bd09ecc88a
parent 10198 84698d93792c
child 10200 56cc93e7b6ef
7057297: Project Coin: diamond erroneously accepts in array initializer expressions Summary: Diamond in array initializer expressions should be rejected Reviewed-by: jjg
langtools/src/share/classes/com/sun/tools/javac/parser/JavacParser.java
langtools/src/share/classes/com/sun/tools/javac/resources/compiler.properties
langtools/test/tools/javac/diags/examples/CannotCreateArrayWithDiamond.java
langtools/test/tools/javac/generics/diamond/7057297/T7057297.java
langtools/test/tools/javac/generics/diamond/7057297/T7057297.out
--- a/langtools/src/share/classes/com/sun/tools/javac/parser/JavacParser.java	Wed Jul 27 19:01:08 2011 +0100
+++ b/langtools/src/share/classes/com/sun/tools/javac/parser/JavacParser.java	Wed Jul 27 19:01:33 2011 +0100
@@ -1375,8 +1375,10 @@
         int oldmode = mode;
         mode = TYPE;
         boolean diamondFound = false;
+        int lastTypeargsPos = -1;
         if (S.token() == LT) {
             checkGenerics();
+            lastTypeargsPos = S.pos();
             t = typeArguments(t, true);
             diamondFound = (mode & DIAMOND) != 0;
         }
@@ -1389,6 +1391,7 @@
             S.nextToken();
             t = toP(F.at(pos).Select(t, ident()));
             if (S.token() == LT) {
+                lastTypeargsPos = S.pos();
                 checkGenerics();
                 t = typeArguments(t, true);
                 diamondFound = (mode & DIAMOND) != 0;
@@ -1397,7 +1400,11 @@
         mode = oldmode;
         if (S.token() == LBRACKET) {
             JCExpression e = arrayCreatorRest(newpos, t);
-            if (typeArgs != null) {
+            if (diamondFound) {
+                reportSyntaxError(lastTypeargsPos, "cannot.create.array.with.diamond");
+                return toP(F.at(newpos).Erroneous(List.of(e)));
+            }
+            else if (typeArgs != null) {
                 int pos = newpos;
                 if (!typeArgs.isEmpty() && typeArgs.head.pos != Position.NOPOS) {
                     // note: this should always happen but we should
--- a/langtools/src/share/classes/com/sun/tools/javac/resources/compiler.properties	Wed Jul 27 19:01:08 2011 +0100
+++ b/langtools/src/share/classes/com/sun/tools/javac/resources/compiler.properties	Wed Jul 27 19:01:33 2011 +0100
@@ -462,6 +462,9 @@
 compiler.err.cannot.create.array.with.type.arguments=\
     cannot create array with type arguments
 
+compiler.err.cannot.create.array.with.diamond=\
+    cannot create array with ''<>''
+
 #
 # limits.  We don't give the limits in the diagnostic because we expect
 # them to change, yet we want to use the same diagnostic.  These are all
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/diags/examples/CannotCreateArrayWithDiamond.java	Wed Jul 27 19:01:33 2011 +0100
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2011, 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.cannot.create.array.with.diamond
+
+class CannotCreateArrayWithDiamond {
+    Object[] array = new Object<>[3];
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/generics/diamond/7057297/T7057297.java	Wed Jul 27 19:01:33 2011 +0100
@@ -0,0 +1,29 @@
+/*
+ * @test /nodynamiccopyright/
+ * @bug 7057297
+ *
+ * @summary Project Coin: diamond erroneously accepts in array initializer expressions
+ * @compile/fail/ref=T7057297.out T7057297.java -XDrawDiagnostics
+ *
+ */
+
+class T7205797<X> {
+
+    class Inner<Y> {}
+
+    T7205797<String>[] o1 = new T7205797<>[1]; //error
+    T7205797<String>[] o2 = new T7205797<>[1][1]; //error
+    T7205797<String>[] o3 = new T7205797<>[1][1][1]; //error
+
+    T7205797<String>[] o4 = new T7205797<>[] { }; //error
+    T7205797<String>[] o5 = new T7205797<>[][] { }; //error
+    T7205797<String>[] o6 = new T7205797<>[][][] { }; //error
+
+    T7205797<String>.Inner<String>[] o1 = new T7205797<String>.Inner<>[1]; //error
+    T7205797<String>.Inner<String>[] o2 = new T7205797<String>.Inner<>[1][1]; //error
+    T7205797<String>.Inner<String>[] o3 = new T7205797<String>.Inner<>[1][1][1]; //error
+
+    T7205797<String>.Inner<String>[] o4 = new T7205797<String>.Inner<>[] { }; //error
+    T7205797<String>.Inner<String>[] o5 = new T7205797<String>.Inner<>[][] { }; //error
+    T7205797<String>.Inner<String>[] o6 = new T7205797<String>.Inner<>[][][] { }; //error
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/generics/diamond/7057297/T7057297.out	Wed Jul 27 19:01:33 2011 +0100
@@ -0,0 +1,13 @@
+T7057297.java:14:41: compiler.err.cannot.create.array.with.diamond
+T7057297.java:15:41: compiler.err.cannot.create.array.with.diamond
+T7057297.java:16:41: compiler.err.cannot.create.array.with.diamond
+T7057297.java:18:41: compiler.err.cannot.create.array.with.diamond
+T7057297.java:19:41: compiler.err.cannot.create.array.with.diamond
+T7057297.java:20:41: compiler.err.cannot.create.array.with.diamond
+T7057297.java:22:69: compiler.err.cannot.create.array.with.diamond
+T7057297.java:23:69: compiler.err.cannot.create.array.with.diamond
+T7057297.java:24:69: compiler.err.cannot.create.array.with.diamond
+T7057297.java:26:69: compiler.err.cannot.create.array.with.diamond
+T7057297.java:27:69: compiler.err.cannot.create.array.with.diamond
+T7057297.java:28:69: compiler.err.cannot.create.array.with.diamond
+12 errors