Skip to content

Commit a99b4fa

Browse files
committed
Remove match keyword
1 parent ee6ad6b commit a99b4fa

File tree

15 files changed

+79
-120
lines changed

15 files changed

+79
-120
lines changed

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

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3019,27 +3019,18 @@ List<JCStatement> blockStatement() {
30193019
dc = token.docComment();
30203020
if (isRecordStart() && allowRecords) {
30213021
return List.of(recordDeclaration(F.at(pos).Modifiers(0), dc));
3022-
} else if (isMatchStatementStart() && allowMatchStatements) {
3023-
nextToken();
3022+
} else if (analyzePatternAssignment() == LocalVariableDeclOrRecordPattern.RecordPattern && allowMatchStatements) {
30243023
int patternPos = token.pos;
30253024
JCModifiers mods = optFinal(0);
30263025
JCExpression type = unannotatedType(false);
30273026

30283027
JCPattern pattern = parsePattern(patternPos, mods, type, false, false);
30293028

3030-
if (pattern.getTag() != RECORDPATTERN) {
3031-
log.error(token.pos, Errors.NotADeconstructionPatternForMatchStatement(pattern.toString()));
3032-
3033-
return List.of(toP(F.at(pos).Exec(F.at(patternPos).Erroneous())));
3034-
} else {
3035-
accept(EQ);
3036-
3037-
JCExpression expr = parseExpression();
3038-
3039-
accept(SEMI);
3029+
accept(EQ);
3030+
JCExpression expr = parseExpression();
3031+
accept(SEMI);
30403032

3041-
return List.of(toP(F.at(pos).Match(pattern, expr)));
3042-
}
3033+
return List.of(toP(F.at(pos).Match(pattern, expr)));
30433034
} else {
30443035
Token prevToken = token;
30453036
JCExpression t = term(EXPR | TYPE);
@@ -3113,8 +3104,10 @@ public JCStatement parseSimpleStatement() {
31133104
case FOR: {
31143105
nextToken();
31153106
accept(LPAREN);
3116-
if (S.token().kind == IDENTIFIER && S.token().name() == names.match) {
3117-
nextToken();
3107+
3108+
LocalVariableDeclOrRecordPattern initResult = analyzePatternAssignment();
3109+
3110+
if (initResult == LocalVariableDeclOrRecordPattern.RecordPattern) {
31183111
int patternPos = token.pos;
31193112
JCModifiers mods = optFinal(0);
31203113
JCExpression type = unannotatedType(false);
@@ -3266,22 +3259,13 @@ public JCStatement parseSimpleStatement() {
32663259
}
32673260
}
32683261

3269-
protected boolean isMatchStatementStart() {
3270-
if (token.kind == IDENTIFIER && token.name() == names.match && S.token(1).kind == IDENTIFIER) {
3271-
checkSourceLevel(Feature.MATCH_STATEMENTS);
3272-
return true;
3273-
} else {
3274-
return false;
3275-
}
3276-
}
3277-
3278-
private enum ForInitResult {
3262+
public enum LocalVariableDeclOrRecordPattern {
32793263
LocalVarDecl,
32803264
RecordPattern
32813265
}
32823266

32833267
@SuppressWarnings("fallthrough")
3284-
ForInitResult analyzeForInit() {
3268+
public LocalVariableDeclOrRecordPattern analyzePatternAssignment() {
32853269
boolean inType = false;
32863270
boolean inSelectionAndParenthesis = false;
32873271
int typeParameterPossibleStart = -1;
@@ -3292,38 +3276,46 @@ ForInitResult analyzeForInit() {
32923276
if (inType) break; // in qualified type
32933277
case COMMA:
32943278
typeParameterPossibleStart = lookahead;
3279+
if (peekToken(lookahead, LPAREN)) {
3280+
return LocalVariableDeclOrRecordPattern.LocalVarDecl;
3281+
}
32953282
break;
32963283
case QUES:
32973284
// "?" only allowed in a type parameter position - otherwise it's an expression
32983285
if (typeParameterPossibleStart == lookahead - 1) break;
3299-
else return ForInitResult.LocalVarDecl;
3286+
else return LocalVariableDeclOrRecordPattern.LocalVarDecl;
33003287
case EXTENDS: case SUPER: case AMP:
33013288
case GTGTGT: case GTGT: case GT:
33023289
case FINAL: case ELLIPSIS:
33033290
break;
33043291
case BYTE: case SHORT: case INT: case LONG: case FLOAT:
33053292
case DOUBLE: case BOOLEAN: case CHAR: case VOID:
33063293
if (peekToken(lookahead, IDENTIFIER)) {
3307-
return inSelectionAndParenthesis ? ForInitResult.RecordPattern
3308-
: ForInitResult.LocalVarDecl;
3294+
return inSelectionAndParenthesis ? LocalVariableDeclOrRecordPattern.RecordPattern
3295+
: LocalVariableDeclOrRecordPattern.LocalVarDecl;
33093296
}
33103297
break;
33113298
case LPAREN:
33123299
if (lookahead != 0 && inType) {
33133300
inSelectionAndParenthesis = true;
33143301
inType = false;
33153302
}
3303+
3304+
if (peekToken(lookahead, LPAREN)) { // ID((byte) test)
3305+
return LocalVariableDeclOrRecordPattern.LocalVarDecl;
3306+
}
33163307
break;
33173308
case RPAREN:
33183309
// a method call in the init part or a record pattern?
33193310
if (inSelectionAndParenthesis) {
33203311
if (peekToken(lookahead, DOT) ||
33213312
peekToken(lookahead, SEMI) ||
33223313
peekToken(lookahead, ARROW)) {
3323-
return ForInitResult.LocalVarDecl;
3314+
return LocalVariableDeclOrRecordPattern.LocalVarDecl;
33243315
}
3325-
else if(peekToken(lookahead, COLON)) {
3326-
return ForInitResult.RecordPattern;
3316+
else if(peekToken(lookahead, COLON) || // in the init part of an enhanced for loop
3317+
peekToken(lookahead, EQ)) { // as a pattern assignment without a binder TODO
3318+
return LocalVariableDeclOrRecordPattern.RecordPattern;
33273319
}
33283320
break;
33293321
}
@@ -3346,16 +3338,26 @@ else if(peekToken(lookahead, COLON)) {
33463338
}
33473339
case LBRACKET:
33483340
if (peekToken(lookahead, RBRACKET)) {
3349-
return inSelectionAndParenthesis ? ForInitResult.RecordPattern
3350-
: ForInitResult.LocalVarDecl;
3341+
int i = lookahead;
3342+
while (peekToken(i, RBRACKET) || peekToken(i, LBRACKET)) {
3343+
i+=1;
3344+
3345+
if (!peekToken(i, IDENTIFIER) && (peekToken(i, DOT) || peekToken(i, COLCOL) || peekToken(i, COMMA))) {
3346+
return LocalVariableDeclOrRecordPattern.LocalVarDecl;
3347+
}
3348+
}
3349+
3350+
return inSelectionAndParenthesis ?
3351+
LocalVariableDeclOrRecordPattern.RecordPattern :
3352+
LocalVariableDeclOrRecordPattern.LocalVarDecl;
33513353
}
3352-
return ForInitResult.LocalVarDecl;
3354+
return LocalVariableDeclOrRecordPattern.LocalVarDecl;
33533355
case LT:
33543356
typeParameterPossibleStart = lookahead;
33553357
break;
33563358
default:
33573359
//this includes EOF
3358-
return ForInitResult.LocalVarDecl;
3360+
return LocalVariableDeclOrRecordPattern.LocalVarDecl;
33593361
}
33603362
}
33613363
}

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
@@ -47,7 +47,7 @@ public class MatchStatementsTest extends KullaTesting {
4747
public void testMatchStatement() {
4848
assertEval("record Point(int a) {}");
4949
assertEquals(varKey(assertEval("Point p = new Point(42);")).name(), "p");
50-
assertEval("match Point(int b) = p;");
50+
assertEval("Point(int b) = p;");
5151
// assertEval("b + 1", "43"); // TODO
5252
}
5353

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)