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
8310128: Switch with unnamed patterns erroneously non-exhaustive
Browse files Browse the repository at this point in the history
Reviewed-by: jlahoda
Backport-of: 32243ef47df75241302e243b4fd9627d686437fe
  • Loading branch information
biboudis authored and lahodaj committed Jun 16, 2023
1 parent 39e98e7 commit aced114
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3501,9 +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)
? selectorType : syms.objectType;
return new BindingPattern(type);
return new BindingPattern(selectorType);
} else {
throw Assert.error();
}
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

1 comment on commit aced114

@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.