Skip to content

Commit

Permalink
8314632: Intra-case dominance check fails in the presence of a guard
Browse files Browse the repository at this point in the history
Backport-of: 81f8c57e4a14e0186f608cbe5a899322df80fd30
  • Loading branch information
biboudis authored and lahodaj committed Sep 22, 2023
1 parent 742ff74 commit fa64237
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
Expand Up @@ -4712,7 +4712,7 @@ void checkSwitchCaseLabelDominated(List<JCCase> cases) {
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);
}
Expand Down
40 changes: 40 additions & 0 deletions test/langtools/tools/javac/patterns/T8314632.java
@@ -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);
}
}
}
6 changes: 6 additions & 0 deletions test/langtools/tools/javac/patterns/T8314632.out
@@ -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

1 comment on commit fa64237

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