Skip to content

Commit 815e4af

Browse files
committed
8269802: javac fails to compile nested pattern matching switches
8269808: javac generates class with invalid stack map Reviewed-by: mcimadamore
1 parent 2daf39a commit 815e4af

2 files changed

Lines changed: 124 additions & 1 deletion

File tree

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TransPatterns.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ private void handleSwitch(JCTree tree,
386386
hasNullCase = true;
387387
}
388388
}
389+
selector = translate(selector);
389390
statements.append(make.at(tree.pos).VarDef(temp, !hasNullCase ? attr.makeNullCheck(selector)
390391
: selector));
391392
VarSymbol index = new VarSymbol(Flags.SYNTHETIC,
@@ -469,6 +470,8 @@ private void handleSwitch(JCTree tree,
469470
currentValue = prevCurrentValue;
470471
bindingContext.pop();
471472
}
473+
} else {
474+
c.stats = translate(c.stats);
472475
}
473476
if (enumSwitch) {
474477
var labels = c.labels;

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

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@
2222
*/
2323

2424
import java.util.Objects;
25+
import java.util.function.BiFunction;
2526
import java.util.function.Consumer;
2627
import java.util.function.Function;
2728

2829
/*
2930
* @test
30-
* @bug 8262891 8268333 8268896
31+
* @bug 8262891 8268333 8268896 8269802 8269808
3132
* @summary Check behavior of pattern switches.
3233
* @compile --enable-preview -source ${jdk.version} Switches.java
3334
* @run main/othervm --enable-preview Switches
@@ -66,6 +67,11 @@ void run() {
6667
npeTest(this::npeTestExpression);
6768
exhaustiveStatementSane("");
6869
exhaustiveStatementSane(null);
70+
switchNestingTest(this::switchNestingStatementStatement);
71+
switchNestingTest(this::switchNestingStatementExpression);
72+
switchNestingTest(this::switchNestingExpressionStatement);
73+
switchNestingTest(this::switchNestingExpressionExpression);
74+
switchNestingTest(this::switchNestingIfSwitch);
6975
}
7076

7177
void run(Function<Object, Integer> mapper) {
@@ -119,6 +125,13 @@ void npeTest(Consumer<I> testCase) {
119125
}
120126
}
121127

128+
void switchNestingTest(BiFunction<Object, Object, String> testCase) {
129+
assertEquals("string, string", testCase.apply("", ""));
130+
assertEquals("string, other", testCase.apply("", 1));
131+
assertEquals("other, string", testCase.apply(1, ""));
132+
assertEquals("other, other", testCase.apply(1, 1));
133+
}
134+
122135
int typeTestPatternSwitchTest(Object o) {
123136
switch (o) {
124137
case String s: return Integer.parseInt(s.toString());
@@ -366,6 +379,113 @@ void exhaustiveStatementSane(Object o) {
366379
}
367380
}
368381

382+
String switchNestingStatementStatement(Object o1, Object o2) {
383+
switch (o1) {
384+
case String s1 -> {
385+
switch (o2) {
386+
case String s2 -> {
387+
return "string, string";
388+
}
389+
default -> {
390+
return "string, other";
391+
}
392+
}
393+
}
394+
default -> {
395+
switch (o2) {
396+
case String s2 -> {
397+
return "other, string";
398+
}
399+
default -> {
400+
return "other, other";
401+
}
402+
}
403+
}
404+
}
405+
}
406+
407+
String switchNestingStatementExpression(Object o1, Object o2) {
408+
switch (o1) {
409+
case String s1 -> {
410+
return switch (o2) {
411+
case String s2 -> "string, string";
412+
default -> "string, other";
413+
};
414+
}
415+
default -> {
416+
return switch (o2) {
417+
case String s2 -> "other, string";
418+
default -> "other, other";
419+
};
420+
}
421+
}
422+
}
423+
424+
String switchNestingExpressionStatement(Object o1, Object o2) {
425+
return switch (o1) {
426+
case String s1 -> {
427+
switch (o2) {
428+
case String s2 -> {
429+
yield "string, string";
430+
}
431+
default -> {
432+
yield "string, other";
433+
}
434+
}
435+
}
436+
default -> {
437+
switch (o2) {
438+
case String s2 -> {
439+
yield "other, string";
440+
}
441+
default -> {
442+
yield "other, other";
443+
}
444+
}
445+
}
446+
};
447+
}
448+
449+
String switchNestingExpressionExpression(Object o1, Object o2) {
450+
return switch (o1) {
451+
case String s1 ->
452+
switch (o2) {
453+
case String s2 -> "string, string";
454+
default -> "string, other";
455+
};
456+
default ->
457+
switch (o2) {
458+
case String s2 -> "other, string";
459+
default -> "other, other";
460+
};
461+
};
462+
}
463+
464+
String switchNestingIfSwitch(Object o1, Object o2) {
465+
BiFunction<Object, Object, String> f = (n1, n2) -> {
466+
if (o1 instanceof CharSequence cs) {
467+
return switch (cs) {
468+
case String s1 ->
469+
switch (o2) {
470+
case String s2 -> "string, string";
471+
default -> "string, other";
472+
};
473+
default ->
474+
switch (o2) {
475+
case String s2 -> "other, string";
476+
default -> "other, other";
477+
};
478+
};
479+
} else {
480+
return switch (o2) {
481+
case String s2 -> "other, string";
482+
default -> "other, other";
483+
};
484+
}
485+
};
486+
return f.apply(o1, o2);
487+
}
488+
369489
//verify that for cases like:
370490
//case ConstantClassClash ->
371491
//ConstantClassClash is interpreted as a field, not as a class

0 commit comments

Comments
 (0)