diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/StackValue.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/StackValue.java index 3235f1d8056..ef7cbc20211 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/StackValue.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/StackValue.java @@ -395,7 +395,15 @@ public abstract class StackValue { } } else if (toType.getSort() == Type.ARRAY) { - v.checkcast(toType); + if (fromType.getSort() != Type.ARRAY) { + v.checkcast(toType); + } + else if (toType.getDimensions() != fromType.getDimensions()) { + v.checkcast(toType); + } + else if (!toType.getElementType().equals(OBJECT_TYPE)) { + v.checkcast(toType); + } } else if (toType.getSort() == Type.OBJECT) { if (fromType.getSort() == Type.OBJECT || fromType.getSort() == Type.ARRAY) { diff --git a/compiler/testData/codegen/box/casts/kt22714.kt b/compiler/testData/codegen/box/casts/kt22714.kt new file mode 100644 index 00000000000..ad7baa3f0b6 --- /dev/null +++ b/compiler/testData/codegen/box/casts/kt22714.kt @@ -0,0 +1,14 @@ +fun Array.getLength(): Int { + return this.size +} + +fun Any.getLength() = + if (this is Array<*>) size else -1 + +fun box(): String { + val array1: Array = arrayOf("1", "2", "3") + val array2: Any = arrayOf("1", "2", "3") + if (array1.getLength() + array2.getLength() != 6) + return "FAILURE" + return "OK" +} \ No newline at end of file diff --git a/compiler/testData/codegen/bytecodeText/checkcast/kt22714.kt b/compiler/testData/codegen/bytecodeText/checkcast/kt22714.kt new file mode 100644 index 00000000000..19197cf4790 --- /dev/null +++ b/compiler/testData/codegen/bytecodeText/checkcast/kt22714.kt @@ -0,0 +1,14 @@ +fun Array.getLength(): Int { + return this.size +} + +fun Any.getLength() = + if (this is Array<*>) size else -1 + +fun testCheckcast1(): Int { + val array1: Array = arrayOf("1", "2", "3") + val array2: Any = arrayOf("1", "2", "3") + return array1.getLength() + array2.getLength() +} + +// 1 CHECKCAST \ No newline at end of file diff --git a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index 3d77f92182d..854e9312d99 100644 --- a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -2556,6 +2556,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes doTest(fileName); } + @TestMetadata("kt22714.kt") + public void testKt22714() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/casts/kt22714.kt"); + doTest(fileName); + } + @TestMetadata("lambdaToUnitCast.kt") public void testLambdaToUnitCast() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/casts/lambdaToUnitCast.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index fe0773288e0..e0e45406768 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -2556,6 +2556,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { doTest(fileName); } + @TestMetadata("kt22714.kt") + public void testKt22714() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/casts/kt22714.kt"); + doTest(fileName); + } + @TestMetadata("lambdaToUnitCast.kt") public void testLambdaToUnitCast() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/casts/lambdaToUnitCast.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java index 76d81f03bd6..27b5762a1bc 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java @@ -782,6 +782,12 @@ public class BytecodeTextTestGenerated extends AbstractBytecodeTextTest { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/checkcast/kt15411.kt"); doTest(fileName); } + + @TestMetadata("kt22714.kt") + public void testKt22714() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/checkcast/kt22714.kt"); + doTest(fileName); + } } @TestMetadata("compiler/testData/codegen/bytecodeText/coercionToUnitOptimization") diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 411c91fcf71..1e5f3d90089 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -2556,6 +2556,12 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes doTest(fileName); } + @TestMetadata("kt22714.kt") + public void testKt22714() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/casts/kt22714.kt"); + doTest(fileName); + } + @TestMetadata("lambdaToUnitCast.kt") public void testLambdaToUnitCast() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/casts/lambdaToUnitCast.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java index a88eb441464..b6dea659a0b 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java @@ -2964,6 +2964,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { doTest(fileName); } + @TestMetadata("kt22714.kt") + public void testKt22714() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/casts/kt22714.kt"); + doTest(fileName); + } + @TestMetadata("lambdaToUnitCast.kt") public void testLambdaToUnitCast() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/casts/lambdaToUnitCast.kt");