diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java index 017dac30e7f9b..ff08ea84b286c 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java @@ -624,6 +624,9 @@ protected Name ident(boolean allowClass, boolean asVariable) { log.warning(token.pos, Warnings.UnderscoreAsIdentifier); } else if (asVariable) { checkSourceLevel(Feature.UNNAMED_VARIABLES); + if (peekToken(LBRACKET)) { + log.error(DiagnosticFlag.SYNTAX, token.pos, Errors.UseOfUnderscoreNotAllowedWithBrackets); + } } else { if (preview.isEnabled() && Feature.UNNAMED_VARIABLES.allowedInSource(source)) { log.error(DiagnosticFlag.SYNTAX, token.pos, Errors.UseOfUnderscoreNotAllowed); diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties index e99b4a699ebeb..9a2f555bb62f3 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties @@ -3162,6 +3162,9 @@ compiler.err.use.of.underscore.not.allowed=\ as of release 21, the underscore keyword ''_'' is only allowed to declare\n\ unnamed patterns, local variables, exception parameters or lambda parameters +compiler.err.use.of.underscore.not.allowed.with.brackets=\ + the underscore keyword ''_'' is not allowed to be followed by brackets + compiler.err.enum.as.identifier=\ as of release 5, ''enum'' is a keyword, and may not be used as an identifier diff --git a/test/langtools/tools/javac/diags/examples/UseOfUnderscoreNotAllowedWithBrackets.java b/test/langtools/tools/javac/diags/examples/UseOfUnderscoreNotAllowedWithBrackets.java new file mode 100644 index 0000000000000..77289f743efec --- /dev/null +++ b/test/langtools/tools/javac/diags/examples/UseOfUnderscoreNotAllowedWithBrackets.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023, 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.use.of.underscore.not.allowed.with.brackets +// key: compiler.misc.feature.unnamed.variables +// key: compiler.warn.preview.feature.use.plural +// options: --enable-preview -source ${jdk.version} -Xlint:preview + +class UseOfUnderscoreNotAllowedWithBrackets { + void test() { + int[] _[] = new int[][]{new int[]{1}, new int[]{2}}; + } +} diff --git a/test/langtools/tools/javac/lambda/IdentifierTest21.out b/test/langtools/tools/javac/lambda/IdentifierTest21.out index da9183a48e6c9..801584fafbc35 100644 --- a/test/langtools/tools/javac/lambda/IdentifierTest21.out +++ b/test/langtools/tools/javac/lambda/IdentifierTest21.out @@ -29,6 +29,7 @@ IdentifierTest.java:145:15: compiler.err.use.of.underscore.not.allowed IdentifierTest.java:146:13: compiler.err.use.of.underscore.not.allowed IdentifierTest.java:151:15: compiler.err.use.of.underscore.not.allowed IdentifierTest.java:152:17: compiler.err.use.of.underscore.not.allowed +IdentifierTest.java:158:16: compiler.err.use.of.underscore.not.allowed.with.brackets IdentifierTest.java:160:25: compiler.err.use.of.underscore.not.allowed IdentifierTest.java:169:5: compiler.err.use.of.underscore.not.allowed IdentifierTest.java:173:26: compiler.err.use.of.underscore.not.allowed @@ -36,4 +37,4 @@ IdentifierTest.java:175:19: compiler.err.use.of.underscore.not.allowed IdentifierTest.java:181:11: compiler.err.use.of.underscore.not.allowed - compiler.note.preview.filename: IdentifierTest.java, DEFAULT - compiler.note.preview.recompile -36 errors \ No newline at end of file +37 errors \ No newline at end of file diff --git a/test/langtools/tools/javac/patterns/UnnamedErrors.java b/test/langtools/tools/javac/patterns/UnnamedErrors.java index b59837082fdf6..a87c2577eed30 100644 --- a/test/langtools/tools/javac/patterns/UnnamedErrors.java +++ b/test/langtools/tools/javac/patterns/UnnamedErrors.java @@ -1,6 +1,6 @@ /** * @test /nodynamiccopyright/ - * @bug 8304246 + * @bug 8304246 8309093 * @summary Compiler Implementation for Unnamed patterns and variables * @enablePreview * @compile/fail/ref=UnnamedErrors.out -XDrawDiagnostics -XDshould-stop.at=FLOW UnnamedErrors.java @@ -106,6 +106,11 @@ void testUnderscoreWithoutInitializer() { } } + void testUnderscoreWithBrackets() { + int _[] = new int[]{1}; + for (int _[] : new int[][]{new int[]{1}, new int[]{2}}) { } + } + class Lock implements AutoCloseable { @Override public void close() {} diff --git a/test/langtools/tools/javac/patterns/UnnamedErrors.out b/test/langtools/tools/javac/patterns/UnnamedErrors.out index c92b1268354a2..430119db577bc 100644 --- a/test/langtools/tools/javac/patterns/UnnamedErrors.out +++ b/test/langtools/tools/javac/patterns/UnnamedErrors.out @@ -21,6 +21,8 @@ UnnamedErrors.java:82:58: compiler.err.expected: ';' UnnamedErrors.java:101:14: compiler.err.expected: = UnnamedErrors.java:102:22: compiler.err.expected: = UnnamedErrors.java:104:26: compiler.err.expected: = +UnnamedErrors.java:110:13: compiler.err.use.of.underscore.not.allowed.with.brackets +UnnamedErrors.java:111:18: compiler.err.use.of.underscore.not.allowed.with.brackets UnnamedErrors.java:11:17: compiler.err.already.defined: kindname.variable, x, kindname.class, UnnamedErrors UnnamedErrors.java:36:13: compiler.err.unconditional.pattern.and.default UnnamedErrors.java:45:18: compiler.err.pattern.dominated @@ -32,4 +34,4 @@ UnnamedErrors.java:83:13: compiler.err.switch.mixing.case.types UnnamedErrors.java:90:30: compiler.err.pattern.dominated - compiler.note.preview.filename: UnnamedErrors.java, DEFAULT - compiler.note.preview.recompile -32 errors \ No newline at end of file +34 errors \ No newline at end of file