Skip to content

Commit 84d94ff

Browse files
committed
Fix exhaustiveness bug
1 parent ade7e12 commit 84d94ff

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4482,9 +4482,9 @@ private List<MethodSymbol> getPatternDeclarationCandidates(Type site, int nested
44824482
ClassSymbol record = (ClassSymbol) site.tsym;
44834483

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

44904490
MethodType mt = new MethodType(List.nil(), syms.voidType, List.nil(), syms.methodClass);

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3573,9 +3573,7 @@ public PatternDescription makePatternDescription(Type selectorType, JCPattern pa
35733573
Type[] componentTypes;
35743574

35753575
if (!record.type.isErroneous()) {
3576-
componentTypes = ((ClassSymbol) record.type.tsym).getRecordComponents()
3577-
.map(r -> types.memberType(record.type, r))
3578-
.toArray(s -> new Type[s]);
3576+
componentTypes = ((JCRecordPattern) pattern).patternDeclaration.type.asMethodType().bindingtypes.toArray(Type[]::new);
35793577
}
35803578
else {
35813579
componentTypes = record.nested.map(t -> types.createErrorType(t.type)).toArray(s -> new Type[s]);;

test/langtools/tools/javac/patterns/declarations/ClassPatternDeclarations.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public static void main(String... args) {
3535
assertEquals("A", test1B(new Person1("A", "B", false)));
3636
assertEquals("Duke", test2(new Person1("Duke", "Java", false)));
3737
assertEquals("DUKE", test2(new Person1("Duke", "Java", true)));
38+
assertEquals("A:B", testInSwitch1(new Person1("A", "B", false)));
39+
assertEquals("A:B", testInSwitch2(new Person2("A", "B")));
3840
}
3941

4042
private static String test1A(Object o) {
@@ -58,6 +60,18 @@ private static String test2(Object o) {
5860
return null;
5961
}
6062

63+
private static String testInSwitch1(Person1 o) {
64+
return switch (o){
65+
case Person1(String name, String username) -> name + ":" + username;
66+
};
67+
}
68+
69+
private static String testInSwitch2(Person2 o) {
70+
return switch (o){
71+
case Person2(String name, String username) -> name + ":" + username;
72+
};
73+
}
74+
6175
public static record Person1(String name, String username, boolean capitalize) {
6276
public Person1(String name) {
6377
this(name, "default", false);
@@ -84,6 +98,8 @@ public pattern Person1(int[] t) {
8498
}
8599
}
86100

101+
public static record Person2(String name, String username){ }
102+
87103
private static <T> void assertEquals(T expected, T actual) {
88104
if (!Objects.equals(expected, actual)) {
89105
throw new AssertionError("Expected: " + expected + ", but got: " + actual);

0 commit comments

Comments
 (0)