From bbb7ce5137cd3e8365552b42610e19b7ebe43ba1 Mon Sep 17 00:00:00 2001 From: Jan Lahoda Date: Fri, 14 Jul 2023 08:21:48 +0000 Subject: [PATCH] 8311038: Incorrect exhaustivity computation Reviewed-by: vromero --- .../com/sun/tools/javac/comp/Flow.java | 8 ++++++- .../tools/javac/patterns/Exhaustiveness.java | 22 ++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java index ff77c9cdf18..0d94e4bd351 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java @@ -844,6 +844,8 @@ public class Flow { for (Type sup : types.directSupertypes(bpOne.type)) { ClassSymbol clazz = (ClassSymbol) sup.tsym; + clazz.complete(); + if (clazz.isSealed() && clazz.isAbstract() && //if a binding pattern for clazz already exists, no need to analyze it again: !existingBindings.contains(clazz)) { @@ -891,7 +893,9 @@ public class Flow { } if (reduces) { - bindings.append(pdOther); + if (!types.isSubtype(types.erasure(clazz.type), types.erasure(bpOther.type))) { + bindings.append(pdOther); + } } } } @@ -926,6 +930,8 @@ public class Flow { permittedSubtypesClosure = permittedSubtypesClosure.tail; + current.complete(); + if (current.isSealed() && current.isAbstract()) { for (Symbol sym : current.permitted) { ClassSymbol csym = (ClassSymbol) sym; diff --git a/test/langtools/tools/javac/patterns/Exhaustiveness.java b/test/langtools/tools/javac/patterns/Exhaustiveness.java index dca5864e1a9..88c897e6edc 100644 --- a/test/langtools/tools/javac/patterns/Exhaustiveness.java +++ b/test/langtools/tools/javac/patterns/Exhaustiveness.java @@ -23,7 +23,7 @@ /** * @test - * @bug 8262891 8268871 8274363 8281100 8294670 + * @bug 8262891 8268871 8274363 8281100 8294670 8311038 * @summary Check exhaustiveness of switches over sealed types. * @library /tools/lib * @modules jdk.compiler/com.sun.tools.javac.api @@ -1948,6 +1948,26 @@ public class Exhaustiveness extends TestRunner { """); } + @Test //JDK-8311038 + public void testReducesTooMuch(Path base) throws Exception { + doTest(base, + new String[0], + """ + package test; + public class Test { + void test(Rec r) { + switch (r) { + case Rec(String s) -> + System.out.println("I2" + s.toString()); + case Rec(Object o) -> + System.out.println("I2"); + } + } + record Rec(Object o) {} + } + """); + } + private void doTest(Path base, String[] libraryCode, String testCode, String... expectedErrors) throws IOException { doTest(base, libraryCode, testCode, false, expectedErrors); }