Skip to content

Commit

Permalink
Improving the desugared code.
Browse files Browse the repository at this point in the history
  • Loading branch information
lahodaj committed Sep 8, 2023
1 parent 52bf0e3 commit 283b0b0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -680,15 +680,30 @@ private void handleSwitch(JCTree tree,
// ...
// }
private static void patchCompletingNormallyCases(List<JCCase> cases) {
for (int j = cases.size() - 1; j >= 0; j--) {
var currentCase = cases.get(j);

if (currentCase != cases.last() && (currentCase.guard != null || TreeInfo.isNullCaseLabel(currentCase.labels.head)) &&
currentCase.caseKind == CaseKind.STATEMENT &&
currentCase.completesNormally) {
var nextCase = cases.get(j + 1);
currentCase.stats = currentCase.stats.appendList(nextCase.stats);
while (cases.nonEmpty()) {
var currentCase = cases.head;

if (currentCase.caseKind == CaseKind.STATEMENT &&
currentCase.completesNormally &&
cases.tail.nonEmpty() &&
cases.tail.head.guard != null) {
ListBuffer<JCStatement> newStatements = new ListBuffer<>();
List<JCCase> copyFrom = cases;

while (copyFrom.nonEmpty()) {
newStatements.appendList(copyFrom.head.stats);

if (!copyFrom.head.completesNormally) {
break;
}

copyFrom = copyFrom.tail;
};

currentCase.stats = newStatements.toList();
}

cases = cases.tail;
}
}

Expand Down
20 changes: 13 additions & 7 deletions test/langtools/tools/javac/patterns/T8314226.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,21 @@ case Integer _ when ((Integer) obj) > 0:
}

int multipleCasesWithLoop(Object obj) {
int i = 0;
switch (obj) {
case Integer _ when ((Integer) obj) > 0:
case String _ when !((String) obj).isEmpty():
return 1;
i = i + 1;
case null:
while (true) {break;}
while (true) {
i = i + 10;
break;
}
default:
return 3;
i = i + 100;
}

return i;
}

public static void main(String[] args) {
Expand Down Expand Up @@ -147,10 +153,10 @@ private void run() {
assertEquals(111, multipleCasesWithEffectNoReturn("test"));
assertEquals(110, multipleCasesWithEffectNoReturn(null));
assertEquals(100, multipleCasesWithEffectNoReturn(""));
assertEquals(1, multipleCasesWithLoop(42));
assertEquals(1, multipleCasesWithLoop("test"));
assertEquals(3, multipleCasesWithLoop(null));
assertEquals(3, multipleCasesWithLoop(""));
assertEquals(111, multipleCasesWithLoop(42));
assertEquals(111, multipleCasesWithLoop("test"));
assertEquals(110, multipleCasesWithLoop(null));
assertEquals(100, multipleCasesWithLoop(""));
}

void assertEquals(Object expected, Object actual) {
Expand Down

0 comments on commit 283b0b0

Please sign in to comment.