Skip to content

Commit 521c63d

Browse files
committed
Remove match keyword
1 parent bb76a90 commit 521c63d

File tree

15 files changed

+84
-122
lines changed

15 files changed

+84
-122
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3028,27 +3028,18 @@ List<JCStatement> blockStatement() {
30283028
dc = token.docComment();
30293029
if (isRecordStart() && allowRecords) {
30303030
return List.of(recordDeclaration(F.at(pos).Modifiers(0), dc));
3031-
} else if (isMatchStatementStart() && allowMatchStatements) {
3032-
nextToken();
3031+
} else if (analyzePatternAssignment() == LocalVariableDeclOrRecordPattern.RecordPattern && allowMatchStatements) {
30333032
int patternPos = token.pos;
30343033
JCModifiers mods = optFinal(0);
30353034
JCExpression type = unannotatedType(false);
30363035

30373036
JCPattern pattern = parsePattern(patternPos, mods, type, false, false);
30383037

3039-
if (pattern.getTag() != RECORDPATTERN) {
3040-
log.error(token.pos, Errors.NotADeconstructionPatternForMatchStatement(pattern.toString()));
3041-
3042-
return List.of(toP(F.at(pos).Exec(F.at(patternPos).Erroneous())));
3043-
} else {
3044-
accept(EQ);
3045-
3046-
JCExpression expr = parseExpression();
3047-
3048-
accept(SEMI);
3038+
accept(EQ);
3039+
JCExpression expr = parseExpression();
3040+
accept(SEMI);
30493041

3050-
return List.of(toP(F.at(pos).Match(pattern, expr)));
3051-
}
3042+
return List.of(toP(F.at(pos).Match(pattern, expr)));
30523043
} else {
30533044
Token prevToken = token;
30543045
JCExpression t = term(EXPR | TYPE);
@@ -3122,8 +3113,10 @@ public JCStatement parseSimpleStatement() {
31223113
case FOR: {
31233114
nextToken();
31243115
accept(LPAREN);
3125-
if (S.token().kind == IDENTIFIER && S.token().name() == names.match) {
3126-
nextToken();
3116+
3117+
LocalVariableDeclOrRecordPattern initResult = analyzePatternAssignment();
3118+
3119+
if (initResult == LocalVariableDeclOrRecordPattern.RecordPattern) {
31273120
int patternPos = token.pos;
31283121
JCModifiers mods = optFinal(0);
31293122
JCExpression type = unannotatedType(false);
@@ -3275,22 +3268,13 @@ public JCStatement parseSimpleStatement() {
32753268
}
32763269
}
32773270

3278-
protected boolean isMatchStatementStart() {
3279-
if (token.kind == IDENTIFIER && token.name() == names.match && S.token(1).kind == IDENTIFIER) {
3280-
checkSourceLevel(Feature.MATCH_STATEMENTS);
3281-
return true;
3282-
} else {
3283-
return false;
3284-
}
3285-
}
3286-
3287-
private enum ForInitResult {
3271+
public enum LocalVariableDeclOrRecordPattern {
32883272
LocalVarDecl,
32893273
RecordPattern
32903274
}
32913275

32923276
@SuppressWarnings("fallthrough")
3293-
ForInitResult analyzeForInit() {
3277+
public LocalVariableDeclOrRecordPattern analyzePatternAssignment() {
32943278
boolean inType = false;
32953279
boolean inSelectionAndParenthesis = false;
32963280
int typeParameterPossibleStart = -1;
@@ -3301,38 +3285,46 @@ ForInitResult analyzeForInit() {
33013285
if (inType) break; // in qualified type
33023286
case COMMA:
33033287
typeParameterPossibleStart = lookahead;
3288+
if (peekToken(lookahead, LPAREN)) {
3289+
return LocalVariableDeclOrRecordPattern.LocalVarDecl;
3290+
}
33043291
break;
33053292
case QUES:
33063293
// "?" only allowed in a type parameter position - otherwise it's an expression
33073294
if (typeParameterPossibleStart == lookahead - 1) break;
3308-
else return ForInitResult.LocalVarDecl;
3295+
else return LocalVariableDeclOrRecordPattern.LocalVarDecl;
33093296
case EXTENDS: case SUPER: case AMP:
33103297
case GTGTGT: case GTGT: case GT:
33113298
case FINAL: case ELLIPSIS:
33123299
break;
33133300
case BYTE: case SHORT: case INT: case LONG: case FLOAT:
33143301
case DOUBLE: case BOOLEAN: case CHAR: case VOID:
33153302
if (peekToken(lookahead, IDENTIFIER)) {
3316-
return inSelectionAndParenthesis ? ForInitResult.RecordPattern
3317-
: ForInitResult.LocalVarDecl;
3303+
return inSelectionAndParenthesis ? LocalVariableDeclOrRecordPattern.RecordPattern
3304+
: LocalVariableDeclOrRecordPattern.LocalVarDecl;
33183305
}
33193306
break;
33203307
case LPAREN:
33213308
if (lookahead != 0 && inType) {
33223309
inSelectionAndParenthesis = true;
33233310
inType = false;
33243311
}
3312+
3313+
if (peekToken(lookahead, LPAREN)) { // ID((byte) test)
3314+
return LocalVariableDeclOrRecordPattern.LocalVarDecl;
3315+
}
33253316
break;
33263317
case RPAREN:
3327-
// a method call in the init part or a record pattern?
3318+
// a method call or a record pattern?
33283319
if (inSelectionAndParenthesis) {
33293320
if (peekToken(lookahead, DOT) ||
33303321
peekToken(lookahead, SEMI) ||
33313322
peekToken(lookahead, ARROW)) {
3332-
return ForInitResult.LocalVarDecl;
3323+
return LocalVariableDeclOrRecordPattern.LocalVarDecl;
33333324
}
3334-
else if(peekToken(lookahead, COLON)) {
3335-
return ForInitResult.RecordPattern;
3325+
else if(peekToken(lookahead, COLON) || // in the init part of an enhanced for loop
3326+
peekToken(lookahead, EQ)) { // as a pattern assignment without a binder TODO
3327+
return LocalVariableDeclOrRecordPattern.RecordPattern;
33363328
}
33373329
break;
33383330
}
@@ -3343,6 +3335,9 @@ else if(peekToken(lookahead, COLON)) {
33433335
if (lookahead == 0) {
33443336
inType = true;
33453337
}
3338+
if (peekToken(lookahead, IDENTIFIER) && peekToken(lookahead + 1, LPAREN)) { // String m(int x) for JShell
3339+
return LocalVariableDeclOrRecordPattern.LocalVarDecl;
3340+
}
33463341
break;
33473342
case MONKEYS_AT: {
33483343
int prevLookahead = lookahead;
@@ -3353,18 +3348,28 @@ else if(peekToken(lookahead, COLON)) {
33533348
}
33543349
break;
33553350
}
3356-
case LBRACKET:
3351+
case LBRACKET: // int[].class, A<int[], short[][]>::method, etc
33573352
if (peekToken(lookahead, RBRACKET)) {
3358-
return inSelectionAndParenthesis ? ForInitResult.RecordPattern
3359-
: ForInitResult.LocalVarDecl;
3353+
int i = lookahead;
3354+
while (peekToken(i, RBRACKET) || peekToken(i, LBRACKET)) {
3355+
i+=1;
3356+
3357+
if (!peekToken(i, IDENTIFIER) && (peekToken(i, DOT) || peekToken(i, COLCOL) || peekToken(i, COMMA))) {
3358+
return LocalVariableDeclOrRecordPattern.LocalVarDecl;
3359+
}
3360+
}
3361+
3362+
return inSelectionAndParenthesis ?
3363+
LocalVariableDeclOrRecordPattern.RecordPattern :
3364+
LocalVariableDeclOrRecordPattern.LocalVarDecl;
33603365
}
3361-
return ForInitResult.LocalVarDecl;
3366+
return LocalVariableDeclOrRecordPattern.LocalVarDecl;
33623367
case LT:
33633368
typeParameterPossibleStart = lookahead;
33643369
break;
33653370
default:
33663371
//this includes EOF
3367-
return ForInitResult.LocalVarDecl;
3372+
return LocalVariableDeclOrRecordPattern.LocalVarDecl;
33683373
}
33693374
}
33703375
}

src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -669,10 +669,6 @@ compiler.err.foreach.not.exhaustive.on.type=\
669669
compiler.misc.feature.match.statements=\
670670
match statements
671671

672-
# 0: string
673-
compiler.err.not.a.deconstruction.pattern.for.match.statement=\
674-
match statements can use only deconstruction patterns, {0} is not a deconstruction pattern
675-
676672
compiler.err.fp.number.too.large=\
677673
floating-point number too large
678674

src/jdk.compiler/share/classes/com/sun/tools/javac/util/Names.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ public static Names instance(Context context) {
7373
public final Name when;
7474
public final Name with;
7575
public final Name yield;
76-
public final Name match;
7776

7877
// field and method names
7978
public final Name _name;
@@ -265,7 +264,6 @@ public Names(Context context) {
265264
when = fromString("when");
266265
with = fromString("with");
267266
yield = fromString("yield");
268-
match = fromString("match");
269267

270268
// field and method names
271269
_name = fromString("name");

src/jdk.jshell/share/classes/jdk/jshell/ReplParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ List<? extends JCTree> replUnit(JCModifiers pmods, Comment dc) {
190190
} else {
191191
int pos = token.pos;
192192

193-
if (isMatchStatementStart()) {
193+
if (analyzePatternAssignment() == LocalVariableDeclOrRecordPattern.RecordPattern) {
194194
return List.<JCTree>of(parseStatement());
195195
}
196196

test/langtools/jdk/jshell/MatchStatementsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class MatchStatementsTest extends KullaTesting {
4040
public void testMatchStatement() {
4141
assertEval("record Point(int a) {}");
4242
assertEquals(varKey(assertEval("Point p = new Point(42);")).name(), "p");
43-
assertEval("match Point(int b) = p;");
43+
assertEval("Point(int b) = p;");
4444
// assertEval("b + 1", "43"); // TODO
4545
}
4646

test/langtools/tools/javac/diags/examples/ForeachNotExhaustive.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
class ForeachNotExhaustive {
3131
void m(List<Object> points) {
32-
for (match Point(var x, var y): points) {
32+
for (Point(var x, var y): points) {
3333
System.out.println();
3434
}
3535
}

test/langtools/tools/javac/diags/examples/NotADeconstructionPatternForMatchStatement.java

Lines changed: 0 additions & 32 deletions
This file was deleted.

test/langtools/tools/javac/patterns/DisambiguatePatterns.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -128,21 +128,18 @@ public static void main(String... args) throws Throwable {
128128
test.disambiguationTest("R(int x) when (x > 0)",
129129
ExpressionType.PATTERN);
130130
test.forDisambiguationTest("T[] a", ForType.ENHANCED_FOR);
131-
132131
test.forDisambiguationTest("@Annot(field = \"test\") Point p", ForType.ENHANCED_FOR);
133-
test.forDisambiguationTest("match R(T[] a)", ForType.ENHANCED_FOR_WITH_PATTERNS);
134-
test.forDisambiguationTest("match Point(Integer a, Integer b)", ForType.ENHANCED_FOR_WITH_PATTERNS);
135-
test.forDisambiguationTest("match ForEachPatterns.Point(Integer a, Integer b)", ForType.ENHANCED_FOR_WITH_PATTERNS);
136-
test.forDisambiguationTest("match GPoint<Integer>(Integer a, Integer b)", ForType.ENHANCED_FOR_WITH_PATTERNS);
137-
test.forDisambiguationTest("match GPoint<Point>(Point(Integer a, Integer b), Point c)", ForType.ENHANCED_FOR_WITH_PATTERNS);
138-
test.forDisambiguationTest("match GPoint<Point>(Point(var a, Integer b), Point c)", ForType.ENHANCED_FOR_WITH_PATTERNS);
139-
test.forDisambiguationTest("match GPoint<VoidPoint>(VoidPoint(), VoidPoint())", ForType.ENHANCED_FOR_WITH_PATTERNS);
140-
test.forDisambiguationTest("match RecordOfLists(List<Integer> lr)", ForType.ENHANCED_FOR_WITH_PATTERNS);
141-
test.forDisambiguationTest("match RecordOfLists2(List<List<Integer>> lr)", ForType.ENHANCED_FOR_WITH_PATTERNS);
142-
132+
test.forDisambiguationTest("R(T[] a)", ForType.ENHANCED_FOR_WITH_PATTERNS);
133+
test.forDisambiguationTest("Point(Integer a, Integer b)", ForType.ENHANCED_FOR_WITH_PATTERNS);
134+
test.forDisambiguationTest("ForEachPatterns.Point(Integer a, Integer b)", ForType.ENHANCED_FOR_WITH_PATTERNS);
135+
test.forDisambiguationTest("GPoint<Integer>(Integer a, Integer b)", ForType.ENHANCED_FOR_WITH_PATTERNS);
136+
test.forDisambiguationTest("GPoint<Point>(Point(Integer a, Integer b), Point c)", ForType.ENHANCED_FOR_WITH_PATTERNS);
137+
test.forDisambiguationTest("GPoint<Point>(Point(var a, Integer b), Point c)", ForType.ENHANCED_FOR_WITH_PATTERNS);
138+
test.forDisambiguationTest("GPoint<VoidPoint>(VoidPoint(), VoidPoint())", ForType.ENHANCED_FOR_WITH_PATTERNS);
139+
test.forDisambiguationTest("RecordOfLists(List<Integer> lr)", ForType.ENHANCED_FOR_WITH_PATTERNS);
140+
test.forDisambiguationTest("RecordOfLists2(List<List<Integer>> lr)", ForType.ENHANCED_FOR_WITH_PATTERNS);
143141
test.forDisambiguationTest("T[].class.getName()", ForType.TRADITIONAL_FOR);
144142
test.forDisambiguationTest("T[].class", ForType.TRADITIONAL_FOR, "compiler.err.not.stmt");
145-
146143
test.forDisambiguationTest("method()", ForType.TRADITIONAL_FOR);
147144
test.forDisambiguationTest("method(), method()", ForType.TRADITIONAL_FOR);
148145
test.forDisambiguationTest("method2((Integer a) -> 42)", ForType.TRADITIONAL_FOR);

test/langtools/tools/javac/patterns/ForEachPatterns.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,54 +54,54 @@ public static void main(String[] args) {
5454

5555
static int iteratorEnhancedFor(List<Point> points) {
5656
int result = 0;
57-
for (match Point(Integer a, Integer b) : points) {
57+
for (Point(Integer a, Integer b) : points) {
5858
result += a + b;
5959
}
6060
return result;
6161
}
6262

6363
static int arrayEnhancedFor(Point[] points) {
6464
int result = 0;
65-
for (match Point(Integer a, Integer b) : points) {
65+
for (Point(Integer a, Integer b) : points) {
6666
result += a + b;
6767
}
6868
return result;
6969
}
7070

7171
static int simpleDecostructionPatternWithAccesses(List<Point> points) {
7272
int result = 0;
73-
for (match Point(var a, var b): points) {
73+
for (Point(var a, var b): points) {
7474
result += a + b;
7575
}
7676
return result;
7777
}
7878

7979
static int simpleDecostructionPatternException(List<PointEx> points) {
8080
int result = 0;
81-
for (match PointEx(var a, var b): points) {
81+
for (PointEx(var a, var b): points) {
8282
result += a + b;
8383
}
8484
return result;
8585
}
8686

8787
static int simpleDecostructionPatternNoComponentAccess(List<Point> points) {
8888
int result = 0;
89-
for (match Point(var a, var b): points) {
89+
for (Point(var a, var b): points) {
9090
result += 1;
9191
}
9292
return result;
9393
}
9494

9595
static int varAndConcrete(List<Point> points) {
9696
int result = 0;
97-
for (match Point(Integer a, var b): points) {
97+
for (Point(Integer a, var b): points) {
9898
result += a + b;
9999
}
100100
return result;
101101
}
102102

103103
static int returnFromEnhancedFor(List<Point> points) {
104-
for (match Point(var a, var b): points) {
104+
for (Point(var a, var b): points) {
105105
return a + b;
106106
}
107107
return -1;
@@ -110,7 +110,7 @@ static int returnFromEnhancedFor(List<Point> points) {
110110
static int breakFromEnhancedFor(List<Point> points) {
111111
int i = 1;
112112
int result = 0;
113-
for (match Point(var a, var b): points) {
113+
for (Point(var a, var b): points) {
114114
if (i == 1) break;
115115
else result += a + b;
116116
}
@@ -120,7 +120,7 @@ static int breakFromEnhancedFor(List<Point> points) {
120120
static int sealedRecordPassBaseType(List<IPoint> points) {
121121
int result = 0;
122122

123-
for(match Point(var x, var y) : points) {
123+
for(Point(var x, var y) : points) {
124124
result += (x + y);
125125
}
126126

@@ -129,7 +129,7 @@ static int sealedRecordPassBaseType(List<IPoint> points) {
129129

130130
static int withPrimitives(List<WithPrimitives> points) {
131131
int result = 0;
132-
for (match WithPrimitives(int a, double b): points) {
132+
for (WithPrimitives(int a, double b): points) {
133133
result += a + (int) b;
134134
}
135135
return result;
@@ -171,14 +171,14 @@ static List<Color> JEPExample() {
171171
//where
172172
static List<Color> printUpperLeftColors(Rectangle[] r) {
173173
List<Color> ret = new ArrayList<>();
174-
for (match Rectangle(ColoredPoint(Point p, Color c), ColoredPoint lr): r) {
174+
for (Rectangle(ColoredPoint(Point p, Color c), ColoredPoint lr): r) {
175175
ret.add(c);
176176
}
177177
return ret;
178178
}
179179

180180
static int arrayWithSealed(IParent[] recs){
181-
for (match Rec(int a) : recs) {
181+
for (Rec(int a) : recs) {
182182
return a;
183183
}
184184
return -1;

0 commit comments

Comments
 (0)