Keep original casts during reification to avoid VerifyError

#KT-26435 Fixed
This commit is contained in:
Mikhael Bogdanov
2019-12-17 09:29:11 +01:00
parent 6f16d02920
commit 03c2350e79
11 changed files with 190 additions and 1 deletions
@@ -222,7 +222,11 @@ class ReifiedTypeInliner<KT : KotlinTypeMarker>(
generateAsCast(InstructionAdapter(newMethodNode), kotlinType, asmType, safe, languageVersionSettings)
instructions.insert(insn, newMethodNode.instructions)
instructions.remove(stubCheckcast)
// Keep stubCheckcast to avoid VerifyErrors on 1.8+ bytecode,
// it's safe to remove cast to Object as FrameMap will use it as default value for merged branches
if (stubCheckcast.desc == AsmTypes.OBJECT_TYPE.internalName) {
instructions.remove(stubCheckcast)
}
// TODO: refine max stack calculation (it's not always as big as +4)
maxStackSize = max(maxStackSize, 4)
@@ -0,0 +1,37 @@
// FILE: 1.kt
// WITH_RUNTIME
package test
enum class Id {
OK,
FAIL
}
sealed class Base(val id: Id)
class A(id: Id) : Base(id)
class B(id: Id) : Base(id)
inline fun <reified T : Base> process(t: T, f: (T) -> Unit): Base? {
f(t)
return getSomeBaseObject(t.id) as? T ?: throw RuntimeException()
}
fun getSomeBaseObject(id: Id): Base = if (id == Id.OK) A(id) else B(id)
// FILE: 2.kt
import test.*
fun doSth(base: Base): Base? =
if (base is A) process(base, f = ::doSomethingInCaseOfA)
else if (base is B) process(base, f = ::doSomethingInCaseOfB) else error("123")
fun doSomethingInCaseOfA(a: A) {}
fun doSomethingInCaseOfB(b: B) {}
fun box(): String {
val a = doSth(A(Id.OK))!!
return a.id.name
}
@@ -0,0 +1,34 @@
// 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: Base) {
result = base.name
}
fun cond() = true
inline fun <reified T : Base, reified Y : Base> process(a: Base) {
val z = if (cond())
a as T
else
a as Y
foo(z)
}
// FILE: 2.kt
import test.*
fun box(): String {
process<A, B>(A("OK"))
return result
}
@@ -0,0 +1,32 @@
// 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: Base) {
result = base.name
}
fun cond() = true
inline fun <reified T: Base, reified Y : Base> process(a: Base): Base {
val z = if (cond())
a as T
else
a as Y
return z
}
// FILE: 2.kt
import test.*
fun box(): String {
return process<A, B>(A("OK")).name
}
@@ -0,0 +1,12 @@
// KOTLIN_CONFIGURATION_FLAGS: +JVM.DISABLE_OPTIMIZATION
inline fun <reified T> foo(s: Any) {
s as T
}
fun main() {
foo<String>("123")
}
// only one checkcast in reified function
// 1 CHECKCAST java/lang/Object
@@ -2874,6 +2874,21 @@ public class BlackBoxInlineCodegenTestGenerated extends AbstractBlackBoxInlineCo
runTest("compiler/testData/codegen/boxInline/reified/checkCast/chain.kt");
}
@TestMetadata("kt26435.kt")
public void testKt26435() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/checkCast/kt26435.kt");
}
@TestMetadata("kt26435_2.kt")
public void testKt26435_2() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/checkCast/kt26435_2.kt");
}
@TestMetadata("kt26435_3.kt")
public void testKt26435_3() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/checkCast/kt26435_3.kt");
}
@TestMetadata("kt8043.kt")
public void testKt8043() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/checkCast/kt8043.kt");
@@ -1646,6 +1646,11 @@ public class BytecodeTextTestGenerated extends AbstractBytecodeTextTest {
runTest("compiler/testData/codegen/bytecodeText/disabledOptimizations/noJumpInSingleBranch.kt");
}
@TestMetadata("noObjectCastAfterReification.kt")
public void testNoObjectCastAfterReification() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/disabledOptimizations/noObjectCastAfterReification.kt");
}
@TestMetadata("noUnitInstanceInDefaultParameterInitialization.kt")
public void testNoUnitInstanceInDefaultParameterInitialization() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/disabledOptimizations/noUnitInstanceInDefaultParameterInitialization.kt");
@@ -2874,6 +2874,21 @@ public class CompileKotlinAgainstInlineKotlinTestGenerated extends AbstractCompi
runTest("compiler/testData/codegen/boxInline/reified/checkCast/chain.kt");
}
@TestMetadata("kt26435.kt")
public void testKt26435() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/checkCast/kt26435.kt");
}
@TestMetadata("kt26435_2.kt")
public void testKt26435_2() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/checkCast/kt26435_2.kt");
}
@TestMetadata("kt26435_3.kt")
public void testKt26435_3() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/checkCast/kt26435_3.kt");
}
@TestMetadata("kt8043.kt")
public void testKt8043() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/checkCast/kt8043.kt");
@@ -2874,6 +2874,21 @@ public class IrBlackBoxInlineCodegenTestGenerated extends AbstractIrBlackBoxInli
runTest("compiler/testData/codegen/boxInline/reified/checkCast/chain.kt");
}
@TestMetadata("kt26435.kt")
public void testKt26435() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/checkCast/kt26435.kt");
}
@TestMetadata("kt26435_2.kt")
public void testKt26435_2() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/checkCast/kt26435_2.kt");
}
@TestMetadata("kt26435_3.kt")
public void testKt26435_3() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/checkCast/kt26435_3.kt");
}
@TestMetadata("kt8043.kt")
public void testKt8043() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/checkCast/kt8043.kt");
@@ -1601,6 +1601,11 @@ public class IrBytecodeTextTestGenerated extends AbstractIrBytecodeTextTest {
runTest("compiler/testData/codegen/bytecodeText/disabledOptimizations/noJumpInSingleBranch.kt");
}
@TestMetadata("noObjectCastAfterReification.kt")
public void testNoObjectCastAfterReification() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/disabledOptimizations/noObjectCastAfterReification.kt");
}
@TestMetadata("noUnitInstanceInDefaultParameterInitialization.kt")
public void testNoUnitInstanceInDefaultParameterInitialization() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/disabledOptimizations/noUnitInstanceInDefaultParameterInitialization.kt");
@@ -2874,6 +2874,21 @@ public class IrCompileKotlinAgainstInlineKotlinTestGenerated extends AbstractIrC
runTest("compiler/testData/codegen/boxInline/reified/checkCast/chain.kt");
}
@TestMetadata("kt26435.kt")
public void testKt26435() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/checkCast/kt26435.kt");
}
@TestMetadata("kt26435_2.kt")
public void testKt26435_2() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/checkCast/kt26435_2.kt");
}
@TestMetadata("kt26435_3.kt")
public void testKt26435_3() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/checkCast/kt26435_3.kt");
}
@TestMetadata("kt8043.kt")
public void testKt8043() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/checkCast/kt8043.kt");