Skip to content

Commit

Permalink
8309093: Underscore with brackets
Browse files Browse the repository at this point in the history
  • Loading branch information
biboudis committed May 30, 2023
1 parent 70130d3 commit 4826edb
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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}};
}
}
3 changes: 2 additions & 1 deletion test/langtools/tools/javac/lambda/IdentifierTest21.out
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ 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
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
37 errors
7 changes: 6 additions & 1 deletion test/langtools/tools/javac/patterns/UnnamedErrors.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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() {}
Expand Down
4 changes: 3 additions & 1 deletion test/langtools/tools/javac/patterns/UnnamedErrors.out
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
34 errors

0 comments on commit 4826edb

Please sign in to comment.