Skip to content

Commit

Permalink
[23] ECJ accepts both boolean values and a default case in a switch (#…
Browse files Browse the repository at this point in the history
…2919)

fixes #2916
  • Loading branch information
stephan-herrmann authored Sep 5, 2024
1 parent 3a89f5f commit 9706123
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2696,4 +2696,9 @@ public interface IProblem {
*/
int IncompatibleCaseType = PreviewRelated + 2101;

/**
* @since 3.39
* @noreference preview feature
*/
int DefaultTrueAndFalseCases = PreviewRelated + 2102;
}
Original file line number Diff line number Diff line change
Expand Up @@ -1272,6 +1272,11 @@ public void resolve(BlockScope upperScope) {
patternVariables = LocalVariableBinding.merge(patternVariables, statement.bindingsWhenComplete());
}
}
if (expressionType != null
&& (expressionType.id == TypeIds.T_boolean || expressionType.id == TypeIds.T_JavaLangBoolean)
&& defaultFound && isExhaustive()) {
upperScope.problemReporter().caseDefaultPlusTrueAndFalse(this);
}
if (length != counter) { // resize constants array
System.arraycopy(this.otherConstants, 0, this.otherConstants = new ResolvedCase[counter], 0, counter);
System.arraycopy(this.constants, 0, this.constants = new int[counter], 0, counter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1676,6 +1676,14 @@ public void caseConstantIncompatible(TypeBinding resolvedType, TypeBinding switc
expression.sourceStart,
expression.sourceEnd);
}
public void caseDefaultPlusTrueAndFalse(ASTNode location) {
this.handle(
IProblem.DefaultTrueAndFalseCases,
NoArgument,
NoArgument,
location.sourceStart,
location.sourceEnd);
}
public void classExtendFinalClass(SourceTypeBinding type, TypeReference superclass, TypeBinding superTypeBinding) {
String name = new String(type.sourceName());
String superTypeFullName = new String(superTypeBinding.readableName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,7 @@
# JEP 455 Primitive Types in Patterns, instanceof, and switch (Preview)
2100 = Case constants in a switch on ''{0}'' must have type ''{1}''
2101 = Case constant of type {0} is incompatible with switch selector type {1}
2102 = Switch cannot have both boolean values and a default label

### ELABORATIONS
## Access restrictions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ class ProblemAttributes {
expectedProblemAttributes.put("DeadCode", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM));
expectedProblemAttributes.put("DefaultMethodNotBelow18", new ProblemAttributes(CategorizedProblem.CAT_SYNTAX));
expectedProblemAttributes.put("DefaultMethodOverridesObjectMethod", new ProblemAttributes(CategorizedProblem.CAT_MEMBER));
expectedProblemAttributes.put("DefaultTrueAndFalseCases", new ProblemAttributes(CategorizedProblem.CAT_PREVIEW_RELATED));
expectedProblemAttributes.put("DereferencingNullableExpression", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM));
expectedProblemAttributes.put("DiamondNotBelow17", new ProblemAttributes(CategorizedProblem.CAT_TYPE));
expectedProblemAttributes.put("DirectInvocationOfAbstractMethod", new ProblemAttributes(CategorizedProblem.CAT_MEMBER));
Expand Down Expand Up @@ -1577,6 +1578,7 @@ class ProblemAttributes {
expectedProblemAttributes.put("DeadCode", new ProblemAttributes(JavaCore.COMPILER_PB_DEAD_CODE));
expectedProblemAttributes.put("DefaultMethodNotBelow18", SKIP);
expectedProblemAttributes.put("DefaultMethodOverridesObjectMethod", SKIP);
expectedProblemAttributes.put("DefaultTrueAndFalseCases", SKIP);
expectedProblemAttributes.put("DereferencingNullableExpression", new ProblemAttributes(JavaCore.COMPILER_PB_POTENTIAL_NULL_REFERENCE));
expectedProblemAttributes.put("DiamondNotBelow17", SKIP);
expectedProblemAttributes.put("DirectInvocationOfAbstractMethod", SKIP);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1907,7 +1907,7 @@ public static void main(String... args) {
},
"10");
}
public void testBooleanSwitchExhaustive_NOK() {
public void testBooleanSwitchExhaustive_NOK_1() {
runNegativeTest(new String[] {
"X.java",
"""
Expand All @@ -1929,6 +1929,64 @@ static int m1(boolean b) {
----------
""");
}
public void testBooleanSwitchExhaustive_NOK_2() {
runNegativeTest(new String[] {
"X.java",
"""
public class X {
static int m1(boolean b) {
return switch (b) {
case true -> 1;
case false -> 2;
default -> 3;
};
}
}
"""
},
"""
----------
1. ERROR in X.java (at line 3)
return switch (b) {
case true -> 1;
case false -> 2;
default -> 3;
};
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Switch cannot have both boolean values and a default label
----------
""");
}
public void testBooleanSwitchExhaustive_NOK_3() {
runNegativeTest(new String[] {
"X.java",
"""
public class X {
public void foo(Boolean b) {
final boolean TRUE = true;
final boolean FALSE = false;
switch (b) {
case TRUE -> { break;}
case FALSE -> { break;}
default -> { break;}
}
}
}
"""
},
"""
----------
1. ERROR in X.java (at line 5)
switch (b) {
case TRUE -> { break;}
case FALSE -> { break;}
default -> { break;}
}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Switch cannot have both boolean values and a default label
----------
""");
}

// exhaustiveness with identity conversion is already cover testNarrowingInSwitchFrom()

Expand Down

0 comments on commit 9706123

Please sign in to comment.