Skip to content
This repository has been archived by the owner on Sep 19, 2023. It is now read-only.
/ jdk21 Public archive

Commit

Permalink
8311038: Incorrect exhaustivity computation
Browse files Browse the repository at this point in the history
Reviewed-by: vromero
Backport-of: bbb7ce5137cd3e8365552b42610e19b7ebe43ba1
  • Loading branch information
lahodaj committed Jul 18, 2023
1 parent b8eddab commit 7a5d6f9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,8 @@ private List<PatternDescription> reduceBindingPatterns(Type selectorType, List<P
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)) {
Expand Down Expand Up @@ -891,7 +893,9 @@ private List<PatternDescription> reduceBindingPatterns(Type selectorType, List<P
}

if (reduces) {
bindings.append(pdOther);
if (!types.isSubtype(types.erasure(clazz.type), types.erasure(bpOther.type))) {
bindings.append(pdOther);
}
}
}
}
Expand Down Expand Up @@ -926,6 +930,8 @@ private Set<Symbol> allPermittedSubTypes(ClassSymbol root, Predicate<ClassSymbol

permittedSubtypesClosure = permittedSubtypesClosure.tail;

current.complete();

if (current.isSealed() && current.isAbstract()) {
for (Symbol sym : current.permitted) {
ClassSymbol csym = (ClassSymbol) sym;
Expand Down
22 changes: 21 additions & 1 deletion test/langtools/tools/javac/patterns/Exhaustiveness.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1948,6 +1948,26 @@ void test(I i) {
""");
}

@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);
}
Expand Down

1 comment on commit 7a5d6f9

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.