Details
-
Bug
-
Resolution: Fixed
-
P3
-
23
-
b24
Description
In JDK 22, the following switch is correct:
public static void test() {
Byte selExpr = (byte)0;
int retval = switch (selExpr) {
case Byte p when p.equals(selExpr) : {
yield 6;
}
case Byte p : {
yield 7;
}
case (short)0 : {
yield 9;
}
};
}
According to the spec:
A case label with a case constant c is dominated if one of the following holds:
- c is a constant expression of a primitive type S, and there is a preceding case label in the switch block with an unguarded case pattern p, where p is unconditional for the wrapper class of S.
- ...
According to the test, the last case is of primitive type short and there is a preceding case label with an unguarded case pattern Byte, where Byte is not unconditional for the wrapper class of short, which is Short.
In JDK 23 (non-preview or preview), the Byte conditional pattern case element erroneously dominates the short constant case element.
test/langtools/tools/javac/patterns/T8332463.java:40: error: this case label is dominated by a preceding case label
case (short)0 : {
^
public static void test() {
Byte selExpr = (byte)0;
int retval = switch (selExpr) {
case Byte p when p.equals(selExpr) : {
yield 6;
}
case Byte p : {
yield 7;
}
case (short)0 : {
yield 9;
}
};
}
According to the spec:
A case label with a case constant c is dominated if one of the following holds:
- c is a constant expression of a primitive type S, and there is a preceding case label in the switch block with an unguarded case pattern p, where p is unconditional for the wrapper class of S.
- ...
According to the test, the last case is of primitive type short and there is a preceding case label with an unguarded case pattern Byte, where Byte is not unconditional for the wrapper class of short, which is Short.
In JDK 23 (non-preview or preview), the Byte conditional pattern case element erroneously dominates the short constant case element.
test/langtools/tools/javac/patterns/T8332463.java:40: error: this case label is dominated by a preceding case label
case (short)0 : {
^