Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public boolean coversType(TypeBinding type, Scope scope) {
case BOXING_CONVERSION_AND_WIDENING_REFERENCE_CONVERSION:
return true;
case WIDENING_PRIMITIVE_CONVERSION:
return BaseTypeBinding.isExactWidening(this.resolvedType.id, type.id);
return BaseTypeBinding.isExactWidening(this.resolvedType.unboxedType().id, type.id);
case WIDENING_AND_NARROWING_PRIMITIVE_CONVERSION:
return false; // char->byte
/* §14.11.1.1 "CE contains a type pattern with a primitive type P,
Expand All @@ -118,9 +118,10 @@ public boolean coversValue(Constant cst, BlockScope scope) {
return false;
if (!(this.resolvedType.unboxedType() instanceof BaseTypeBinding baseType))
return false;
if (!cst.isExactTestingConversion(baseType))
return false;
int constantTypeID = cst.typeID();
// find route as well as flag preview
if (!cst.isExactTestingConversion(baseType))
return false; // flagging a preview already taken care above - let's return.
PrimitiveConversionRoute route = findPrimitiveConversionRoute(baseType, TypeBinding.wellKnownBaseType(constantTypeID), scope);
switch (route) {
// JLS §5.7.2:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,9 @@ private boolean duplicateConstant(LabelExpression current, LabelExpression prior
return false;
}

void gatherLabelExpression(LabelExpression labelExpression) {
void gatherLabelExpression(LabelExpression candidateLabelExpression) {
// domination check
if (labelExpression.expression instanceof Pattern pattern) {
if (candidateLabelExpression.expression instanceof Pattern pattern) {
if (this.defaultCase != null) {
this.scope.problemReporter().patternDominatedByAnother(pattern);
} else {
Expand All @@ -328,34 +328,35 @@ void gatherLabelExpression(LabelExpression labelExpression) {
}
}
} else {
if (labelExpression.expression instanceof NullLiteral) {
if (candidateLabelExpression.expression instanceof NullLiteral) {
if (this.defaultCase != null)
this.scope.problemReporter().patternDominatedByAnother(labelExpression.expression);
this.scope.problemReporter().patternDominatedByAnother(candidateLabelExpression.expression);
} else {
TypeBinding boxedType = labelExpression.type.isBaseType() ? this.scope.environment().computeBoxingType(labelExpression.type) : labelExpression.type;
// TypeBinding boxedType = labelExpression.type.isBaseType() ? this.scope.environment().computeBoxingType(labelExpression.type) : labelExpression.type;
TypeBinding candidateType = candidateLabelExpression.type;
for (int i = 0; i < this.labelExpressionIndex; i++) {
if (this.labelExpressions[i].expression instanceof Pattern priorPattern) {
if (priorPattern.coversType(boxedType, this.scope)) {
this.scope.problemReporter().patternDominatedByAnother(labelExpression.expression);
if (priorPattern.coversType(candidateType, this.scope)) {
this.scope.problemReporter().patternDominatedByAnother(candidateLabelExpression.expression);
break;
}
Constant cst = labelExpression.expression.constant;
Constant cst = candidateLabelExpression.expression.constant;
if (cst != null && cst != Constant.NotAConstant && priorPattern.coversValue(cst, this.scope)) {
this.scope.problemReporter().patternDominatedByAnother(labelExpression.expression);
this.scope.problemReporter().patternDominatedByAnother(candidateLabelExpression.expression);
break;
}
}
}
}
// duplicate constant check
for (int i = 0; i < this.labelExpressionIndex; i++) {
if (duplicateConstant(labelExpression, this.labelExpressions[i])) {
this.scope.problemReporter().duplicateCase(labelExpression.expression);
if (duplicateConstant(candidateLabelExpression, this.labelExpressions[i])) {
this.scope.problemReporter().duplicateCase(candidateLabelExpression.expression);
break;
}
}
}
this.labelExpressions[this.labelExpressionIndex++] = labelExpression;
this.labelExpressions[this.labelExpressionIndex++] = candidateLabelExpression;
}

private void complainIfNotExhaustiveSwitch(BlockScope upperScope, TypeBinding selectorType, CompilerOptions compilerOptions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class PrimitiveInPatternsTest extends AbstractRegressionTest9 {
static {
// TESTS_NUMBERS = new int [] { 1 };
// TESTS_RANGE = new int[] { 1, -1 };
// TESTS_NAMES = new String[] { "testDominanceIssue4979_001" };
// TESTS_NAMES = new String[] { "testDominanceIssue4979_002" };
}
private String extraLibPath;
public static Class<?> testClass() {
Expand Down Expand Up @@ -7648,4 +7648,70 @@ public int foo(Character c) {
"----------\n");
}

public void testDominanceIssue4979_002() {
runNegativeTest(new String[] {
"X.java",
"""
public class X {
public int foo1(Short c) {
int result = 0;
switch (c) {
case Short c1 -> {
result = c1;
break;
}
case (byte) 0 -> {
result = 0;
break;
}
}
return result;
}
}
"""
},
"----------\n" +
"1. WARNING in X.java (at line 5)\n" +
" case Short c1 -> {\n" +
" ^^^^^^^^\n" +
"You are using a preview language feature that may or may not be supported in a future release\n" +
"----------\n" +
"2. ERROR in X.java (at line 9)\n" +
" case (byte) 0 -> {\n" +
" ^^^^^^^^\n" +
"This case label is dominated by one of the preceding case labels\n" +
"----------\n");
}
public void testDominanceIssue4979_003() {
runNegativeTest(new String[] {
"X.java",
"""
@SuppressWarnings("preview")
public class X {
void foo() {
int j = 1;
switch(j) {
case byte b ->
System.out.println("A byte");
case 260 -> // not dominated
System.out.println("An int that can be represented as a byte exactly");
default ->
System.out.println("Integer that cannot be represented as a float exactly");
}
}
public static void main(String[] args) {
Zork();
}
}
"""
},
"----------\n" +
"1. ERROR in X.java (at line 15)\n" +
" Zork();\n" +
" ^^^^\n" +
"The method Zork() is undefined for the type X\n" +
"----------\n"
);
}

}
Loading