Skip to content

Commit

Permalink
8304694: Runtime exception thrown when break stmt is missing
Browse files Browse the repository at this point in the history
Reviewed-by: vromero
  • Loading branch information
lahodaj committed Mar 27, 2023
1 parent 46b0602 commit 138cdc9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ private void handleSwitch(JCTree tree,
return l;
});
newCases.add(c.head);
appendBreakIfNeeded(tree, cases, c.head);
}
cases = processCases(tree, newCases.toList());
ListBuffer<JCStatement> statements = new ListBuffer<>();
Expand Down Expand Up @@ -595,7 +596,6 @@ private void handleSwitch(JCTree tree,
previousCompletesNormally =
c.caseKind == CaseTree.CaseKind.STATEMENT &&
c.completesNormally;
appendBreakIfNeeded(tree, c);
}

if (tree.hasTag(Tag.SWITCH)) {
Expand Down Expand Up @@ -640,9 +640,11 @@ public void visitCase(JCCase c) {
}.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);
}
Expand Down Expand Up @@ -745,7 +747,6 @@ public void resolve(VarSymbol commonBinding,
} else {
newLabel = List.of(make.PatternCaseLabel(binding, newGuard));
}
appendBreakIfNeeded(currentSwitch, accummulated);
nestedCases.add(make.Case(CaseKind.STATEMENT, newLabel, accummulated.stats, null));
lastGuard = newGuard;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -45,6 +45,8 @@ private void test() {
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) {
Expand Down Expand Up @@ -113,6 +115,15 @@ case R1(Object o):
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;}

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

record R4(Super o) {}
record R5(R4 o) {}
record R6(int i1, int i2) {}
}

1 comment on commit 138cdc9

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.