7057297: Project Coin: diamond erroneously accepts in array initializer expressions

Diamond in array initializer expressions should be rejected

Reviewed-by: jjg
This commit is contained in:
Maurizio Cimadamore 2011-07-27 19:01:33 +01:00
parent 02802c9449
commit 5c209a7d2d
5 changed files with 81 additions and 1 deletions

View file

@ -1375,8 +1375,10 @@ public class JavacParser implements Parser {
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 @@ public class JavacParser implements Parser {
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 @@ public class JavacParser implements Parser {
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

View file

@ -462,6 +462,9 @@ compiler.err.local.enum=\
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

View file

@ -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];
}

View file

@ -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
}

View file

@ -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