Skip to content

Commit

Permalink
Fix exhaustiveness bug
Browse files Browse the repository at this point in the history
  • Loading branch information
biboudis committed Aug 30, 2024
1 parent ade7e12 commit 84d94ff
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4482,9 +4482,9 @@ private List<MethodSymbol> getPatternDeclarationCandidates(Type site, int nested
ClassSymbol record = (ClassSymbol) site.tsym;

if (record.getRecordComponents().size() == nestedPatternCount) {
List<Type> recordComponents = record.getRecordComponents()
List<Type> recordComponents = ((ClassSymbol) record.type.tsym).getRecordComponents()
.stream()
.map(rc -> rc.type)
.map(rc -> types.memberType(site, rc))
.collect(List.collector());

MethodType mt = new MethodType(List.nil(), syms.voidType, List.nil(), syms.methodClass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3573,9 +3573,7 @@ public PatternDescription makePatternDescription(Type selectorType, JCPattern pa
Type[] componentTypes;

if (!record.type.isErroneous()) {
componentTypes = ((ClassSymbol) record.type.tsym).getRecordComponents()
.map(r -> types.memberType(record.type, r))
.toArray(s -> new Type[s]);
componentTypes = ((JCRecordPattern) pattern).patternDeclaration.type.asMethodType().bindingtypes.toArray(Type[]::new);
}
else {
componentTypes = record.nested.map(t -> types.createErrorType(t.type)).toArray(s -> new Type[s]);;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public static void main(String... args) {
assertEquals("A", test1B(new Person1("A", "B", false)));
assertEquals("Duke", test2(new Person1("Duke", "Java", false)));
assertEquals("DUKE", test2(new Person1("Duke", "Java", true)));
assertEquals("A:B", testInSwitch1(new Person1("A", "B", false)));
assertEquals("A:B", testInSwitch2(new Person2("A", "B")));
}

private static String test1A(Object o) {
Expand All @@ -58,6 +60,18 @@ private static String test2(Object o) {
return null;
}

private static String testInSwitch1(Person1 o) {
return switch (o){
case Person1(String name, String username) -> name + ":" + username;
};
}

private static String testInSwitch2(Person2 o) {
return switch (o){
case Person2(String name, String username) -> name + ":" + username;
};
}

public static record Person1(String name, String username, boolean capitalize) {
public Person1(String name) {
this(name, "default", false);
Expand All @@ -84,6 +98,8 @@ public pattern Person1(int[] t) {
}
}

public static record Person2(String name, String username){ }

private static <T> void assertEquals(T expected, T actual) {
if (!Objects.equals(expected, actual)) {
throw new AssertionError("Expected: " + expected + ", but got: " + actual);
Expand Down

0 comments on commit 84d94ff

Please sign in to comment.