Skip to content

Commit c60ae50

Browse files
committed
Fixes issue #4979 - Unboxed Primitive dominance checked
1 parent 0b27f84 commit c60ae50

2 files changed

Lines changed: 37 additions & 3 deletions

File tree

  • org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast
  • org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,12 @@ public boolean coversType(TypeBinding type, Scope scope) {
116116
public boolean coversValue(Constant cst, BlockScope scope) {
117117
if (!isUnguarded())
118118
return false;
119-
if (!(this.resolvedType instanceof BaseTypeBinding baseType))
119+
if (!(this.resolvedType.unboxedType() instanceof BaseTypeBinding baseType))
120120
return false;
121121
if (!cst.isExactTestingConversion(baseType))
122122
return false;
123123
int constantTypeID = cst.typeID();
124-
PrimitiveConversionRoute route = findPrimitiveConversionRoute(this.resolvedType, TypeBinding.wellKnownBaseType(constantTypeID), scope);
124+
PrimitiveConversionRoute route = findPrimitiveConversionRoute(baseType, TypeBinding.wellKnownBaseType(constantTypeID), scope);
125125
switch (route) {
126126
// JLS §5.7.2:
127127
case NARROWING_PRIMITVE_CONVERSION:

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

Lines changed: 35 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[] { "testSwitchPrimitiveboolean_04" };
33+
// TESTS_NAMES = new String[] { "testDominanceIssue4979_001" };
3434
}
3535
private String extraLibPath;
3636
public static Class<?> testClass() {
@@ -7613,5 +7613,39 @@ public static void main(String[] args) {
76137613
" #31 10.0\n";
76147614
verifyClassFile(expectedOutput, "X.class", ClassFileBytesDisassembler.SYSTEM);
76157615
}
7616+
public void testDominanceIssue4979_001() {
7617+
runNegativeTest(new String[] {
7618+
"X.java",
7619+
"""
7620+
public class X {
7621+
public int foo(Character c) {
7622+
int result = 0;
7623+
switch (c) {
7624+
case Character c1 -> {
7625+
result = c1;
7626+
break;
7627+
}
7628+
case 0 -> { // Same goes for case (int) 0
7629+
result = 0;
7630+
break;
7631+
}
7632+
}
7633+
return result;
7634+
}
7635+
}
7636+
"""
7637+
},
7638+
"----------\n" +
7639+
"1. WARNING in X.java (at line 5)\n" +
7640+
" case Character c1 -> {\n" +
7641+
" ^^^^^^^^^^^^\n" +
7642+
"You are using a preview language feature that may or may not be supported in a future release\n" +
7643+
"----------\n" +
7644+
"2. ERROR in X.java (at line 9)\n" +
7645+
" case 0 -> { // Same goes for case (int) 0\n" +
7646+
" ^\n" +
7647+
"This case label is dominated by one of the preceding case labels\n" +
7648+
"----------\n");
7649+
}
76167650

76177651
}

0 commit comments

Comments
 (0)