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

Wed, 27 Jul 2011 19:01:33 +0100

author
mcimadamore
date
Wed, 27 Jul 2011 19:01:33 +0100
changeset 1061
e427c42e1a7e
parent 1060
d5f33267a06d
child 1062
0d6d41563040

7057297: Project Coin: diamond erroneously accepts in array initializer expressions
Summary: Diamond in array initializer expressions should be rejected
Reviewed-by: jjg

src/share/classes/com/sun/tools/javac/parser/JavacParser.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/resources/compiler.properties file | annotate | diff | comparison | revisions
test/tools/javac/diags/examples/CannotCreateArrayWithDiamond.java file | annotate | diff | comparison | revisions
test/tools/javac/generics/diamond/7057297/T7057297.java file | annotate | diff | comparison | revisions
test/tools/javac/generics/diamond/7057297/T7057297.out file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/parser/JavacParser.java	Wed Jul 27 19:01:08 2011 +0100
     1.2 +++ b/src/share/classes/com/sun/tools/javac/parser/JavacParser.java	Wed Jul 27 19:01:33 2011 +0100
     1.3 @@ -1375,8 +1375,10 @@
     1.4          int oldmode = mode;
     1.5          mode = TYPE;
     1.6          boolean diamondFound = false;
     1.7 +        int lastTypeargsPos = -1;
     1.8          if (S.token() == LT) {
     1.9              checkGenerics();
    1.10 +            lastTypeargsPos = S.pos();
    1.11              t = typeArguments(t, true);
    1.12              diamondFound = (mode & DIAMOND) != 0;
    1.13          }
    1.14 @@ -1389,6 +1391,7 @@
    1.15              S.nextToken();
    1.16              t = toP(F.at(pos).Select(t, ident()));
    1.17              if (S.token() == LT) {
    1.18 +                lastTypeargsPos = S.pos();
    1.19                  checkGenerics();
    1.20                  t = typeArguments(t, true);
    1.21                  diamondFound = (mode & DIAMOND) != 0;
    1.22 @@ -1397,7 +1400,11 @@
    1.23          mode = oldmode;
    1.24          if (S.token() == LBRACKET) {
    1.25              JCExpression e = arrayCreatorRest(newpos, t);
    1.26 -            if (typeArgs != null) {
    1.27 +            if (diamondFound) {
    1.28 +                reportSyntaxError(lastTypeargsPos, "cannot.create.array.with.diamond");
    1.29 +                return toP(F.at(newpos).Erroneous(List.of(e)));
    1.30 +            }
    1.31 +            else if (typeArgs != null) {
    1.32                  int pos = newpos;
    1.33                  if (!typeArgs.isEmpty() && typeArgs.head.pos != Position.NOPOS) {
    1.34                      // note: this should always happen but we should
     2.1 --- a/src/share/classes/com/sun/tools/javac/resources/compiler.properties	Wed Jul 27 19:01:08 2011 +0100
     2.2 +++ b/src/share/classes/com/sun/tools/javac/resources/compiler.properties	Wed Jul 27 19:01:33 2011 +0100
     2.3 @@ -462,6 +462,9 @@
     2.4  compiler.err.cannot.create.array.with.type.arguments=\
     2.5      cannot create array with type arguments
     2.6  
     2.7 +compiler.err.cannot.create.array.with.diamond=\
     2.8 +    cannot create array with ''<>''
     2.9 +
    2.10  #
    2.11  # limits.  We don't give the limits in the diagnostic because we expect
    2.12  # them to change, yet we want to use the same diagnostic.  These are all
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/tools/javac/diags/examples/CannotCreateArrayWithDiamond.java	Wed Jul 27 19:01:33 2011 +0100
     3.3 @@ -0,0 +1,28 @@
     3.4 +/*
     3.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
     3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3.7 + *
     3.8 + * This code is free software; you can redistribute it and/or modify it
     3.9 + * under the terms of the GNU General Public License version 2 only, as
    3.10 + * published by the Free Software Foundation.
    3.11 + *
    3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    3.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    3.15 + * version 2 for more details (a copy is included in the LICENSE file that
    3.16 + * accompanied this code).
    3.17 + *
    3.18 + * You should have received a copy of the GNU General Public License version
    3.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    3.21 + *
    3.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    3.23 + * or visit www.oracle.com if you need additional information or have any
    3.24 + * questions.
    3.25 + */
    3.26 +
    3.27 +// key: compiler.err.cannot.create.array.with.diamond
    3.28 +
    3.29 +class CannotCreateArrayWithDiamond {
    3.30 +    Object[] array = new Object<>[3];
    3.31 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/test/tools/javac/generics/diamond/7057297/T7057297.java	Wed Jul 27 19:01:33 2011 +0100
     4.3 @@ -0,0 +1,29 @@
     4.4 +/*
     4.5 + * @test /nodynamiccopyright/
     4.6 + * @bug 7057297
     4.7 + *
     4.8 + * @summary Project Coin: diamond erroneously accepts in array initializer expressions
     4.9 + * @compile/fail/ref=T7057297.out T7057297.java -XDrawDiagnostics
    4.10 + *
    4.11 + */
    4.12 +
    4.13 +class T7205797<X> {
    4.14 +
    4.15 +    class Inner<Y> {}
    4.16 +
    4.17 +    T7205797<String>[] o1 = new T7205797<>[1]; //error
    4.18 +    T7205797<String>[] o2 = new T7205797<>[1][1]; //error
    4.19 +    T7205797<String>[] o3 = new T7205797<>[1][1][1]; //error
    4.20 +
    4.21 +    T7205797<String>[] o4 = new T7205797<>[] { }; //error
    4.22 +    T7205797<String>[] o5 = new T7205797<>[][] { }; //error
    4.23 +    T7205797<String>[] o6 = new T7205797<>[][][] { }; //error
    4.24 +
    4.25 +    T7205797<String>.Inner<String>[] o1 = new T7205797<String>.Inner<>[1]; //error
    4.26 +    T7205797<String>.Inner<String>[] o2 = new T7205797<String>.Inner<>[1][1]; //error
    4.27 +    T7205797<String>.Inner<String>[] o3 = new T7205797<String>.Inner<>[1][1][1]; //error
    4.28 +
    4.29 +    T7205797<String>.Inner<String>[] o4 = new T7205797<String>.Inner<>[] { }; //error
    4.30 +    T7205797<String>.Inner<String>[] o5 = new T7205797<String>.Inner<>[][] { }; //error
    4.31 +    T7205797<String>.Inner<String>[] o6 = new T7205797<String>.Inner<>[][][] { }; //error
    4.32 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/test/tools/javac/generics/diamond/7057297/T7057297.out	Wed Jul 27 19:01:33 2011 +0100
     5.3 @@ -0,0 +1,13 @@
     5.4 +T7057297.java:14:41: compiler.err.cannot.create.array.with.diamond
     5.5 +T7057297.java:15:41: compiler.err.cannot.create.array.with.diamond
     5.6 +T7057297.java:16:41: compiler.err.cannot.create.array.with.diamond
     5.7 +T7057297.java:18:41: compiler.err.cannot.create.array.with.diamond
     5.8 +T7057297.java:19:41: compiler.err.cannot.create.array.with.diamond
     5.9 +T7057297.java:20:41: compiler.err.cannot.create.array.with.diamond
    5.10 +T7057297.java:22:69: compiler.err.cannot.create.array.with.diamond
    5.11 +T7057297.java:23:69: compiler.err.cannot.create.array.with.diamond
    5.12 +T7057297.java:24:69: compiler.err.cannot.create.array.with.diamond
    5.13 +T7057297.java:26:69: compiler.err.cannot.create.array.with.diamond
    5.14 +T7057297.java:27:69: compiler.err.cannot.create.array.with.diamond
    5.15 +T7057297.java:28:69: compiler.err.cannot.create.array.with.diamond
    5.16 +12 errors

mercurial