Skip to content

Commit

Permalink
Issue 2936 - Parameterized Patterns getting error flagged (#2947)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpalat authored Sep 12, 2024
1 parent 8cb27d9 commit 4e315ae
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -242,16 +242,17 @@ public static PrimitiveConversionRoute findPrimitiveConversionRoute(TypeBinding
//an unboxing conversion followed by a widening primitive conversion
if (BaseTypeBinding.isWidening(destinationType.id, unboxedExpressionType.id))
return PrimitiveConversionRoute.UNBOXING_AND_WIDENING_PRIMITIVE_CONVERSION;
} else if (destinationIsBaseType && expressionType.erasure().isBoxedPrimitiveType()) { // <T extends Integer> / <? extends Short> ...
int boxId = expressionType.erasure().id;
int exprPrimId = TypeIds.box2primitive(boxId);
if (exprPrimId == destinationType.id)
return PrimitiveConversionRoute.WIDENING_REFERENCE_AND_UNBOXING_COVERSION;
if (BaseTypeBinding.isWidening(destinationType.id, exprPrimId))
return PrimitiveConversionRoute.WIDENING_REFERENCE_AND_UNBOXING_COVERSION_AND_WIDENING_PRIMITIVE_CONVERSION;
} else if (destinationIsBaseType) {
if (expressionType.erasure().isBoxedPrimitiveType()) { // <T extends Integer> / <? extends Short> ...
int boxId = expressionType.erasure().id;
int exprPrimId = TypeIds.box2primitive(boxId);
if (exprPrimId == destinationType.id)
return PrimitiveConversionRoute.WIDENING_REFERENCE_AND_UNBOXING_COVERSION;
if (BaseTypeBinding.isWidening(destinationType.id, exprPrimId))
return PrimitiveConversionRoute.WIDENING_REFERENCE_AND_UNBOXING_COVERSION_AND_WIDENING_PRIMITIVE_CONVERSION;
}
TypeBinding boxedDestinationType = scope.environment().computeBoxingType(destinationType);
if (boxedDestinationType.isCompatibleWith(expressionType))
if (boxedDestinationType.isCompatibleWith(expressionType.erasure()))
return PrimitiveConversionRoute.NARROWING_AND_UNBOXING_CONVERSION;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class PrimitiveInPatternsTest extends AbstractRegressionTest9 {
static {
// TESTS_NUMBERS = new int [] { 1 };
// TESTS_RANGE = new int[] { 1, -1 };
// TESTS_NAMES = new String[] { "testByteToFloat" };
// TESTS_NAMES = new String[] { "testIssue2936" };
}
private String extraLibPath;
public static Class<?> testClass() {
Expand Down Expand Up @@ -7001,6 +7001,47 @@ <T extends Y> void foo(T single) {
"----------\n");
}

public void testIssue2936_001() {
runConformTest(new String[] {
"X.java",
"""
record R<Short>(Short s) {}
public class X {
public static <Short> short foo(R<Short> s) {
return switch (s) {
case R(Short s1) -> 1;
default -> 0;
};
}
public static void main(String[] args) {
Short s = 100;
R<Short> r = new R<>(s);
System.out.println(X.foo(r));
}
}
"""
},
"1");
}
public void testIssue2936_002() {
runConformTest(new String[] {
"X.java",
"""
public class X {
public static <T extends Object> float foo(T t) {
if (t instanceof float i) { return i; }
return 100.0f;
}
public static void main(String argv[]) {
System.out.println(X.foo(1.0f));
System.out.println(X.foo(2));
}
}
"""
},
"1.0\n" +
"100.0");
}
// test from spec
public void _testSpec001() {
runConformTest(new String[] {
Expand Down

0 comments on commit 4e315ae

Please sign in to comment.