8326404: Assertion error when trying to compile switch with fallthrough with pattern
Co-authored-by: Jan Lahoda <jlahoda@openjdk.org> Reviewed-by: vromero
This commit is contained in:
parent
beea5305b0
commit
ea5eb74a65
src/jdk.compiler/share/classes/com/sun/tools/javac/comp
test/langtools/tools/javac/patterns
@ -458,7 +458,9 @@ public class TransPatterns extends TreeTranslator {
|
||||
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"),
|
||||
@ -523,8 +525,6 @@ public class TransPatterns extends TreeTranslator {
|
||||
boolean previousCompletesNormally = false;
|
||||
boolean hasDefault = false;
|
||||
|
||||
patchCompletingNormallyCases(cases);
|
||||
|
||||
for (var c : cases) {
|
||||
List<JCCaseLabel> clearedPatterns = c.labels;
|
||||
boolean hasJoinedNull =
|
||||
@ -685,7 +685,7 @@ public class TransPatterns extends TreeTranslator {
|
||||
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;
|
||||
|
||||
@ -700,6 +700,7 @@ public class TransPatterns extends TreeTranslator {
|
||||
};
|
||||
|
||||
currentCase.stats = newStatements.toList();
|
||||
currentCase.completesNormally = false;
|
||||
}
|
||||
|
||||
cases = cases.tail;
|
||||
@ -948,10 +949,12 @@ public class TransPatterns extends TreeTranslator {
|
||||
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;
|
||||
|
||||
@ -986,6 +989,8 @@ public class TransPatterns extends TreeTranslator {
|
||||
commonBinding.isUnnamedVariable() == currentBinding.isUnnamedVariable() &&
|
||||
!previousNullable &&
|
||||
!currentNullable &&
|
||||
!previousCompletesNormally &&
|
||||
!currentCompletesNormally &&
|
||||
new TreeDiffer(List.of(commonBinding), List.of(currentBinding))
|
||||
.scan(commonNestedExpression, currentNestedExpression)) {
|
||||
accummulator.add(c.head);
|
||||
@ -1001,6 +1006,7 @@ public class TransPatterns extends TreeTranslator {
|
||||
commonNestedBinding = currentNestedBinding;
|
||||
}
|
||||
previousNullable = currentNullable;
|
||||
previousCompletesNormally = currentCompletesNormally;
|
||||
}
|
||||
resolveAccummulator.resolve(commonBinding, commonNestedExpression, commonNestedBinding);
|
||||
return result.toList();
|
||||
@ -1473,8 +1479,8 @@ public class TransPatterns extends TreeTranslator {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
157
test/langtools/tools/javac/patterns/T8326404.java
Normal file
157
test/langtools/tools/javac/patterns/T8326404.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user