Skip to content

Commit 8aa5028

Browse files
biboudislahodaj
andcommitted
8302344: Compiler Implementation for Unnamed patterns and variables (Preview)
8307444: java.lang.AssertionError when using unnamed patterns 8307482: Compiler should accept var _ in nested patterns in switch case 8307007: Implementation for javax.lang.model for unnamed variables (Preview) 8308312: Compiler should fail when a local variable declaration does not include an Identifier and does not have an initializer 8308309: Compiler should accept mixed masked and unmasked variables in lambda parameters Co-authored-by: Jan Lahoda <jlahoda@openjdk.org> Co-authored-by: Aggelos Biboudis <abimpoudis@openjdk.org> Reviewed-by: vromero, darcy
1 parent b588797 commit 8aa5028

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1636
-449
lines changed

src/java.base/share/classes/jdk/internal/javac/PreviewFeature.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ public enum Feature {
7070
FOREIGN,
7171
@JEP(number=430, title="String Templates", status="First Preview")
7272
STRING_TEMPLATES,
73+
@JEP(number=443, title="Unnamed Patterns and Variables")
74+
UNNAMED,
7375
/**
7476
* A key for testing.
7577
*/

src/java.compiler/share/classes/javax/lang/model/element/Element.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,10 @@ public interface Element extends javax.lang.model.AnnotatedConstruct {
131131
* {@code java.util.Set<E>} is {@code "Set"}.
132132
*
133133
* If this element represents an unnamed {@linkplain
134-
* PackageElement#getSimpleName package} or unnamed {@linkplain
135-
* ModuleElement#getSimpleName module}, an {@linkplain
136-
* Name##empty_name empty name} is returned.
134+
* PackageElement#getSimpleName package}, an unnamed {@linkplain
135+
* ModuleElement#getSimpleName module} or an unnamed {@linkplain
136+
* VariableElement#getSimpleName variable}, an {@linkplain Name##empty_name empty name}
137+
* is returned.
137138
*
138139
* If it represents a {@linkplain ExecutableElement#getSimpleName
139140
* constructor}, the name "{@code <init>}" is returned. If it

src/java.compiler/share/classes/javax/lang/model/element/VariableElement.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
package javax.lang.model.element;
2727

28+
import jdk.internal.javac.PreviewFeature;
29+
2830
import javax.lang.model.util.Elements;
2931
import javax.lang.model.type.TypeMirror;
3032
import javax.lang.model.type.TypeKind;
@@ -81,6 +83,9 @@ public interface VariableElement extends Element {
8183
* parameters of the same executable. If the original source
8284
* names are not available, an implementation may synthesize names
8385
* subject to the distinctness requirement above.
86+
*
87+
* <p>For variables, the name of each variable is returned, or an empty name
88+
* if the variable is unnamed.
8489
*/
8590
@Override
8691
Name getSimpleName();
@@ -93,4 +98,21 @@ public interface VariableElement extends Element {
9398
*/
9499
@Override
95100
Element getEnclosingElement();
101+
102+
/**
103+
* {@return {@code true} if this is an unnamed variable and {@code
104+
* false} otherwise}
105+
*
106+
* @implSpec
107+
* The default implementation of this method calls {@code
108+
* getSimpleName()} and returns {@code true} if the result is
109+
* empty and {@code false} otherwise.
110+
*
111+
* @jls 6.1 Declarations
112+
* @jls 14.4 Local Variable Declarations
113+
*
114+
* @since 21
115+
*/
116+
@PreviewFeature(feature=PreviewFeature.Feature.UNNAMED, reflective = true)
117+
default boolean isUnnamed() { return getSimpleName().isEmpty(); }
96118
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.sun.source.tree;
26+
27+
import jdk.internal.javac.PreviewFeature;
28+
29+
/**
30+
* A tree node for a binding pattern that matches a pattern
31+
* with a variable of any name and a type of the match candidate;
32+
* an unnamed pattern.
33+
*
34+
* For example the use of underscore {@code _} below:
35+
* <pre>
36+
* if (r instanceof R(_)) {}
37+
* </pre>
38+
*
39+
* @jls 14.30.1 Kinds of Patterns
40+
*
41+
* @since 21
42+
*/
43+
@PreviewFeature(feature=PreviewFeature.Feature.UNNAMED)
44+
public interface AnyPatternTree extends PatternTree {
45+
}

src/jdk.compiler/share/classes/com/sun/source/tree/Tree.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,14 @@ public enum Kind {
227227
*/
228228
PARENTHESIZED(ParenthesizedTree.class),
229229

230+
/**
231+
* Used for instances of {@link BindingPatternTree}.
232+
*
233+
* @since 21
234+
*/
235+
@PreviewFeature(feature=PreviewFeature.Feature.UNNAMED)
236+
ANY_PATTERN(AnyPatternTree.class),
237+
230238
/**
231239
* Used for instances of {@link BindingPatternTree}.
232240
*

src/jdk.compiler/share/classes/com/sun/source/tree/TreeVisitor.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,16 @@ public interface TreeVisitor<R,P> {
268268
@PreviewFeature(feature=PreviewFeature.Feature.STRING_TEMPLATES, reflective=true)
269269
R visitStringTemplate(StringTemplateTree node, P p);
270270

271+
/**
272+
* Visits a {@code AnyPatternTree} node.
273+
* @param node the node being visited
274+
* @param p a parameter value
275+
* @return a result value
276+
* @since 21
277+
*/
278+
@PreviewFeature(feature=PreviewFeature.Feature.UNNAMED)
279+
R visitAnyPattern(AnyPatternTree node, P p);
280+
271281
/**
272282
* Visits a {@code BindingPatternTree} node.
273283
* @param node the node being visited

src/jdk.compiler/share/classes/com/sun/source/tree/VariableTree.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ public interface VariableTree extends StatementTree {
5151
ModifiersTree getModifiers();
5252

5353
/**
54-
* Returns the name of the variable being declared.
54+
* Returns the name of the variable being declared or empty name if both the variable
55+
* is unnamed and the preview features are enabled (Unnamed Patterns and Variables).
5556
* @return the name
5657
*/
5758
Name getName();

src/jdk.compiler/share/classes/com/sun/source/util/SimpleTreeVisitor.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,22 @@ public R visitStringTemplate(StringTemplateTree node, P p) {
641641
return defaultAction(node, p);
642642
}
643643

644+
/**
645+
* {@inheritDoc}
646+
*
647+
* @implSpec This implementation calls {@code defaultAction}.
648+
*
649+
* @param node {@inheritDoc}
650+
* @param p {@inheritDoc}
651+
* @return the result of {@code defaultAction}
652+
* @since 21
653+
*/
654+
@Override
655+
@PreviewFeature(feature=PreviewFeature.Feature.UNNAMED)
656+
public R visitAnyPattern(AnyPatternTree node, P p) {
657+
return defaultAction(node, p);
658+
}
659+
644660
/**
645661
* {@inheritDoc}
646662
*

src/jdk.compiler/share/classes/com/sun/source/util/TreeScanner.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,21 @@ public R visitInstanceOf(InstanceOfTree node, P p) {
760760
return r;
761761
}
762762

763+
/**
764+
* {@inheritDoc}
765+
*
766+
* @implSpec This implementation returns {@code null}.
767+
*
768+
* @param node {@inheritDoc}
769+
* @param p {@inheritDoc}
770+
* @return the result of scanning
771+
* @since 21
772+
*/
773+
@Override
774+
public R visitAnyPattern(AnyPatternTree node, P p) {
775+
return null;
776+
}
777+
763778
/**
764779
* {@inheritDoc}
765780
*

src/jdk.compiler/share/classes/com/sun/tools/javac/code/Preview.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public boolean isEnabled() {
210210
public boolean isPreview(Feature feature) {
211211
return switch (feature) {
212212
case STRING_TEMPLATES -> true;
213-
213+
case UNNAMED_VARIABLES -> true;
214214
//Note: this is a backdoor which allows to optionally treat all features as 'preview' (for testing).
215215
//When real preview features will be added, this method can be implemented to return 'true'
216216
//for those selected features, and 'false' for all the others.

0 commit comments

Comments
 (0)