Skip to content

Commit

Permalink
8310128: Switch with unnamed patterns erroneously non-exhaustive
Browse files Browse the repository at this point in the history
  • Loading branch information
biboudis committed Jun 15, 2023
1 parent 4c0e164 commit b39cc77
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3501,7 +3501,7 @@ public PatternDescription makePatternDescription(Type selectorType, JCPattern pa
}
return new RecordPattern(record.type, componentTypes, nestedDescriptions);
} else if (pattern instanceof JCAnyPattern) {
Type type = types.isSubtype(selectorType, syms.objectType)
Type type = types.isSubtype(selectorType, syms.objectType) || selectorType.isPrimitive()
? selectorType : syms.objectType;
return new BindingPattern(type);
} else {
Expand Down
25 changes: 25 additions & 0 deletions test/langtools/tools/javac/patterns/Unnamed.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ public void run() {
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();
}
Expand Down Expand Up @@ -270,6 +272,29 @@ String unnamedGuardAddsBindings(Object o1, Object o2) {
};
}

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 }
Expand Down

0 comments on commit b39cc77

Please sign in to comment.