From 3b05a94c36e5d54693694c2e9950eca42626962b Mon Sep 17 00:00:00 2001 From: Aggelos Biboudis Date: Thu, 9 Feb 2023 16:03:34 +0000 Subject: [PATCH] 8301858: Verification error when compiling switch with record patterns Reviewed-by: vromero --- .../com/sun/tools/javac/comp/TransPatterns.java | 6 +++--- .../javac/patterns/DeconstructionDesugaring.java | 15 ++++++++++++++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TransPatterns.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TransPatterns.java index c928c7c75d1..3463439a8a2 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TransPatterns.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TransPatterns.java @@ -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 newLabel; if (hasUnconditional) { newLabel = List.of(make.ConstantCaseLabel(makeNull()), - make.DefaultCaseLabel()); + make.PatternCaseLabel(binding, newGuard)); } else { newLabel = List.of(make.PatternCaseLabel(binding, newGuard)); } diff --git a/test/langtools/tools/javac/patterns/DeconstructionDesugaring.java b/test/langtools/tools/javac/patterns/DeconstructionDesugaring.java index 47b988b6284..09bf4813113 100644 --- a/test/langtools/tools/javac/patterns/DeconstructionDesugaring.java +++ b/test/langtools/tools/javac/patterns/DeconstructionDesugaring.java @@ -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 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 + ", " +