8304694: Runtime exception thrown when break stmt is missing

Reviewed-by: vromero
This commit is contained in:
Jan Lahoda 2023-03-27 15:01:45 +00:00
parent 46b0602376
commit 138cdc9283
2 changed files with 19 additions and 6 deletions

View File

@ -451,6 +451,7 @@ public class TransPatterns extends TreeTranslator {
return l;
});
newCases.add(c.head);
appendBreakIfNeeded(tree, cases, c.head);
}
cases = processCases(tree, newCases.toList());
ListBuffer<JCStatement> statements = new ListBuffer<>();
@ -595,7 +596,6 @@ public class TransPatterns extends TreeTranslator {
previousCompletesNormally =
c.caseKind == CaseTree.CaseKind.STATEMENT &&
c.completesNormally;
appendBreakIfNeeded(tree, c);
}
if (tree.hasTag(Tag.SWITCH)) {
@ -640,9 +640,11 @@ public class TransPatterns extends TreeTranslator {
}.scan(c.stats);
}
private void appendBreakIfNeeded(JCTree switchTree, JCCase c) {
if (c.caseKind == CaseTree.CaseKind.RULE) {
JCBreak brk = make.at(TreeInfo.endPos(c.stats.last())).Break(null);
void appendBreakIfNeeded(JCTree switchTree, List<JCCase> cases, JCCase c) {
if (c.caseKind == CaseTree.CaseKind.RULE || (cases.last() == c && c.completesNormally)) {
JCTree pos = c.stats.nonEmpty() ? c.stats.last()
: c;
JCBreak brk = make.at(TreeInfo.endPos(pos)).Break(null);
brk.target = switchTree;
c.stats = c.stats.append(brk);
}
@ -745,7 +747,6 @@ public class TransPatterns extends TreeTranslator {
} else {
newLabel = List.of(make.PatternCaseLabel(binding, newGuard));
}
appendBreakIfNeeded(currentSwitch, accummulated);
nestedCases.add(make.Case(CaseKind.STATEMENT, newLabel, accummulated.stats, null));
lastGuard = newGuard;
}

View File

@ -23,7 +23,7 @@
/**
* @test
* @bug 8291769 8301858
* @bug 8291769 8301858 8304694
* @summary Verify more complex switches work properly
* @compile --enable-preview -source ${jdk.version} DeconstructionDesugaring.java
* @run main/othervm --enable-preview DeconstructionDesugaring
@ -45,6 +45,8 @@ public class DeconstructionDesugaring {
assertEquals(runCheckExpressionWithUnconditional1(new R5(null)), 3);
assertEquals(runCheckExpressionWithUnconditionalAndParams(new R1(42)), 1);
assertEquals(runCheckExpressionWithUnconditionalAndParams(new R1(new Object())), 2);
assertEquals(runFallThrough(new R6(1, 1)), 0);
assertEquals(runFallThrough(new R6(0, 0)), 1);
}
private void test(ToIntFunction<Object> task) {
@ -113,6 +115,15 @@ public class DeconstructionDesugaring {
return meth_O(o);
}
}
public static int runFallThrough(R6 r) {
switch (r) {
case R6(var v1, var v2) when v1 != 0: return 0;
case R6(var v1, var v2):
}
return 1;
}
public static int meth_I(Integer i) { return 1; }
public static int meth_O(Object o) { return 2;}
@ -134,4 +145,5 @@ public class DeconstructionDesugaring {
record R4(Super o) {}
record R5(R4 o) {}
record R6(int i1, int i2) {}
}