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.

 #KT-22714 Fixed
This commit is contained in:
Mikaël Peltier
2018-02-06 15:56:29 +01:00
committed by Mikhael Bogdanov
parent 9a9452f73a
commit 126afbb8ac
8 changed files with 67 additions and 1 deletions
@@ -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) {
+14
View File
@@ -0,0 +1,14 @@
fun <T> Array<T>.getLength(): Int {
return this.size
}
fun Any.getLength() =
if (this is Array<*>) size else -1
fun box(): String {
val array1: Array<String> = arrayOf("1", "2", "3")
val array2: Any = arrayOf("1", "2", "3")
if (array1.getLength() + array2.getLength() != 6)
return "FAILURE"
return "OK"
}
@@ -0,0 +1,14 @@
fun <T> Array<T>.getLength(): Int {
return this.size
}
fun Any.getLength() =
if (this is Array<*>) size else -1
fun testCheckcast1(): Int {
val array1: Array<String> = arrayOf("1", "2", "3")
val array2: Any = arrayOf("1", "2", "3")
return array1.getLength() + array2.getLength()
}
// 1 CHECKCAST
@@ -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");
@@ -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");
@@ -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")
@@ -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");
@@ -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");