8310128: Switch with unnamed patterns erroneously non-exhaustive

Reviewed-by: vromero, jlahoda
This commit is contained in:
Aggelos Biboudis 2023-06-16 07:03:28 +00:00 committed by Jan Lahoda
parent cfae6ef2f6
commit 32243ef47d
2 changed files with 26 additions and 3 deletions
src/jdk.compiler/share/classes/com/sun/tools/javac/comp
test/langtools/tools/javac/patterns

@ -3501,9 +3501,7 @@ public class Flow {
}
return new RecordPattern(record.type, componentTypes, nestedDescriptions);
} else if (pattern instanceof JCAnyPattern) {
Type type = types.isSubtype(selectorType, syms.objectType)
? selectorType : syms.objectType;
return new BindingPattern(type);
return new BindingPattern(selectorType);
} else {
throw Assert.error();
}

@ -79,6 +79,8 @@ public class Unnamed {
assertEquals(2, testMixVarWithExplicit(new Box<>(new R2())));
assertEquals("binding", unnamedGuardAddsBindings("match1", "binding"));
assertEquals("any", unnamedGuardAddsBindings(42, 42));
assertEquals(true, testUnnamedPrimitiveAndExhaustiveness(new Prim1(4)));
assertEquals(false, testUnnamedPrimitiveAndExhaustiveness(new Prim2(5)));
unnamedTest();
}
@ -270,6 +272,29 @@ public class Unnamed {
};
}
boolean testUnnamedPrimitiveAndExhaustiveness(RecordWithPrimitive a) {
boolean r1 = switch (a) {
case Prim1(var _) -> true;
case Prim2(_) -> false;
};
boolean r2 = switch (a) {
case Prim1(var _) -> true;
case Prim2(var _) -> false;
};
boolean r3 = switch (a) {
case Prim1(_) -> true;
case Prim2(_) -> false;
};
return r1 && r2 && r3;
}
sealed interface RecordWithPrimitive permits Prim1, Prim2 {};
record Prim1(int n1) implements RecordWithPrimitive {};
record Prim2(int n2) implements RecordWithPrimitive {};
// JEP 443 examples
record Point(int x, int y) { }
enum Color { RED, GREEN, BLUE }