From 4cfe68da1eba51b812ffdf27281e389042bd06ed Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Fri, 26 Oct 2012 18:03:40 +0400 Subject: [PATCH] Coerce any type to Number before unboxing --- .../org/jetbrains/jet/codegen/StackValue.java | 18 ++++++++---------- .../codegen/primitiveTypes/unboxComparable.kt | 5 +++++ .../jet/codegen/PrimitiveTypesTest.java | 4 ++++ 3 files changed, 17 insertions(+), 10 deletions(-) create mode 100644 compiler/testData/codegen/primitiveTypes/unboxComparable.kt diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/StackValue.java b/compiler/backend/src/org/jetbrains/jet/codegen/StackValue.java index c9e542c43d8..35e6645cd28 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/StackValue.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/StackValue.java @@ -294,16 +294,14 @@ public abstract class StackValue { } } else if (fromType.getSort() == Type.OBJECT && toType.getSort() <= Type.DOUBLE) { - if (fromType.equals(OBJECT_TYPE)) { - if (toType.getSort() == Type.BOOLEAN) { - v.checkcast(JvmPrimitiveType.BOOLEAN.getWrapper().getAsmType()); - } - else if (toType.getSort() == Type.CHAR) { - v.checkcast(JvmPrimitiveType.CHAR.getWrapper().getAsmType()); - } - else { - v.checkcast(getType(Number.class)); - } + if (toType.getSort() == Type.BOOLEAN) { + coerce(fromType, JvmPrimitiveType.BOOLEAN.getWrapper().getAsmType(), v); + } + else if (toType.getSort() == Type.CHAR) { + coerce(fromType, JvmPrimitiveType.CHAR.getWrapper().getAsmType(), v); + } + else { + coerce(fromType, getType(Number.class), v); } unbox(toType, v); } diff --git a/compiler/testData/codegen/primitiveTypes/unboxComparable.kt b/compiler/testData/codegen/primitiveTypes/unboxComparable.kt new file mode 100644 index 00000000000..870989087fd --- /dev/null +++ b/compiler/testData/codegen/primitiveTypes/unboxComparable.kt @@ -0,0 +1,5 @@ +fun foo(x: Int) = x + +fun bar(x: Comparable) = if (x is Int) foo(x) else 0 + +fun box() = if (bar(42) == 42) "OK" else "Fail" diff --git a/compiler/tests/org/jetbrains/jet/codegen/PrimitiveTypesTest.java b/compiler/tests/org/jetbrains/jet/codegen/PrimitiveTypesTest.java index 6fcdf26c62a..d93fcf50a58 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/PrimitiveTypesTest.java +++ b/compiler/tests/org/jetbrains/jet/codegen/PrimitiveTypesTest.java @@ -456,4 +456,8 @@ public class PrimitiveTypesTest extends CodegenTestCase { public void test2794() throws Exception { blackBoxFile("regressions/kt2794.kt"); } + + public void testUnboxComparable() { + blackBoxFile("primitiveTypes/unboxComparable.kt"); + } }