Skip to content

Commit c4b7ca7

Browse files
committed
switch dominance 4979
1 parent 687b99d commit c4b7ca7

3 files changed

Lines changed: 49 additions & 16 deletions

File tree

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/Pattern.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public boolean coversType(TypeBinding type, Scope scope) {
9494
case BOXING_CONVERSION_AND_WIDENING_REFERENCE_CONVERSION:
9595
return true;
9696
case WIDENING_PRIMITIVE_CONVERSION:
97-
return BaseTypeBinding.isExactWidening(this.resolvedType.id, type.id);
97+
return BaseTypeBinding.isExactWidening(this.resolvedType.unboxedType().id, type.id);
9898
case WIDENING_AND_NARROWING_PRIMITIVE_CONVERSION:
9999
return false; // char->byte
100100
/* §14.11.1.1 "CE contains a type pattern with a primitive type P,
@@ -120,9 +120,9 @@ public boolean coversValue(Constant cst, BlockScope scope) {
120120
return false;
121121
int constantTypeID = cst.typeID();
122122
// find route as well as flag preview
123+
if (!cst.isExactTestingConversion(baseType))
124+
return false; // flagging a preview already taken care above - let's return.
123125
PrimitiveConversionRoute route = findPrimitiveConversionRoute(baseType, TypeBinding.wellKnownBaseType(constantTypeID), scope);
124-
if (cst.isExactTestingConversion(baseType))
125-
return true; // flagging a preview already taken care above - let's return.
126126
switch (route) {
127127
// JLS §5.7.2:
128128
case NARROWING_PRIMITVE_CONVERSION:

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/SwitchStatement.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,9 @@ private boolean duplicateConstant(LabelExpression current, LabelExpression prior
314314
return false;
315315
}
316316

317-
void gatherLabelExpression(LabelExpression labelExpression) {
317+
void gatherLabelExpression(LabelExpression candidateLabelExpression) {
318318
// domination check
319-
if (labelExpression.expression instanceof Pattern pattern) {
319+
if (candidateLabelExpression.expression instanceof Pattern pattern) {
320320
if (this.defaultCase != null) {
321321
this.scope.problemReporter().patternDominatedByAnother(pattern);
322322
} else {
@@ -328,34 +328,35 @@ void gatherLabelExpression(LabelExpression labelExpression) {
328328
}
329329
}
330330
} else {
331-
if (labelExpression.expression instanceof NullLiteral) {
331+
if (candidateLabelExpression.expression instanceof NullLiteral) {
332332
if (this.defaultCase != null)
333-
this.scope.problemReporter().patternDominatedByAnother(labelExpression.expression);
333+
this.scope.problemReporter().patternDominatedByAnother(candidateLabelExpression.expression);
334334
} else {
335-
TypeBinding boxedType = labelExpression.type.isBaseType() ? this.scope.environment().computeBoxingType(labelExpression.type) : labelExpression.type;
335+
// TypeBinding boxedType = labelExpression.type.isBaseType() ? this.scope.environment().computeBoxingType(labelExpression.type) : labelExpression.type;
336+
TypeBinding candidateType = candidateLabelExpression.type;
336337
for (int i = 0; i < this.labelExpressionIndex; i++) {
337338
if (this.labelExpressions[i].expression instanceof Pattern priorPattern) {
338-
if (priorPattern.coversType(boxedType, this.scope)) {
339-
this.scope.problemReporter().patternDominatedByAnother(labelExpression.expression);
339+
if (priorPattern.coversType(candidateType, this.scope)) {
340+
this.scope.problemReporter().patternDominatedByAnother(candidateLabelExpression.expression);
340341
break;
341342
}
342-
Constant cst = labelExpression.expression.constant;
343+
Constant cst = candidateLabelExpression.expression.constant;
343344
if (cst != null && cst != Constant.NotAConstant && priorPattern.coversValue(cst, this.scope)) {
344-
this.scope.problemReporter().patternDominatedByAnother(labelExpression.expression);
345+
this.scope.problemReporter().patternDominatedByAnother(candidateLabelExpression.expression);
345346
break;
346347
}
347348
}
348349
}
349350
}
350351
// duplicate constant check
351352
for (int i = 0; i < this.labelExpressionIndex; i++) {
352-
if (duplicateConstant(labelExpression, this.labelExpressions[i])) {
353-
this.scope.problemReporter().duplicateCase(labelExpression.expression);
353+
if (duplicateConstant(candidateLabelExpression, this.labelExpressions[i])) {
354+
this.scope.problemReporter().duplicateCase(candidateLabelExpression.expression);
354355
break;
355356
}
356357
}
357358
}
358-
this.labelExpressions[this.labelExpressionIndex++] = labelExpression;
359+
this.labelExpressions[this.labelExpressionIndex++] = candidateLabelExpression;
359360
}
360361

361362
private void complainIfNotExhaustiveSwitch(BlockScope upperScope, TypeBinding selectorType, CompilerOptions compilerOptions) {

org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/PrimitiveInPatternsTest.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class PrimitiveInPatternsTest extends AbstractRegressionTest9 {
3030
static {
3131
// TESTS_NUMBERS = new int [] { 1 };
3232
// TESTS_RANGE = new int[] { 1, -1 };
33-
// TESTS_NAMES = new String[] { "testDominanceIssue4979_002" };
33+
// TESTS_NAMES = new String[] { "testDominanceIssue4979_002" };
3434
}
3535
private String extraLibPath;
3636
public static Class<?> testClass() {
@@ -7682,4 +7682,36 @@ public int foo1(Short c) {
76827682
"This case label is dominated by one of the preceding case labels\n" +
76837683
"----------\n");
76847684
}
7685+
public void testDominanceIssue4979_003() {
7686+
runNegativeTest(new String[] {
7687+
"X.java",
7688+
"""
7689+
@SuppressWarnings("preview")
7690+
public class X {
7691+
void foo() {
7692+
int j = 1;
7693+
switch(j) {
7694+
case byte b ->
7695+
System.out.println("A byte");
7696+
case 260 -> // not dominated
7697+
System.out.println("An int that can be represented as a byte exactly");
7698+
default ->
7699+
System.out.println("Integer that cannot be represented as a float exactly");
7700+
}
7701+
}
7702+
public static void main(String[] args) {
7703+
Zork();
7704+
}
7705+
}
7706+
"""
7707+
},
7708+
"----------\n" +
7709+
"1. ERROR in X.java (at line 15)\n" +
7710+
" Zork();\n" +
7711+
" ^^^^\n" +
7712+
"The method Zork() is undefined for the type X\n" +
7713+
"----------\n"
7714+
);
7715+
}
7716+
76857717
}

0 commit comments

Comments
 (0)