8057647: javac parser needs updates to have better error recovery for error cases of new array creation with dimensions

Reviewed-by: jlahoda
This commit is contained in:
Priya Lakshmi Muthuswamy 2017-07-19 11:44:08 +05:30
parent f7a836d55a
commit c31576193a
5 changed files with 53 additions and 7 deletions
langtools
src/jdk.compiler/share/classes/com/sun/tools/javac
test/tools/javac

@ -2243,8 +2243,20 @@ public class JavacParser implements Parser {
}
}
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 @@ public class JavacParser implements Parser {
/** 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 @@ public class JavacParser implements Parser {
}
}
accept(RBRACE);
return toP(F.at(newpos).NewArray(t, List.nil(), elems.toList()));
return elems.toList();
}
/** VariableInitializer = ArrayInitializer | Expression

@ -165,6 +165,9 @@ compiler.err.array.and.varargs=\
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

@ -1,6 +1,6 @@
/*
* @test /nodynamiccopyright/
* @bug 5019614
* @bug 5019614 8057647
* @summary variance prototype syntax leftover
*
* @compile/fail/ref=ExtraneousEquals.out -XDrawDiagnostics ExtraneousEquals.java

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

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