Skip to content

Commit

Permalink
8315532: Compiler Implementation for Unnamed Variables & Patterns
Browse files Browse the repository at this point in the history
8317221: Implementation for javax.lang.model for Unnamed Variables & Patterns

Co-authored-by: Jan Lahoda <jlahoda@openjdk.org>
Co-authored-by: Maurizio Cimadamore <mcimadamore@openjdk.org>
Co-authored-by: Gavin Bierman <gbierman@openjdk.org>
Co-authored-by: Brian Goetz <briangoetz@openjdk.org>
Co-authored-by: Joe Darcy <darcy@openjdk.org>
Co-authored-by: Aggelos Biboudis <abimpoudis@openjdk.org>
Reviewed-by: jlahoda, mcimadamore
  • Loading branch information
6 people committed Oct 30, 2023
1 parent 3934127 commit c9d23c3
Show file tree
Hide file tree
Showing 38 changed files with 195 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ public enum Feature {

@JEP(number=430, title="String Templates")
STRING_TEMPLATES,
@JEP(number=443, title="Unnamed Patterns and Variables")
UNNAMED,
@JEP(number=445, title="Unnamed Classes and Instance Main Methods")
UNNAMED_CLASSES,
@JEP(number=446, title="Scoped Values", status="Preview")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ public interface VariableElement extends Element {
* @jls 6.1 Declarations
* @jls 14.4 Local Variable Declarations
*
* @since 21
* @since 22
*/
@PreviewFeature(feature=PreviewFeature.Feature.UNNAMED, reflective = true)
default boolean isUnnamed() { return getSimpleName().isEmpty(); }
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@
*
* @jls 14.30.1 Kinds of Patterns
*
* @since 21
* @since 22
*/
@PreviewFeature(feature=PreviewFeature.Feature.UNNAMED)
public interface AnyPatternTree extends PatternTree {
}
3 changes: 1 addition & 2 deletions src/jdk.compiler/share/classes/com/sun/source/tree/Tree.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,8 @@ public enum Kind {
/**
* Used for instances of {@link BindingPatternTree}.
*
* @since 21
* @since 22
*/
@PreviewFeature(feature=PreviewFeature.Feature.UNNAMED)
ANY_PATTERN(AnyPatternTree.class),

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,8 @@ public interface TreeVisitor<R,P> {
* @param node the node being visited
* @param p a parameter value
* @return a result value
* @since 21
* @since 22
*/
@PreviewFeature(feature=PreviewFeature.Feature.UNNAMED)
R visitAnyPattern(AnyPatternTree node, P p);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -649,10 +649,9 @@ public R visitStringTemplate(StringTemplateTree node, P p) {
* @param node {@inheritDoc}
* @param p {@inheritDoc}
* @return the result of {@code defaultAction}
* @since 21
* @since 22
*/
@Override
@PreviewFeature(feature=PreviewFeature.Feature.UNNAMED)
public R visitAnyPattern(AnyPatternTree node, P p) {
return defaultAction(node, p);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ public boolean isPreview(Feature feature) {
return switch (feature) {
case STRING_TEMPLATES -> true;
case UNNAMED_CLASSES -> true;
case UNNAMED_VARIABLES -> true;
//Note: this is a backdoor which allows to optionally treat all features as 'preview' (for testing).
//When real preview features will be added, this method can be implemented to return 'true'
//for those selected features, and 'false' for all the others.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public enum Feature {
STRING_TEMPLATES(JDK21, Fragments.FeatureStringTemplates, DiagKind.PLURAL),
UNNAMED_CLASSES(JDK21, Fragments.FeatureUnnamedClasses, DiagKind.PLURAL),
WARN_ON_ILLEGAL_UTF8(MIN, JDK21),
UNNAMED_VARIABLES(JDK21, Fragments.FeatureUnnamedVariables, DiagKind.PLURAL),
UNNAMED_VARIABLES(JDK22, Fragments.FeatureUnnamedVariables, DiagKind.PLURAL),
;

enum DiagKind {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -653,8 +653,8 @@ protected Name ident(boolean allowClass, boolean asVariable) {
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);
if (Feature.UNNAMED_VARIABLES.allowedInSource(source)) {
log.error(DiagnosticFlag.SYNTAX, token.pos, Errors.UseOfUnderscoreNotAllowedNonVariable);
} else {
log.error(DiagnosticFlag.SYNTAX, token.pos, Errors.UnderscoreAsIdentifier);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3204,8 +3204,12 @@ compiler.err.underscore.as.identifier=\
as of release 9, ''_'' is a keyword, and may not be used as an identifier

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
underscore not allowed here\n\
as of release 9, ''_'' is a keyword, and may not be used as an identifier\n\
as of release 22, ''_'' can be used as a name in the declaration of unnamed patterns, local variables, exception parameters or lambda parameters

compiler.err.use.of.underscore.not.allowed.non.variable=\
underscore not allowed here

compiler.err.use.of.underscore.not.allowed.with.brackets=\
the underscore keyword ''_'' is not allowed to be followed by brackets
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,6 @@ public void visitPatternCaseLabel(JCPatternCaseLabel tree) {
}

@Override
@PreviewFeature(feature=PreviewFeature.Feature.UNNAMED)
public void visitAnyPattern(JCAnyPattern that) {
}

Expand Down
2 changes: 1 addition & 1 deletion test/langtools/tools/javac/T8312163.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* @test /nodynamiccopyright/
* @bug 8312163
* @summary Crash in dominance check when compiling unnamed patterns
* @enablePreview
* @compile/fail/ref=T8312163.out -XDrawDiagnostics T8312163.java
*/

public class T8312163 {
sealed interface A permits B {}
record B() implements A {}
Expand Down
2 changes: 0 additions & 2 deletions test/langtools/tools/javac/T8312163.out
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@ T8312163.java:33:18: compiler.err.pattern.dominated
T8312163.java:39:18: compiler.err.pattern.dominated
T8312163.java:49:18: compiler.err.pattern.dominated
T8312163.java:54:18: compiler.err.pattern.dominated
- compiler.note.preview.filename: T8312163.java, DEFAULT
- compiler.note.preview.recompile
6 errors
2 changes: 1 addition & 1 deletion test/langtools/tools/javac/T8314216.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @test /nodynamiccopyright/
* @bug 8314216
* @summary Multiple patterns without unnamed variables
* @compile/fail/ref=T8314216.out -XDrawDiagnostics --enable-preview --source ${jdk.version} T8314216.java
* @compile/fail/ref=T8314216.out -XDrawDiagnostics T8314216.java
*/

public class T8314216 {
Expand Down
2 changes: 0 additions & 2 deletions test/langtools/tools/javac/T8314216.out
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
T8314216.java:13:23: compiler.err.invalid.case.label.combination
T8314216.java:14:28: compiler.err.invalid.case.label.combination
- compiler.note.preview.filename: T8314216.java, DEFAULT
- compiler.note.preview.recompile
2 errors
4 changes: 2 additions & 2 deletions test/langtools/tools/javac/T8314423.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* @test /nodynamiccopyright/
* @bug 8314423
* @summary Multiple patterns without unnamed variables
* @compile/fail/ref=T8314423.out -XDrawDiagnostics T8314423.java
* @compile --enable-preview --source ${jdk.version} T8314423.java
* @compile/fail/ref=T8314423.out -XDrawDiagnostics --release 21 T8314423.java
* @compile T8314423.java
*/

public class T8314423 {
Expand Down
4 changes: 2 additions & 2 deletions test/langtools/tools/javac/T8314423.out
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
T8314423.java:15:24: compiler.err.preview.feature.disabled.plural: (compiler.misc.feature.unnamed.variables)
1 error
T8314423.java:15:24: compiler.err.feature.not.supported.in.source.plural: (compiler.misc.feature.unnamed.variables), 21, 22
1 error
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
* @test /nodynamiccopyright/
* @bug 8304246
* @summary Compiler Implementation for Unnamed patterns and variables
* @enablePreview
* @compile/ref=TwrLintUnderscore.out --enable-preview -source ${jdk.version} -Xlint:try -XDrawDiagnostics TwrLintUnderscore.java
* @compile -Xlint:try -XDrawDiagnostics TwrLintUnderscore.java
*/
class TwrLintUnderscore implements AutoCloseable {
private static void test1() {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/

// key: compiler.err.underscore.as.identifier
// options: --release 21

class UnderscoreAsIdentifierError {
String _ = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
* questions.
*/

// key: compiler.err.feature.not.supported.in.source.plural
// key: compiler.misc.feature.unnamed.variables
// key: compiler.warn.preview.feature.use.plural
// options: --enable-preview -source ${jdk.version} -Xlint:preview
// key: compiler.warn.source.no.system.modules.path
// options: -source 21

public class UnderscoreInLambdaExpression {
java.util.function.Function<String,String> f = _ -> "x";
Expand Down
32 changes: 32 additions & 0 deletions test/langtools/tools/javac/diags/examples/UnnamedVariables.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2013, 2014, 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.feature.not.supported.in.source.plural
// key: compiler.misc.feature.unnamed.variables
// options: -Xlint:-options --source 21

class UnnamedVariables {
void test() {
String _ = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
*/

// key: compiler.err.use.of.underscore.not.allowed
// options: --enable-preview -source ${jdk.version} -Xlint:preview

import java.util.function.*;

class UseOfUnderscoreNotAllowed {
IntBinaryOperator f = (int x, int y) -> _ + x;
private int a = 0, _ = 1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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.non.variable

import java.util.function.*;

class UseOfUnderscoreNotAllowedNonVar {
IntBinaryOperator f = (int x, int y) -> _ + x;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
*/

// 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() {
Expand Down
4 changes: 2 additions & 2 deletions test/langtools/tools/javac/lambda/IdentifierTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* @author sogoel
* @summary Test generation of warnings when '_' is used an identifier
* @compile/fail/ref=IdentifierTest8.out --release 8 -Werror -XDrawDiagnostics -Xlint:-options IdentifierTest.java
* @compile/fail/ref=IdentifierTest9.out -XDrawDiagnostics IdentifierTest.java
* @compile/fail/ref=IdentifierTest21.out -source ${jdk.version} --enable-preview -XDrawDiagnostics IdentifierTest.java
* @compile/fail/ref=IdentifierTest9.out --release 9 -XDrawDiagnostics IdentifierTest.java
* @compile/fail/ref=IdentifierTest22.out -XDrawDiagnostics IdentifierTest.java
*/

import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,38 @@
IdentifierTest.java:42:11: compiler.err.use.of.underscore.not.allowed
IdentifierTest.java:45:16: compiler.err.use.of.underscore.not.allowed
IdentifierTest.java:47:22: compiler.err.use.of.underscore.not.allowed
IdentifierTest.java:52:13: compiler.err.use.of.underscore.not.allowed
IdentifierTest.java:52:23: compiler.err.use.of.underscore.not.allowed
IdentifierTest.java:54:13: compiler.err.use.of.underscore.not.allowed
IdentifierTest.java:56:13: compiler.err.use.of.underscore.not.allowed
IdentifierTest.java:64:67: compiler.err.use.of.underscore.not.allowed
IdentifierTest.java:71:13: compiler.err.use.of.underscore.not.allowed
IdentifierTest.java:72:14: compiler.err.use.of.underscore.not.allowed
IdentifierTest.java:73:18: compiler.err.use.of.underscore.not.allowed
IdentifierTest.java:80:13: compiler.err.use.of.underscore.not.allowed
IdentifierTest.java:80:15: compiler.err.use.of.underscore.not.allowed
IdentifierTest.java:82:13: compiler.err.use.of.underscore.not.allowed
IdentifierTest.java:82:15: compiler.err.use.of.underscore.not.allowed
IdentifierTest.java:89:10: compiler.err.use.of.underscore.not.allowed
IdentifierTest.java:89:38: compiler.err.use.of.underscore.not.allowed
IdentifierTest.java:95:14: compiler.err.use.of.underscore.not.allowed
IdentifierTest.java:102:17: compiler.err.use.of.underscore.not.allowed
IdentifierTest.java:102:26: compiler.err.use.of.underscore.not.allowed
IdentifierTest.java:119:20: compiler.err.use.of.underscore.not.allowed
IdentifierTest.java:124:10: compiler.err.use.of.underscore.not.allowed
IdentifierTest.java:129:17: compiler.err.use.of.underscore.not.allowed
IdentifierTest.java:132:17: compiler.err.use.of.underscore.not.allowed
IdentifierTest.java:139:24: compiler.err.use.of.underscore.not.allowed
IdentifierTest.java:139:33: compiler.err.use.of.underscore.not.allowed
IdentifierTest.java:140:39: compiler.err.use.of.underscore.not.allowed
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:42:11: compiler.err.use.of.underscore.not.allowed.non.variable
IdentifierTest.java:45:16: compiler.err.use.of.underscore.not.allowed.non.variable
IdentifierTest.java:47:22: compiler.err.use.of.underscore.not.allowed.non.variable
IdentifierTest.java:52:13: compiler.err.use.of.underscore.not.allowed.non.variable
IdentifierTest.java:52:23: compiler.err.use.of.underscore.not.allowed.non.variable
IdentifierTest.java:54:13: compiler.err.use.of.underscore.not.allowed.non.variable
IdentifierTest.java:56:13: compiler.err.use.of.underscore.not.allowed.non.variable
IdentifierTest.java:64:67: compiler.err.use.of.underscore.not.allowed.non.variable
IdentifierTest.java:71:13: compiler.err.use.of.underscore.not.allowed.non.variable
IdentifierTest.java:72:14: compiler.err.use.of.underscore.not.allowed.non.variable
IdentifierTest.java:73:18: compiler.err.use.of.underscore.not.allowed.non.variable
IdentifierTest.java:80:13: compiler.err.use.of.underscore.not.allowed.non.variable
IdentifierTest.java:80:15: compiler.err.use.of.underscore.not.allowed.non.variable
IdentifierTest.java:82:13: compiler.err.use.of.underscore.not.allowed.non.variable
IdentifierTest.java:82:15: compiler.err.use.of.underscore.not.allowed.non.variable
IdentifierTest.java:89:10: compiler.err.use.of.underscore.not.allowed.non.variable
IdentifierTest.java:89:38: compiler.err.use.of.underscore.not.allowed.non.variable
IdentifierTest.java:95:14: compiler.err.use.of.underscore.not.allowed.non.variable
IdentifierTest.java:102:17: compiler.err.use.of.underscore.not.allowed.non.variable
IdentifierTest.java:102:26: compiler.err.use.of.underscore.not.allowed.non.variable
IdentifierTest.java:119:20: compiler.err.use.of.underscore.not.allowed.non.variable
IdentifierTest.java:124:10: compiler.err.use.of.underscore.not.allowed.non.variable
IdentifierTest.java:129:17: compiler.err.use.of.underscore.not.allowed.non.variable
IdentifierTest.java:132:17: compiler.err.use.of.underscore.not.allowed.non.variable
IdentifierTest.java:139:24: compiler.err.use.of.underscore.not.allowed.non.variable
IdentifierTest.java:139:33: compiler.err.use.of.underscore.not.allowed.non.variable
IdentifierTest.java:140:39: compiler.err.use.of.underscore.not.allowed.non.variable
IdentifierTest.java:145:15: compiler.err.use.of.underscore.not.allowed.non.variable
IdentifierTest.java:146:13: compiler.err.use.of.underscore.not.allowed.non.variable
IdentifierTest.java:151:15: compiler.err.use.of.underscore.not.allowed.non.variable
IdentifierTest.java:152:17: compiler.err.use.of.underscore.not.allowed.non.variable
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
IdentifierTest.java:160:25: compiler.err.use.of.underscore.not.allowed.non.variable
IdentifierTest.java:169:5: compiler.err.use.of.underscore.not.allowed.non.variable
IdentifierTest.java:173:26: compiler.err.use.of.underscore.not.allowed.non.variable
IdentifierTest.java:175:19: compiler.err.use.of.underscore.not.allowed.non.variable
IdentifierTest.java:181:11: compiler.err.use.of.underscore.not.allowed.non.variable
37 errors
2 changes: 1 addition & 1 deletion test/langtools/tools/javac/lambda/IdentifierTest9.out
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
IdentifierTest.java:42:11: compiler.err.underscore.as.identifier
IdentifierTest.java:45:16: compiler.err.underscore.as.identifier
IdentifierTest.java:46:20: compiler.err.preview.feature.disabled.plural: (compiler.misc.feature.unnamed.variables)
IdentifierTest.java:46:20: compiler.err.feature.not.supported.in.source.plural: (compiler.misc.feature.unnamed.variables), 9, 22
IdentifierTest.java:47:22: compiler.err.underscore.as.identifier
IdentifierTest.java:52:13: compiler.err.underscore.as.identifier
IdentifierTest.java:52:23: compiler.err.underscore.as.identifier
Expand Down
4 changes: 2 additions & 2 deletions test/langtools/tools/javac/lambda/UnderscoreAsIdent.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* @test /nodynamiccopyright/
* @summary Check usages of underscore as identifier generate warnings
* @compile/fail/ref=UnderscoreAsIdent8.out --release 8 -XDrawDiagnostics -Xlint:-options -Werror UnderscoreAsIdent.java
* @compile/fail/ref=UnderscoreAsIdent9.out -XDrawDiagnostics -Werror UnderscoreAsIdent.java
* @compile/fail/ref=UnderscoreAsIdent21.out -source ${jdk.version} --enable-preview -XDrawDiagnostics UnderscoreAsIdent.java
* @compile/fail/ref=UnderscoreAsIdent9.out --release 9 -XDrawDiagnostics -Werror UnderscoreAsIdent.java
* @compile/fail/ref=UnderscoreAsIdent22.out -XDrawDiagnostics UnderscoreAsIdent.java
*/
package _._;

Expand Down
Loading

1 comment on commit c9d23c3

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.