diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/ExhaustivenessComputer.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/ExhaustivenessComputer.java index 6eb87abb8bf26..e0706e93e861a 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/ExhaustivenessComputer.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/ExhaustivenessComputer.java @@ -213,7 +213,6 @@ private Set reduceBindingPatterns(Type selectorType, Set bindings = new ListBuffer<>(); //do not reduce to types unrelated to the selector type: Type clazzErasure = types.erasure(clazz.type); if (components(selectorType).stream() @@ -232,16 +231,26 @@ private Set reduceBindingPatterns(Type selectorType, Set filteredPermitted = new HashSet<>(permitted); for (PatternDescription pdOther : patterns) { if (pdOther instanceof BindingPattern bpOther) { - Set currentPermittedSubTypes = - allPermittedSubTypes(bpOther.type.tsym, s -> true); + Set currentSubTypes; + + if (bpOther.type.tsym.isAbstract()) { + currentSubTypes = + permitted.stream() + .filter(perm -> types.isSubtype(types.erasure(perm.type), + types.erasure(bpOther.type))) + .collect(Collectors.toSet()); + } else { + currentSubTypes = Set.of(); + } - PERMITTED: for (Iterator it = permitted.iterator(); it.hasNext();) { + PERMITTED: for (Iterator it = filteredPermitted.iterator(); it.hasNext();) { Symbol perm = it.next(); - for (Symbol currentPermitted : currentPermittedSubTypes) { + for (Symbol currentPermitted : currentSubTypes) { if (types.isSubtype(types.erasure(currentPermitted.type), types.erasure(perm.type))) { it.remove(); @@ -256,7 +265,7 @@ private Set reduceBindingPatterns(Type selectorType, Set 0; + }; + + } + + } + """); + } + + @Test //JDK-8366968 + public void testNonAbstract(Path base) throws Exception { + doTest(base, + new String[0], + """ + class Demo { + sealed interface I permits Base, C3 { } + sealed class Base implements I permits C1, C2 { } + final class C1 extends Base { } + final class C2 extends Base { } + final class C3 implements I { } + + void method1(I i) { + switch (i) { + case C1 _ -> {} + case C2 _ -> {} + case C3 _ -> {} + } + } + } + """, + "Demo.java:9:9: compiler.err.not.exhaustive.statement", + "1 error"); + } + private void doTest(Path base, String[] libraryCode, String testCode, String... expectedErrors) throws IOException { doTest(base, libraryCode, testCode, false, expectedErrors); }