Skip to content

Commit 138cdc9

Browse files
committedMar 27, 2023
8304694: Runtime exception thrown when break stmt is missing
Reviewed-by: vromero
1 parent 46b0602 commit 138cdc9

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed
 

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

+6-5
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ private void handleSwitch(JCTree tree,
451451
return l;
452452
});
453453
newCases.add(c.head);
454+
appendBreakIfNeeded(tree, cases, c.head);
454455
}
455456
cases = processCases(tree, newCases.toList());
456457
ListBuffer<JCStatement> statements = new ListBuffer<>();
@@ -595,7 +596,6 @@ private void handleSwitch(JCTree tree,
595596
previousCompletesNormally =
596597
c.caseKind == CaseTree.CaseKind.STATEMENT &&
597598
c.completesNormally;
598-
appendBreakIfNeeded(tree, c);
599599
}
600600

601601
if (tree.hasTag(Tag.SWITCH)) {
@@ -640,9 +640,11 @@ public void visitCase(JCCase c) {
640640
}.scan(c.stats);
641641
}
642642

643-
private void appendBreakIfNeeded(JCTree switchTree, JCCase c) {
644-
if (c.caseKind == CaseTree.CaseKind.RULE) {
645-
JCBreak brk = make.at(TreeInfo.endPos(c.stats.last())).Break(null);
643+
void appendBreakIfNeeded(JCTree switchTree, List<JCCase> cases, JCCase c) {
644+
if (c.caseKind == CaseTree.CaseKind.RULE || (cases.last() == c && c.completesNormally)) {
645+
JCTree pos = c.stats.nonEmpty() ? c.stats.last()
646+
: c;
647+
JCBreak brk = make.at(TreeInfo.endPos(pos)).Break(null);
646648
brk.target = switchTree;
647649
c.stats = c.stats.append(brk);
648650
}
@@ -745,7 +747,6 @@ public void resolve(VarSymbol commonBinding,
745747
} else {
746748
newLabel = List.of(make.PatternCaseLabel(binding, newGuard));
747749
}
748-
appendBreakIfNeeded(currentSwitch, accummulated);
749750
nestedCases.add(make.Case(CaseKind.STATEMENT, newLabel, accummulated.stats, null));
750751
lastGuard = newGuard;
751752
}

‎test/langtools/tools/javac/patterns/DeconstructionDesugaring.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
/**
2525
* @test
26-
* @bug 8291769 8301858
26+
* @bug 8291769 8301858 8304694
2727
* @summary Verify more complex switches work properly
2828
* @compile --enable-preview -source ${jdk.version} DeconstructionDesugaring.java
2929
* @run main/othervm --enable-preview DeconstructionDesugaring
@@ -45,6 +45,8 @@ private void test() {
4545
assertEquals(runCheckExpressionWithUnconditional1(new R5(null)), 3);
4646
assertEquals(runCheckExpressionWithUnconditionalAndParams(new R1(42)), 1);
4747
assertEquals(runCheckExpressionWithUnconditionalAndParams(new R1(new Object())), 2);
48+
assertEquals(runFallThrough(new R6(1, 1)), 0);
49+
assertEquals(runFallThrough(new R6(0, 0)), 1);
4850
}
4951

5052
private void test(ToIntFunction<Object> task) {
@@ -113,6 +115,15 @@ case R1(Object o):
113115
return meth_O(o);
114116
}
115117
}
118+
119+
public static int runFallThrough(R6 r) {
120+
switch (r) {
121+
case R6(var v1, var v2) when v1 != 0: return 0;
122+
case R6(var v1, var v2):
123+
}
124+
return 1;
125+
}
126+
116127
public static int meth_I(Integer i) { return 1; }
117128
public static int meth_O(Object o) { return 2;}
118129

@@ -134,4 +145,5 @@ final class Sub3 extends Super {}
134145

135146
record R4(Super o) {}
136147
record R5(R4 o) {}
148+
record R6(int i1, int i2) {}
137149
}

1 commit comments

Comments
 (1)

openjdk-notifier[bot] commented on Mar 27, 2023

@openjdk-notifier[bot]
Please sign in to comment.