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 fc96eaa0be1..4d9f0012733 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 @@ -4818,7 +4818,6 @@ public class Check { JCCaseLabel testCaseLabel = caseAndLabel.snd; Type testType = labelType(testCaseLabel); boolean dominated = false; - if (unconditionalCaseLabel == testCaseLabel) unconditionalFound = true; if (types.isUnconditionallyExact(currentType, testType) && !currentType.hasTag(ERROR) && !testType.hasTag(ERROR)) { //the current label is potentially dominated by the existing (test) label, check: @@ -4833,11 +4832,6 @@ public class Check { } } - // Domination can occur even when we have not an unconditional pair between case labels. - if (unconditionalFound && unconditionalCaseLabel != label) { - dominated = true; - } - if (dominated) { log.error(label.pos(), Errors.PatternDominated); } diff --git a/test/langtools/tools/javac/patterns/PrimitivePatternsSwitchErrors.java b/test/langtools/tools/javac/patterns/PrimitivePatternsSwitchErrors.java index 4186b83f2f1..3d47c4ac9bc 100644 --- a/test/langtools/tools/javac/patterns/PrimitivePatternsSwitchErrors.java +++ b/test/langtools/tools/javac/patterns/PrimitivePatternsSwitchErrors.java @@ -1,6 +1,6 @@ /* * @test /nodynamiccopyright/ - * @bug 8304487 8325653 + * @bug 8304487 8325653 8332463 * @summary Compiler Implementation for Primitive types in patterns, instanceof, and switch (Preview) * @enablePreview * @compile/fail/ref=PrimitivePatternsSwitchErrors.out -XDrawDiagnostics -XDshould-stop.at=FLOW PrimitivePatternsSwitchErrors.java @@ -59,7 +59,7 @@ public class PrimitivePatternsSwitchErrors { int i = 42; return switch (i) { case Integer ib -> ib; - case byte ip -> ip; // Error - dominated! + case byte ip -> ip; // OK - not dominated! }; } diff --git a/test/langtools/tools/javac/patterns/PrimitivePatternsSwitchErrors.out b/test/langtools/tools/javac/patterns/PrimitivePatternsSwitchErrors.out index 1cbf288e774..75fd62016a0 100644 --- a/test/langtools/tools/javac/patterns/PrimitivePatternsSwitchErrors.out +++ b/test/langtools/tools/javac/patterns/PrimitivePatternsSwitchErrors.out @@ -1,7 +1,6 @@ PrimitivePatternsSwitchErrors.java:15:18: compiler.err.pattern.dominated PrimitivePatternsSwitchErrors.java:24:18: compiler.err.pattern.dominated PrimitivePatternsSwitchErrors.java:31:24: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: int, java.lang.Long) -PrimitivePatternsSwitchErrors.java:62:18: compiler.err.pattern.dominated PrimitivePatternsSwitchErrors.java:70:18: compiler.err.pattern.dominated PrimitivePatternsSwitchErrors.java:78:18: compiler.err.pattern.dominated PrimitivePatternsSwitchErrors.java:84:18: compiler.err.prob.found.req: (compiler.misc.possible.loss.of.precision: long, byte) @@ -44,4 +43,4 @@ PrimitivePatternsSwitchErrors.java:254:16: compiler.err.not.exhaustive PrimitivePatternsSwitchErrors.java:260:16: compiler.err.not.exhaustive - compiler.note.preview.filename: PrimitivePatternsSwitchErrors.java, DEFAULT - compiler.note.preview.recompile -44 errors +43 errors \ No newline at end of file diff --git a/test/langtools/tools/javac/patterns/T8332463a.java b/test/langtools/tools/javac/patterns/T8332463a.java new file mode 100644 index 00000000000..96aaad86a85 --- /dev/null +++ b/test/langtools/tools/javac/patterns/T8332463a.java @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 8332463 + * @summary Byte conditional pattern case element dominates short constant case element + * @compile --enable-preview --source ${jdk.version} T8332463a.java + */ +public class T8332463a { + public int test2() { + Byte i = (byte) 42; + return switch (i) { + case Byte ib -> 1; + case short s -> 2; + }; + } + + public int test4() { + int i = 42; + return switch (i) { + case Integer ib -> 1; + case byte ip -> 2; + }; + } + + public int test3() { + int i = 42; + return switch (i) { + case Integer ib -> 1; + case (byte) 0 -> 2; + }; + } +} diff --git a/test/langtools/tools/javac/patterns/T8332463b.java b/test/langtools/tools/javac/patterns/T8332463b.java new file mode 100644 index 00000000000..7956fd31f9f --- /dev/null +++ b/test/langtools/tools/javac/patterns/T8332463b.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 8332463 + * @summary Byte conditional pattern case element dominates short constant case element + * @enablePreview + * @compile T8332463b.java + * @compile --enable-preview --source ${jdk.version} T8332463b.java + */ +public class T8332463b { + public int test1() { + Byte i = (byte) 42; + return switch (i) { + case Byte ib -> 1; + case (short) 0 -> 2; + }; + } +}