8301858: Verification error when compiling switch with record patterns

Reviewed-by: vromero
This commit is contained in:
Aggelos Biboudis 2023-02-09 16:03:34 +00:00 committed by Vicente Romero
parent e4d1cff659
commit 3b05a94c36
2 changed files with 17 additions and 4 deletions

View File

@ -539,10 +539,10 @@ public class TransPatterns extends TreeTranslator {
pattern = parenthesized.pattern;
}
Assert.check(pattern.hasTag(Tag.BINDINGPATTERN));
VarSymbol binding = ((JCBindingPattern) pattern).var.sym;
BindingSymbol binding = (BindingSymbol) ((JCBindingPattern) pattern).var.sym;
guard = makeBinary(Tag.OR,
makeBinary(Tag.EQ,
make.Ident(binding),
make.Ident(bindingContext.getBindingFor(binding)),
makeNull()),
guard);
}
@ -740,7 +740,7 @@ public class TransPatterns extends TreeTranslator {
List<JCCaseLabel> newLabel;
if (hasUnconditional) {
newLabel = List.of(make.ConstantCaseLabel(makeNull()),
make.DefaultCaseLabel());
make.PatternCaseLabel(binding, newGuard));
} else {
newLabel = List.of(make.PatternCaseLabel(binding, newGuard));
}

View File

@ -23,7 +23,7 @@
/**
* @test
* @bug 8291769
* @bug 8291769 8301858
* @summary Verify more complex switches work properly
* @compile --enable-preview -source ${jdk.version} DeconstructionDesugaring.java
* @run main/othervm --enable-preview DeconstructionDesugaring
@ -43,6 +43,8 @@ public class DeconstructionDesugaring {
assertEquals(runCheckExpressionWithUnconditional(new R5(new R4(null))), 3);
assertEquals(runCheckExpressionWithUnconditional1(new R5(new R4(null))), 2);
assertEquals(runCheckExpressionWithUnconditional1(new R5(null)), 3);
assertEquals(runCheckExpressionWithUnconditionalAndParams(new R1(42)), 1);
assertEquals(runCheckExpressionWithUnconditionalAndParams(new R1(new Object())), 2);
}
private void test(ToIntFunction<Object> task) {
@ -103,6 +105,17 @@ public class DeconstructionDesugaring {
};
}
public static int runCheckExpressionWithUnconditionalAndParams(R1 r) {
switch (r) {
case R1(Integer i):
return meth_I(i);
case R1(Object o):
return meth_O(o);
}
}
public static int meth_I(Integer i) { return 1; }
public static int meth_O(Object o) { return 2;}
private void assertEquals(int expected, int actual) {
if (expected != actual) {
throw new AssertionError("expected: " + expected + ", " +