Skip to content

Commit

Permalink
Boolean switch inconsistency between ECJ and javac (#3576)
Browse files Browse the repository at this point in the history
* Fixes #3559
  • Loading branch information
srikanth-sankaran authored Jan 20, 2025
1 parent 834a03e commit d22d033
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public void resolve(BlockScope scope) {
// - if T is one of long, float, double, or boolean, the type of the case constant is T.
// - if T is one of Long, Float, Double, or Boolean, the type of the case constant is, respectively, long, float, double, or boolean.
if (caseType.id != T_null) {
TypeBinding expectedCaseType = selectorType.isBoxedPrimitiveType() ? selectorType.unboxedType() : selectorType;
TypeBinding expectedCaseType = selectorType.isBoxedPrimitiveType() && JavaFeature.PRIMITIVES_IN_PATTERNS.isSupported(scope.compilerOptions()) ? selectorType.unboxedType() : selectorType;
switch (expectedCaseType.id) {
case TypeIds.T_long, TypeIds.T_float, TypeIds.T_double, TypeIds.T_boolean -> {
if (caseType.id != expectedCaseType.id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9670,4 +9670,46 @@ public static void main(String[] args) {
String unexpectedOutput = "static synthetic int[] $SWITCH_TABLE$E();\n";
SwitchPatternTest.verifyClassFile(expectedOutput, unexpectedOutput, "X.class", ClassFileBytesDisassembler.SYSTEM);
}

// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/3559
// Boolean switch inconsistency between ECJ and javac
public void testIssue3559() throws Exception {
if (this.complianceLevel > ClassFileConstants.JDK23) // 21-23 testing is good enough
return;
runNegativeTest(
new String[] {
"X.java",
"""
public class X {
public static void main(String[] args) {
new X().d(true);
}
void d(Boolean b) {
switch (b) {
case true -> System.out.println("1");
case false -> System.out.println("2");
};
}
}
"""
},
"----------\n" +
"1. ERROR in X.java (at line 8)\n" +
" switch (b) {\n" +
" ^\n" +
"An enhanced switch statement should be exhaustive; a default label expected\n" +
"----------\n" +
"2. ERROR in X.java (at line 9)\n" +
" case true -> System.out.println(\"1\");\n" +
" ^^^^\n" +
"Case constant of type boolean is incompatible with switch selector type Boolean\n" +
"----------\n" +
"3. ERROR in X.java (at line 10)\n" +
" case false -> System.out.println(\"2\");\n" +
" ^^^^^\n" +
"Case constant of type boolean is incompatible with switch selector type Boolean\n" +
"----------\n");
}
}

0 comments on commit d22d033

Please sign in to comment.