8314632: Intra-case dominance check fails in the presence of a guard

Reviewed-by: vromero
This commit is contained in:
Aggelos Biboudis 2023-09-07 12:51:37 +00:00 committed by Jan Lahoda
parent b408a82f9b
commit 81f8c57e4a
3 changed files with 47 additions and 1 deletions

View File

@ -4712,7 +4712,7 @@ public class Check {
TreeInfo.unguardedCase(testCase);
} else if (label instanceof JCPatternCaseLabel patternCL &&
testCaseLabel instanceof JCPatternCaseLabel testPatternCaseLabel &&
TreeInfo.unguardedCase(testCase)) {
(testCase.equals(c) || TreeInfo.unguardedCase(testCase))) {
dominated = patternDominated(testPatternCaseLabel.pat,
patternCL.pat);
}

View File

@ -0,0 +1,40 @@
/*
* @test /nodynamiccopyright/
* @bug 8314632
* @summary Intra-case dominance check fails in the presence of a guard
* @enablePreview
* @compile/fail/ref=T8314632.out -XDrawDiagnostics T8314632.java
*/
public class T8314632 {
void test1(Object obj) {
switch (obj) {
case Float _ -> {}
case Integer _, CharSequence _, String _ when obj.hashCode() > 0 -> { } // error
default -> throw new IllegalStateException("Unexpected value: " + obj);
}
}
void test2(Object obj) {
switch (obj) {
case Float _ -> {}
case Integer _, CharSequence _, String _ -> { }
default -> throw new IllegalStateException("Unexpected value: " + obj); // error
}
}
void test3(Object obj) {
switch (obj) {
case Float _, CharSequence _ when obj.hashCode() > 0 -> { } // OK
case Integer _, String _ when obj.hashCode() > 0 -> { } // OK, since the previous case is guarded
default -> throw new IllegalStateException("Unexpected value: " + obj);
}
}
void test4(Object obj) {
switch (obj) {
case Float _, CharSequence _ -> { } // OK
case Integer _, String _ when obj.hashCode() > 0 -> { } // error, since the previous case is unguarded
default -> throw new IllegalStateException("Unexpected value: " + obj);
}
}
}

View File

@ -0,0 +1,6 @@
T8314632.java:12:45: compiler.err.pattern.dominated
T8314632.java:20:45: compiler.err.pattern.dominated
T8314632.java:36:29: compiler.err.pattern.dominated
- compiler.note.preview.filename: T8314632.java, DEFAULT
- compiler.note.preview.recompile
3 errors