Skip to content

Commit c4557a7

Browse files
committed
8332463: Byte conditional pattern case element dominates short constant case element
Reviewed-by: vromero
1 parent d59c12f commit c4557a7

File tree

5 files changed

+97
-10
lines changed

5 files changed

+97
-10
lines changed

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4818,7 +4818,6 @@ void checkSwitchCaseLabelDominated(JCCaseLabel unconditionalCaseLabel, List<JCCa
48184818
JCCaseLabel testCaseLabel = caseAndLabel.snd;
48194819
Type testType = labelType(testCaseLabel);
48204820
boolean dominated = false;
4821-
if (unconditionalCaseLabel == testCaseLabel) unconditionalFound = true;
48224821
if (types.isUnconditionallyExact(currentType, testType) &&
48234822
!currentType.hasTag(ERROR) && !testType.hasTag(ERROR)) {
48244823
//the current label is potentially dominated by the existing (test) label, check:
@@ -4833,11 +4832,6 @@ void checkSwitchCaseLabelDominated(JCCaseLabel unconditionalCaseLabel, List<JCCa
48334832
}
48344833
}
48354834

4836-
// Domination can occur even when we have not an unconditional pair between case labels.
4837-
if (unconditionalFound && unconditionalCaseLabel != label) {
4838-
dominated = true;
4839-
}
4840-
48414835
if (dominated) {
48424836
log.error(label.pos(), Errors.PatternDominated);
48434837
}

test/langtools/tools/javac/patterns/PrimitivePatternsSwitchErrors.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* @test /nodynamiccopyright/
3-
* @bug 8304487 8325653
3+
* @bug 8304487 8325653 8332463
44
* @summary Compiler Implementation for Primitive types in patterns, instanceof, and switch (Preview)
55
* @enablePreview
66
* @compile/fail/ref=PrimitivePatternsSwitchErrors.out -XDrawDiagnostics -XDshould-stop.at=FLOW PrimitivePatternsSwitchErrors.java
@@ -59,7 +59,7 @@ public static int dominationBetweenBoxedAndPrimitive() {
5959
int i = 42;
6060
return switch (i) {
6161
case Integer ib -> ib;
62-
case byte ip -> ip; // Error - dominated!
62+
case byte ip -> ip; // OK - not dominated!
6363
};
6464
}
6565

test/langtools/tools/javac/patterns/PrimitivePatternsSwitchErrors.out

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
PrimitivePatternsSwitchErrors.java:15:18: compiler.err.pattern.dominated
22
PrimitivePatternsSwitchErrors.java:24:18: compiler.err.pattern.dominated
33
PrimitivePatternsSwitchErrors.java:31:24: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: int, java.lang.Long)
4-
PrimitivePatternsSwitchErrors.java:62:18: compiler.err.pattern.dominated
54
PrimitivePatternsSwitchErrors.java:70:18: compiler.err.pattern.dominated
65
PrimitivePatternsSwitchErrors.java:78:18: compiler.err.pattern.dominated
76
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
4443
PrimitivePatternsSwitchErrors.java:260:16: compiler.err.not.exhaustive
4544
- compiler.note.preview.filename: PrimitivePatternsSwitchErrors.java, DEFAULT
4645
- compiler.note.preview.recompile
47-
44 errors
46+
43 errors
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8332463
27+
* @summary Byte conditional pattern case element dominates short constant case element
28+
* @compile --enable-preview --source ${jdk.version} T8332463a.java
29+
*/
30+
public class T8332463a {
31+
public int test2() {
32+
Byte i = (byte) 42;
33+
return switch (i) {
34+
case Byte ib -> 1;
35+
case short s -> 2;
36+
};
37+
}
38+
39+
public int test4() {
40+
int i = 42;
41+
return switch (i) {
42+
case Integer ib -> 1;
43+
case byte ip -> 2;
44+
};
45+
}
46+
47+
public int test3() {
48+
int i = 42;
49+
return switch (i) {
50+
case Integer ib -> 1;
51+
case (byte) 0 -> 2;
52+
};
53+
}
54+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8332463
27+
* @summary Byte conditional pattern case element dominates short constant case element
28+
* @enablePreview
29+
* @compile T8332463b.java
30+
* @compile --enable-preview --source ${jdk.version} T8332463b.java
31+
*/
32+
public class T8332463b {
33+
public int test1() {
34+
Byte i = (byte) 42;
35+
return switch (i) {
36+
case Byte ib -> 1;
37+
case (short) 0 -> 2;
38+
};
39+
}
40+
}

0 commit comments

Comments
 (0)