From 9274241c0e5a05d10124b26e414f1ce0e829a180 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Peltier?= Date: Tue, 6 Feb 2018 15:56:29 +0100 Subject: [PATCH] KT-22714 Avoid to generate unecessary checkcast - StackValue already avoid to generate checkcast from a type or an array to java.lang.Object. Add a new case to avoid to generate a checkcast from an array to an array of java.lang.Object when arrays have the same dimensions. Fix of https://youtrack.jetbrains.com/issue/KT-22714 --- .../codegen/java8/box/jvm8/checkcast/kt22714.kt | 16 ++++++++++++++++ .../java8/bytecodeText/checkcast/kt22714.kt | 16 ++++++++++++++++ .../BlackBoxWithJava8CodegenTestGenerated.java | 15 +++++++++++++++ .../codegen/BytecodeTextJava8TestGenerated.java | 15 +++++++++++++++ 4 files changed, 62 insertions(+) create mode 100644 compiler/testData/codegen/java8/box/jvm8/checkcast/kt22714.kt create mode 100644 compiler/testData/codegen/java8/bytecodeText/checkcast/kt22714.kt diff --git a/compiler/testData/codegen/java8/box/jvm8/checkcast/kt22714.kt b/compiler/testData/codegen/java8/box/jvm8/checkcast/kt22714.kt new file mode 100644 index 00000000000..124d0cb26bb --- /dev/null +++ b/compiler/testData/codegen/java8/box/jvm8/checkcast/kt22714.kt @@ -0,0 +1,16 @@ +// JVM_TARGET: 1.8 + +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/java8/bytecodeText/checkcast/kt22714.kt b/compiler/testData/codegen/java8/bytecodeText/checkcast/kt22714.kt new file mode 100644 index 00000000000..6b8cb1fcc05 --- /dev/null +++ b/compiler/testData/codegen/java8/bytecodeText/checkcast/kt22714.kt @@ -0,0 +1,16 @@ +// JVM_TARGET: 1.8 + +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-java8/tests/org/jetbrains/kotlin/codegen/BlackBoxWithJava8CodegenTestGenerated.java b/compiler/tests-java8/tests/org/jetbrains/kotlin/codegen/BlackBoxWithJava8CodegenTestGenerated.java index fc09ec5687a..fb10685c510 100644 --- a/compiler/tests-java8/tests/org/jetbrains/kotlin/codegen/BlackBoxWithJava8CodegenTestGenerated.java +++ b/compiler/tests-java8/tests/org/jetbrains/kotlin/codegen/BlackBoxWithJava8CodegenTestGenerated.java @@ -333,6 +333,21 @@ public class BlackBoxWithJava8CodegenTestGenerated extends AbstractBlackBoxCodeg doTest(fileName); } + @TestMetadata("compiler/testData/codegen/java8/box/jvm8/checkcast") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Checkcast extends AbstractBlackBoxCodegenTest { + public void testAllFilesPresentInCheckcast() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/java8/box/jvm8/checkcast"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); + } + + @TestMetadata("kt22714.kt") + public void testKt22714() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/java8/box/jvm8/checkcast/kt22714.kt"); + doTest(fileName); + } + } + @TestMetadata("compiler/testData/codegen/java8/box/jvm8/defaults") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) diff --git a/compiler/tests-java8/tests/org/jetbrains/kotlin/codegen/BytecodeTextJava8TestGenerated.java b/compiler/tests-java8/tests/org/jetbrains/kotlin/codegen/BytecodeTextJava8TestGenerated.java index 32aa7415064..2dbc3d93076 100644 --- a/compiler/tests-java8/tests/org/jetbrains/kotlin/codegen/BytecodeTextJava8TestGenerated.java +++ b/compiler/tests-java8/tests/org/jetbrains/kotlin/codegen/BytecodeTextJava8TestGenerated.java @@ -25,6 +25,21 @@ public class BytecodeTextJava8TestGenerated extends AbstractBytecodeTextTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/java8/bytecodeText"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); } + @TestMetadata("compiler/testData/codegen/java8/bytecodeText/checkcast") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Checkcast extends AbstractBytecodeTextTest { + public void testAllFilesPresentInCheckcast() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/java8/bytecodeText/checkcast"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); + } + + @TestMetadata("kt22714.kt") + public void testKt22714() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/java8/bytecodeText/checkcast/kt22714.kt"); + doTest(fileName); + } + } + @TestMetadata("compiler/testData/codegen/java8/bytecodeText/hashCode") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)