8250629: do not allow C-style array declaration in record components

Reviewed-by: jlahoda
This commit is contained in:
Vicente Romero 2020-07-31 12:13:52 -04:00
parent 58107e52a8
commit 229f41808d
4 changed files with 58 additions and 5 deletions

View File

@ -3373,10 +3373,10 @@ public class JavacParser implements Parser {
/** VariableDeclaratorId = Ident BracketsOpt
*/
JCVariableDecl variableDeclaratorId(JCModifiers mods, JCExpression type) {
return variableDeclaratorId(mods, type, false);
return variableDeclaratorId(mods, type, false, false);
}
//where
JCVariableDecl variableDeclaratorId(JCModifiers mods, JCExpression type, boolean lambdaParameter) {
JCVariableDecl variableDeclaratorId(JCModifiers mods, JCExpression type, boolean lambdaParameter, boolean recordComponent) {
int pos = token.pos;
Name name;
if (lambdaParameter && token.kind == UNDERSCORE) {
@ -3421,6 +3421,9 @@ public class JavacParser implements Parser {
token.kind == LBRACKET) {
log.error(token.pos, Errors.VarargsAndOldArraySyntax);
}
if (recordComponent && token.kind == LBRACKET) {
log.error(token.pos, Errors.RecordComponentAndOldArraySyntax);
}
type = bracketsOpt(type);
return toP(F.at(pos).VarDef(mods, name, type, null));
@ -4566,12 +4569,12 @@ public class JavacParser implements Parser {
}
typeAnnotationsPushedBack = List.nil();
}
return variableDeclaratorId(mods, type, lambdaParameter);
return variableDeclaratorId(mods, type, lambdaParameter, recordComponent);
}
protected JCVariableDecl implicitParameter() {
JCModifiers mods = F.at(token.pos).Modifiers(Flags.PARAMETER);
return variableDeclaratorId(mods, null, true);
return variableDeclaratorId(mods, null, true, false);
}
/* ---------- auxiliary methods -------------- */

View File

@ -3505,6 +3505,9 @@ compiler.err.record.cant.declare.field.modifiers=\
compiler.err.illegal.record.component.name=\
illegal record component name {0}
compiler.err.record.component.and.old.array.syntax=\
legacy array notation not allowed on record components
# accessor methods
# 0: symbol, 1: fragment
compiler.err.invalid.accessor.method.in.record=\

View File

@ -0,0 +1,29 @@
/*
* Copyright (c) 2020, 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.record.component.and.old.array.syntax
// key: compiler.note.preview.filename
// key: compiler.note.preview.recompile
// options: --enable-preview -source ${jdk.version}
record R(int i[]) {}

View File

@ -26,7 +26,7 @@
/**
* RecordCompilationTests
*
* @test
* @test 8250629
* @summary Negative compilation tests, and positive compilation (smoke) tests for records
* @library /lib/combo /tools/lib /tools/javac/lib
* @modules
@ -1638,4 +1638,22 @@ public class RecordCompilationTests extends CompilationTestCase {
"""
);
}
public void testDoNotAllowCStyleArraySyntaxForRecComponents() {
assertFail("compiler.err.record.component.and.old.array.syntax",
"""
record R(int i[]) {}
"""
);
assertFail("compiler.err.record.component.and.old.array.syntax",
"""
record R(String s[]) {}
"""
);
assertFail("compiler.err.record.component.and.old.array.syntax",
"""
record R<T>(T t[]) {}
"""
);
}
}