-
Bug
-
Resolution: Fixed
-
P4
-
22
Issue | Fix Version | Assignee | Priority | Status | Resolution | Resolved In Build |
---|---|---|---|---|---|---|
JDK-8316724 | 21.0.2 | Angelos Bimpoudis | P4 | Resolved | Fixed | b02 |
Code:
public class Test {
void test(Object obj) {
switch (obj) {
case Integer _ when ((Integer) obj) > 0:
case String _ when !((String) obj).isEmpty():
System.out.println(obj + ": Positive number or non-empty string");
break;
default:
System.out.println("other");
}
}
public static void main(String[] args) {
new Test().test("");
}
}
This program can be compiled correctly (expected) and prints (unexpectedly)
": Positive number or non-empty string"
It should print "other", as guard condition is not satisfied. Note that if I remove never taken "case Integer _ when ((Integer) obj) > 0:", then the string guard is taken into account.
public class Test {
void test(Object obj) {
switch (obj) {
case Integer _ when ((Integer) obj) > 0:
case String _ when !((String) obj).isEmpty():
System.out.println(obj + ": Positive number or non-empty string");
break;
default:
System.out.println("other");
}
}
public static void main(String[] args) {
new Test().test("");
}
}
This program can be compiled correctly (expected) and prints (unexpectedly)
": Positive number or non-empty string"
It should print "other", as guard condition is not satisfied. Note that if I remove never taken "case Integer _ when ((Integer) obj) > 0:", then the string guard is taken into account.
- backported by
-
JDK-8316724 Series of colon-style fallthrough switch cases with guards compiled incorrectly
-
- Resolved
-
- duplicates
-
JDK-8321262 Incorrect `switch` `case` taken
-
- Closed
-
- relates to
-
JDK-8314578 Non-verifiable code is emitted when two guards declare pattern variables in colon-switch
-
- Resolved
-
- links to
-
Commit openjdk/jdk21u/742ff74e
-
Commit openjdk/jdk/3b0a6d2a
-
Review openjdk/jdk21u/161
-
Review openjdk/jdk/15532
(2 links to)