diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java index b60694744a1..d5706e703c5 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java @@ -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); } diff --git a/test/langtools/tools/javac/patterns/T8314632.java b/test/langtools/tools/javac/patterns/T8314632.java new file mode 100644 index 00000000000..407fba6466a --- /dev/null +++ b/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); + } + } +} diff --git a/test/langtools/tools/javac/patterns/T8314632.out b/test/langtools/tools/javac/patterns/T8314632.out new file mode 100644 index 00000000000..df3cafdb077 --- /dev/null +++ b/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 \ No newline at end of file