Skip to content

Commit

Permalink
8326404: Assertion error when trying to compile switch with fallthrou…
Browse files Browse the repository at this point in the history
…gh with pattern

Co-authored-by: Jan Lahoda <[email protected]>
Reviewed-by: vromero
  • Loading branch information
biboudis and lahodaj committed May 14, 2024
1 parent beea530 commit ea5eb74
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,9 @@ private void handleSwitch(JCTree tree,
newCases.add(c.head);
appendBreakIfNeeded(tree, cases, c.head);
}
cases = processCases(tree, newCases.toList());
cases = newCases.toList();
patchCompletingNormallyCases(cases);
cases = processCases(tree, cases);
ListBuffer<JCStatement> statements = new ListBuffer<>();
VarSymbol temp = new VarSymbol(Flags.SYNTHETIC,
names.fromString("selector" + variableIndex++ + target.syntheticNameChar() + "temp"),
Expand Down Expand Up @@ -523,8 +525,6 @@ private void handleSwitch(JCTree tree,
boolean previousCompletesNormally = false;
boolean hasDefault = false;

patchCompletingNormallyCases(cases);

for (var c : cases) {
List<JCCaseLabel> clearedPatterns = c.labels;
boolean hasJoinedNull =
Expand Down Expand Up @@ -685,7 +685,7 @@ private static void patchCompletingNormallyCases(List<JCCase> cases) {
if (currentCase.caseKind == CaseKind.STATEMENT &&
currentCase.completesNormally &&
cases.tail.nonEmpty() &&
cases.tail.head.guard != null) {
(cases.tail.head.guard != null || cases.tail.head.labels.stream().anyMatch(cl -> cl instanceof JCPatternCaseLabel p && p.syntheticGuard != null))) {
ListBuffer<JCStatement> newStatements = new ListBuffer<>();
List<JCCase> copyFrom = cases;

Expand All @@ -700,6 +700,7 @@ private static void patchCompletingNormallyCases(List<JCCase> cases) {
};

currentCase.stats = newStatements.toList();
currentCase.completesNormally = false;
}

cases = cases.tail;
Expand Down Expand Up @@ -948,10 +949,12 @@ public void resolve(VarSymbol commonBinding,
JCExpression commonNestedExpression = null;
VarSymbol commonNestedBinding = null;
boolean previousNullable = false;
boolean previousCompletesNormally = false;

for (List<JCCase> c = inputCases; c.nonEmpty(); c = c.tail) {
VarSymbol currentBinding = null;
boolean currentNullable = false;
boolean currentCompletesNormally = c.head.completesNormally;
JCExpression currentNestedExpression = null;
VarSymbol currentNestedBinding = null;

Expand Down Expand Up @@ -986,6 +989,8 @@ public void resolve(VarSymbol commonBinding,
commonBinding.isUnnamedVariable() == currentBinding.isUnnamedVariable() &&
!previousNullable &&
!currentNullable &&
!previousCompletesNormally &&
!currentCompletesNormally &&
new TreeDiffer(List.of(commonBinding), List.of(currentBinding))
.scan(commonNestedExpression, currentNestedExpression)) {
accummulator.add(c.head);
Expand All @@ -1001,6 +1006,7 @@ public void resolve(VarSymbol commonBinding,
commonNestedBinding = currentNestedBinding;
}
previousNullable = currentNullable;
previousCompletesNormally = currentCompletesNormally;
}
resolveAccummulator.resolve(commonBinding, commonNestedExpression, commonNestedBinding);
return result.toList();
Expand Down Expand Up @@ -1473,8 +1479,8 @@ List<JCStatement> bindingVars(int diagPos) {
ListBuffer<JCStatement> stats = new ListBuffer<>();
for (Entry<BindingSymbol, VarSymbol> e : hoistedVarMap.entrySet()) {
JCVariableDecl decl = makeHoistedVarDecl(diagPos, e.getValue());
if (!e.getKey().isPreserved() ||
!parent.tryPrepend(e.getKey(), decl)) {
if (!e.getValue().isUnnamedVariable() &&
(!e.getKey().isPreserved() || !parent.tryPrepend(e.getKey(), decl))) {
stats.add(decl);
}
}
Expand Down
157 changes: 157 additions & 0 deletions test/langtools/tools/javac/patterns/T8326404.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 8326404
* @summary Assertion error when trying to compile switch with fallthrough with pattern
* @compile T8326404.java
* @run main T8326404
*/
public class T8326404 {
private static final record R<T>(T a) {}

public static void main(String[] args) {
assertEquals(4, run1(""));
assertEquals(3, run1(new R("")));
assertEquals(2, run1(new R(42)));

assertEquals(2, run1_break1(""));
assertEquals(1, run1_break1(new R("")));
assertEquals(2, run1_break1(new R(42)));

assertEquals(3, run2(""));
assertEquals(4, run2(new R("")));
assertEquals(2, run2(new R(42)));

assertEquals(1, run2_break1(""));
assertEquals(2, run2_break1(new R("")));
assertEquals(2, run2_break1(new R(42)));

assertEquals(2, run3(""));
assertEquals(4, run3(new R("")));
assertEquals(3, run3(new R(42)));

assertEquals(2, run3_break1(""));
assertEquals(2, run3_break1(new R("")));
assertEquals(1, run3_break1(new R(42)));
}

private static int run1(Object o) {
int i = 0;
switch (o) {
case String _:
i++;
case R(String _):
i++;
case R(Integer _):
i++;
default:
i++;
}
return i;
}

private static int run1_break1(Object o) {
int i = 0;
switch (o) {
case String s:
i++;
case R(String _):
i++;
break;
case R(Integer _):
i++;
default:
i++;
}
return i;
}

private static int run2(Object o) {
int i = 0;
switch (o) {
case R(String _):
i++;
case String _:
i++;
case R(Integer _):
i++;
default:
i++;
}
return i;
}

private static int run2_break1(Object o) {
int i = 0;
switch (o) {
case R(String _):
i++;
case String _:
i++;
break;
case R(Integer _):
i++;
default:
i++;
}
return i;
}

private static int run3(Object o) {
int i = 0;
switch (o) {
case R(String _):
i++;
case R(Integer _):
i++;
case String _:
i++;
default:
i++;
}
return i;
}

private static int run3_break1(Object o) {
int i = 0;
switch (o) {
case R(String _):
i++;
case R(Integer _):
i++;
break;
case String _:
i++;
default:
i++;
}
return i;
}

static void assertEquals(int expected, int actual) {
if (expected != actual) {
throw new AssertionError("Expected: " + expected + ", but got: " + actual);
}
}
}

0 comments on commit ea5eb74

Please sign in to comment.