KT-53465, KT-53677 Get rid of unnecessary checkcasts to array of reified type

This commit is contained in:
Pavel Mikhailovskii
2022-09-06 11:12:42 +02:00
committed by teamcity
parent d8522a8967
commit a75d5ba4cf
17 changed files with 213 additions and 1 deletions
@@ -4631,6 +4631,24 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/casts"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@Test
@TestMetadata("arrayDowncastingContravariant.kt")
public void testArrayDowncastingContravariant() throws Exception {
runTest("compiler/testData/codegen/box/casts/arrayDowncastingContravariant.kt");
}
@Test
@TestMetadata("arrayDowncatingInvariant.kt")
public void testArrayDowncatingInvariant() throws Exception {
runTest("compiler/testData/codegen/box/casts/arrayDowncatingInvariant.kt");
}
@Test
@TestMetadata("arrayDownctingCovariant.kt")
public void testArrayDownctingCovariant() throws Exception {
runTest("compiler/testData/codegen/box/casts/arrayDownctingCovariant.kt");
}
@Test
@TestMetadata("as.kt")
public void testAs() throws Exception {
@@ -4757,6 +4775,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/casts/kt50577.kt");
}
@Test
@TestMetadata("kt53677.kt")
public void testKt53677() throws Exception {
runTest("compiler/testData/codegen/box/casts/kt53677.kt");
}
@Test
@TestMetadata("lambdaToUnitCast.kt")
public void testLambdaToUnitCast() throws Exception {
@@ -1092,6 +1092,12 @@ public class FirBytecodeTextTestGenerated extends AbstractFirBytecodeTextTest {
runTest("compiler/testData/codegen/bytecodeText/checkcast/kt22714.kt");
}
@Test
@TestMetadata("kt53465.kt")
public void testKt53465() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/checkcast/kt53465.kt");
}
@Test
@TestMetadata("noCheckcastOnDelegatingDefaultImplsCall.kt")
public void testNoCheckcastOnDelegatingDefaultImplsCall() throws Exception {
@@ -19,6 +19,7 @@ import org.jetbrains.kotlin.backend.jvm.JvmLoweredDeclarationOrigin
import org.jetbrains.kotlin.backend.jvm.ir.*
import org.jetbrains.kotlin.backend.jvm.unboxInlineClass
import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
import org.jetbrains.kotlin.ir.IrBuiltIns
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.IrStatement
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
@@ -36,6 +37,9 @@ import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
import org.jetbrains.kotlin.ir.visitors.acceptVoid
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.types.Variance
import org.jetbrains.kotlin.types.checker.SimpleClassicTypeSystemContext.getArgument
import org.jetbrains.kotlin.types.checker.SimpleClassicTypeSystemContext.isTypeVariableType
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
import org.jetbrains.org.objectweb.asm.Handle
import org.jetbrains.org.objectweb.asm.Opcodes
@@ -88,6 +92,8 @@ private class TypeOperatorLowering(private val backendContext: JvmBackendContext
builder.irAs(argument, type)
argument.type.isInlineClassType() && argument.type.isSubtypeOfClass(type.erasedUpperBound.symbol) ->
argument
isCompatibleArrayType(argument.type, type) ->
argument
type.isNullable() || argument.isDefinitelyNotNull() ->
builder.irAs(argument, type)
else -> {
@@ -121,6 +127,22 @@ private class TypeOperatorLowering(private val backendContext: JvmBackendContext
}
}
private fun isCompatibleArrayType(actualType: IrType, expectedType: IrType): Boolean {
var actual = actualType
var expected = expectedType
while ((actual.isArray() || actual.isNullableArray()) && (expected.isArray() || expected.isNullableArray())) {
actual = actual.getArrayElementLowerType()
expected = expected.getArrayElementLowerType()
}
if (actual == actualType || expected == expectedType) return false
return actual.isSubtypeOfClass(expected.erasedUpperBound.symbol)
}
private fun IrType.getArrayElementLowerType(): IrType =
if (isBoxedArray && this is IrSimpleType && (arguments.singleOrNull() as? IrTypeProjection)?.variance == Variance.IN_VARIANCE)
backendContext.irBuiltIns.anyNType
else getArrayElementType(backendContext.irBuiltIns)
// TODO extract null check elimination on IR somewhere?
private fun IrExpression.isDefinitelyNotNull(): Boolean =
when (this) {
@@ -0,0 +1,10 @@
// TARGET_BACKEND: JVM
inline fun <reified T : CharSequence> f(x: Array<in String>) = x as Array<T>
fun box(): String = try {
f<String>(arrayOf<Any>(42))
"Fail"
} catch (e: Exception) {
"OK"
}
@@ -0,0 +1,10 @@
// TARGET_BACKEND: JVM
inline fun <reified T : CharSequence> f(x: Array<Any>) = x as Array<T>
fun box(): String = try {
f<String>(arrayOf<Any>(42))
"Fail"
} catch (e: Exception) {
"OK"
}
@@ -0,0 +1,10 @@
// TARGET_BACKEND: JVM
inline fun <reified T : CharSequence> f(x: Array<out Any>) = x as Array<T>
fun box(): String = try {
f<String>(arrayOf<Int>(42))
"Fail"
} catch (e: Exception) {
"OK"
}
@@ -2,8 +2,13 @@ abstract class Base {
private fun test(): String = "OK"
fun test(d: Derived): String = (d as Base).test()
fun test(d: Array<out Derived>) = (d as Array<out Base>)[0].test()
}
class Derived : Base()
fun box(): String = Derived().test(Derived())
fun box(): String {
Derived().test(arrayOf(Derived()))
return Derived().test(Derived())
}
+29
View File
@@ -0,0 +1,29 @@
// WITH_STDLIB
// WITH_COROUTINES
// DONT_TARGET_EXACT_BACKEND: JVM
// DONT_TARGET_EXACT_BACKEND: JS
import kotlin.coroutines.*
public inline fun <reified T> myEmptyArray(): Array<T> = arrayOfNulls<T>(0) as Array<T>
inline fun <reified T> Array<out T>?.myOrEmpty(): Array<out T> = this ?: myEmptyArray<T>()
fun <T> runBlocking(c: suspend () -> T): T {
var res: T? = null
c.startCoroutine(Continuation(EmptyCoroutineContext) {
res = it.getOrThrow()
})
return res!!
}
suspend fun suspendHere(x: String) {}
suspend fun main() {
arrayOf("1").myOrEmpty().forEach { suspendHere(it) }
}
fun box(): String {
runBlocking(::main)
return "OK"
}
@@ -0,0 +1,15 @@
// TARGET_BACKEND: JVM_IR
public inline fun <reified T> myEmptyArray(): Array<T> = arrayOfNulls<T>(0) as Array<T>
inline fun <reified T> Array<out T>?.myOrEmpty(): Array<out T> = this ?: myEmptyArray<T>()
fun foo(a : Array<String>?) = a.myOrEmpty()
val a = arrayOf<Int>(1) as Array<Any>
val b = arrayOf<Int>(1) as Array<Int>
val c = arrayOf(arrayOf<Int>(1)) as Array<Array<Any>?>
// 0 CHECKCAST
@@ -4517,6 +4517,24 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/casts"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM, true);
}
@Test
@TestMetadata("arrayDowncastingContravariant.kt")
public void testArrayDowncastingContravariant() throws Exception {
runTest("compiler/testData/codegen/box/casts/arrayDowncastingContravariant.kt");
}
@Test
@TestMetadata("arrayDowncatingInvariant.kt")
public void testArrayDowncatingInvariant() throws Exception {
runTest("compiler/testData/codegen/box/casts/arrayDowncatingInvariant.kt");
}
@Test
@TestMetadata("arrayDownctingCovariant.kt")
public void testArrayDownctingCovariant() throws Exception {
runTest("compiler/testData/codegen/box/casts/arrayDownctingCovariant.kt");
}
@Test
@TestMetadata("as.kt")
public void testAs() throws Exception {
@@ -4631,6 +4631,24 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/casts"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@Test
@TestMetadata("arrayDowncastingContravariant.kt")
public void testArrayDowncastingContravariant() throws Exception {
runTest("compiler/testData/codegen/box/casts/arrayDowncastingContravariant.kt");
}
@Test
@TestMetadata("arrayDowncatingInvariant.kt")
public void testArrayDowncatingInvariant() throws Exception {
runTest("compiler/testData/codegen/box/casts/arrayDowncatingInvariant.kt");
}
@Test
@TestMetadata("arrayDownctingCovariant.kt")
public void testArrayDownctingCovariant() throws Exception {
runTest("compiler/testData/codegen/box/casts/arrayDownctingCovariant.kt");
}
@Test
@TestMetadata("as.kt")
public void testAs() throws Exception {
@@ -4757,6 +4775,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/casts/kt50577.kt");
}
@Test
@TestMetadata("kt53677.kt")
public void testKt53677() throws Exception {
runTest("compiler/testData/codegen/box/casts/kt53677.kt");
}
@Test
@TestMetadata("lambdaToUnitCast.kt")
public void testLambdaToUnitCast() throws Exception {
@@ -1092,6 +1092,12 @@ public class IrBytecodeTextTestGenerated extends AbstractIrBytecodeTextTest {
runTest("compiler/testData/codegen/bytecodeText/checkcast/kt22714.kt");
}
@Test
@TestMetadata("kt53465.kt")
public void testKt53465() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/checkcast/kt53465.kt");
}
@Test
@TestMetadata("noCheckcastOnDelegatingDefaultImplsCall.kt")
public void testNoCheckcastOnDelegatingDefaultImplsCall() throws Exception {
@@ -7,6 +7,7 @@ package org.jetbrains.kotlin.codegen;
import com.android.tools.r8.*;
import com.android.tools.r8.origin.PathOrigin;
import com.android.tools.r8.utils.ExceptionDiagnostic;
import kotlin.Pair;
import org.jetbrains.kotlin.backend.common.output.OutputFile;
import org.jetbrains.kotlin.test.KtAssert;
@@ -3946,6 +3946,21 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/casts"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM, true);
}
@TestMetadata("arrayDowncastingContravariant.kt")
public void testArrayDowncastingContravariant() throws Exception {
runTest("compiler/testData/codegen/box/casts/arrayDowncastingContravariant.kt");
}
@TestMetadata("arrayDowncatingInvariant.kt")
public void testArrayDowncatingInvariant() throws Exception {
runTest("compiler/testData/codegen/box/casts/arrayDowncatingInvariant.kt");
}
@TestMetadata("arrayDownctingCovariant.kt")
public void testArrayDownctingCovariant() throws Exception {
runTest("compiler/testData/codegen/box/casts/arrayDownctingCovariant.kt");
}
@TestMetadata("as.kt")
public void testAs() throws Exception {
runTest("compiler/testData/codegen/box/casts/as.kt");
@@ -3449,6 +3449,12 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/casts/kt50577.kt");
}
@Test
@TestMetadata("kt53677.kt")
public void testKt53677() throws Exception {
runTest("compiler/testData/codegen/box/casts/kt53677.kt");
}
@Test
@TestMetadata("lambdaToUnitCast.kt")
public void testLambdaToUnitCast() throws Exception {
@@ -3046,6 +3046,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest
runTest("compiler/testData/codegen/box/casts/kt50577.kt");
}
@TestMetadata("kt53677.kt")
public void testKt53677() throws Exception {
runTest("compiler/testData/codegen/box/casts/kt53677.kt");
}
@TestMetadata("lambdaToUnitCast.kt")
public void testLambdaToUnitCast() throws Exception {
runTest("compiler/testData/codegen/box/casts/lambdaToUnitCast.kt");
@@ -3523,6 +3523,12 @@ public class NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTest
runTest("compiler/testData/codegen/box/casts/kt50577.kt");
}
@Test
@TestMetadata("kt53677.kt")
public void testKt53677() throws Exception {
runTest("compiler/testData/codegen/box/casts/kt53677.kt");
}
@Test
@TestMetadata("lambdaToUnitCast.kt")
public void testLambdaToUnitCast() throws Exception {