Don't remove checkcast for reified values

#KT-35511 Fixed
This commit is contained in:
Mikhael Bogdanov
2021-01-26 12:42:57 +01:00
parent 0b0ba7d987
commit 16928d6e3f
13 changed files with 174 additions and 2 deletions
@@ -28,7 +28,8 @@ import org.jetbrains.org.objectweb.asm.tree.TypeInsnNode
class RedundantCheckCastEliminationMethodTransformer : MethodTransformer() {
override fun transform(internalClassName: String, methodNode: MethodNode) {
val insns = methodNode.instructions.toArray()
if (!insns.any { it.opcode == Opcodes.CHECKCAST }) return
if (!insns.any { it.opcode == Opcodes.CHECKCAST}) return
if (insns.any { ReifiedTypeInliner.isOperationReifiedMarker(it) }) return
val redundantCheckCasts = ArrayList<TypeInsnNode>()
@@ -36,7 +37,6 @@ class RedundantCheckCastEliminationMethodTransformer : MethodTransformer() {
for (i in insns.indices) {
val valueType = frames[i]?.top()?.type ?: continue
val insn = insns[i]
if (ReifiedTypeInliner.isOperationReifiedMarker(insn.previous)) continue
if (insn is TypeInsnNode) {
val insnType = Type.getObjectType(insn.desc)
@@ -3183,6 +3183,16 @@ public class FirBlackBoxInlineCodegenTestGenerated extends AbstractFirBlackBoxIn
runTest("compiler/testData/codegen/boxInline/reified/kt18977.kt");
}
@TestMetadata("kt35511.kt")
public void testKt35511() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt35511.kt");
}
@TestMetadata("kt35511_try.kt")
public void testKt35511_try() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt35511_try.kt");
}
@TestMetadata("kt6988.kt")
public void testKt6988() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt6988.kt");
+35
View File
@@ -0,0 +1,35 @@
// JVM_TARGET: 1.8
// FILE: 1.kt
// WITH_RUNTIME
package test
open class Base(val name: String)
class A(name: String) : Base(name)
class B(name: String) : Base(name)
var result = "fail"
fun foo(base: Array<out Base>) {
result = base[0].name
}
fun cond() = true
inline fun <reified T : Base, reified Y : Base> process(a: Base) {
val z = if (cond())
arrayOf<T>(a as T)
else
arrayOf<Y>(a as Y)
foo(z)
}
// FILE: 2.kt
import test.*
fun box(): String {
process<A, B>(A("OK"))
return result
}
@@ -0,0 +1,37 @@
// JVM_TARGET: 1.8
// FILE: 1.kt
// WITH_RUNTIME
package test
open class Base(val name: String)
class A(name: String) : Base(name)
class B(name: String) : Base(name)
var result = "fail"
fun foo(base: Array<out Base>) {
result = base[0].name
}
fun cond() = true
inline fun <reified T : Base, reified Y : Base> process(a: Base) {
val z = try {
arrayOf<T>(a as T)
} catch (e: Exception) {
arrayOf<Y>(a as Y)
}
foo(z)
}
// FILE: 2.kt
import test.*
fun box(): String {
process<A, B>(B("OK"))
return result
}
@@ -3183,6 +3183,16 @@ public class BlackBoxInlineCodegenTestGenerated extends AbstractBlackBoxInlineCo
runTest("compiler/testData/codegen/boxInline/reified/kt18977.kt");
}
@TestMetadata("kt35511.kt")
public void testKt35511() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt35511.kt");
}
@TestMetadata("kt35511_try.kt")
public void testKt35511_try() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt35511_try.kt");
}
@TestMetadata("kt6988.kt")
public void testKt6988() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt6988.kt");
@@ -3183,6 +3183,16 @@ public class CompileKotlinAgainstInlineKotlinTestGenerated extends AbstractCompi
runTest("compiler/testData/codegen/boxInline/reified/kt18977.kt");
}
@TestMetadata("kt35511.kt")
public void testKt35511() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt35511.kt");
}
@TestMetadata("kt35511_try.kt")
public void testKt35511_try() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt35511_try.kt");
}
@TestMetadata("kt6988.kt")
public void testKt6988() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt6988.kt");
@@ -3183,6 +3183,16 @@ public class IrBlackBoxInlineCodegenTestGenerated extends AbstractIrBlackBoxInli
runTest("compiler/testData/codegen/boxInline/reified/kt18977.kt");
}
@TestMetadata("kt35511.kt")
public void testKt35511() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt35511.kt");
}
@TestMetadata("kt35511_try.kt")
public void testKt35511_try() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt35511_try.kt");
}
@TestMetadata("kt6988.kt")
public void testKt6988() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt6988.kt");
@@ -3183,6 +3183,16 @@ public class IrCompileKotlinAgainstInlineKotlinTestGenerated extends AbstractIrC
runTest("compiler/testData/codegen/boxInline/reified/kt18977.kt");
}
@TestMetadata("kt35511.kt")
public void testKt35511() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt35511.kt");
}
@TestMetadata("kt35511_try.kt")
public void testKt35511_try() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt35511_try.kt");
}
@TestMetadata("kt6988.kt")
public void testKt6988() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt6988.kt");
@@ -3183,6 +3183,16 @@ public class JvmIrAgainstOldBoxInlineTestGenerated extends AbstractJvmIrAgainstO
runTest("compiler/testData/codegen/boxInline/reified/kt18977.kt");
}
@TestMetadata("kt35511.kt")
public void testKt35511() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt35511.kt");
}
@TestMetadata("kt35511_try.kt")
public void testKt35511_try() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt35511_try.kt");
}
@TestMetadata("kt6988.kt")
public void testKt6988() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt6988.kt");
@@ -3183,6 +3183,16 @@ public class JvmOldAgainstIrBoxInlineTestGenerated extends AbstractJvmOldAgainst
runTest("compiler/testData/codegen/boxInline/reified/kt18977.kt");
}
@TestMetadata("kt35511.kt")
public void testKt35511() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt35511.kt");
}
@TestMetadata("kt35511_try.kt")
public void testKt35511_try() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt35511_try.kt");
}
@TestMetadata("kt6988.kt")
public void testKt6988() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt6988.kt");
@@ -2898,6 +2898,16 @@ public class IrJsCodegenInlineES6TestGenerated extends AbstractIrJsCodegenInline
runTest("compiler/testData/codegen/boxInline/reified/kt18977.kt");
}
@TestMetadata("kt35511.kt")
public void testKt35511() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt35511.kt");
}
@TestMetadata("kt35511_try.kt")
public void testKt35511_try() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt35511_try.kt");
}
@TestMetadata("kt7017.kt")
public void testKt7017() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt7017.kt");
@@ -2898,6 +2898,16 @@ public class IrJsCodegenInlineTestGenerated extends AbstractIrJsCodegenInlineTes
runTest("compiler/testData/codegen/boxInline/reified/kt18977.kt");
}
@TestMetadata("kt35511.kt")
public void testKt35511() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt35511.kt");
}
@TestMetadata("kt35511_try.kt")
public void testKt35511_try() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt35511_try.kt");
}
@TestMetadata("kt7017.kt")
public void testKt7017() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt7017.kt");
@@ -2898,6 +2898,16 @@ public class JsCodegenInlineTestGenerated extends AbstractJsCodegenInlineTest {
runTest("compiler/testData/codegen/boxInline/reified/kt18977.kt");
}
@TestMetadata("kt35511.kt")
public void testKt35511() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt35511.kt");
}
@TestMetadata("kt35511_try.kt")
public void testKt35511_try() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt35511_try.kt");
}
@TestMetadata("kt7017.kt")
public void testKt7017() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt7017.kt");