diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/ReifiedTypeInliner.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/ReifiedTypeInliner.kt index bb24141e1cd..10e2f8cb4ee 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/ReifiedTypeInliner.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/ReifiedTypeInliner.kt @@ -222,7 +222,11 @@ class ReifiedTypeInliner( 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) diff --git a/compiler/testData/codegen/boxInline/reified/checkCast/kt26435.kt b/compiler/testData/codegen/boxInline/reified/checkCast/kt26435.kt new file mode 100644 index 00000000000..fa41a95c1db --- /dev/null +++ b/compiler/testData/codegen/boxInline/reified/checkCast/kt26435.kt @@ -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 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 +} \ No newline at end of file diff --git a/compiler/testData/codegen/boxInline/reified/checkCast/kt26435_2.kt b/compiler/testData/codegen/boxInline/reified/checkCast/kt26435_2.kt new file mode 100644 index 00000000000..6d8e446a808 --- /dev/null +++ b/compiler/testData/codegen/boxInline/reified/checkCast/kt26435_2.kt @@ -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 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("OK")) + + return result +} \ No newline at end of file diff --git a/compiler/testData/codegen/boxInline/reified/checkCast/kt26435_3.kt b/compiler/testData/codegen/boxInline/reified/checkCast/kt26435_3.kt new file mode 100644 index 00000000000..13ba4ed9a16 --- /dev/null +++ b/compiler/testData/codegen/boxInline/reified/checkCast/kt26435_3.kt @@ -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 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("OK")).name +} \ No newline at end of file diff --git a/compiler/testData/codegen/bytecodeText/disabledOptimizations/noObjectCastAfterReification.kt b/compiler/testData/codegen/bytecodeText/disabledOptimizations/noObjectCastAfterReification.kt new file mode 100644 index 00000000000..952c42049df --- /dev/null +++ b/compiler/testData/codegen/bytecodeText/disabledOptimizations/noObjectCastAfterReification.kt @@ -0,0 +1,12 @@ +// KOTLIN_CONFIGURATION_FLAGS: +JVM.DISABLE_OPTIMIZATION + +inline fun foo(s: Any) { + s as T +} + +fun main() { + foo("123") +} + +// only one checkcast in reified function +// 1 CHECKCAST java/lang/Object \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java index 74e10c0c3b6..af79ed45984 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java @@ -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"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java index 392f5e79bc9..915d9040c18 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java @@ -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"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java index 641a0f8fc1a..9b11e9c1794 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java @@ -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"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxInlineCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxInlineCodegenTestGenerated.java index 9f5d63bbad9..13c55c5e146 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxInlineCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxInlineCodegenTestGenerated.java @@ -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"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBytecodeTextTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBytecodeTextTestGenerated.java index 4dc334550e3..afebce7f531 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBytecodeTextTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBytecodeTextTestGenerated.java @@ -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"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrCompileKotlinAgainstInlineKotlinTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrCompileKotlinAgainstInlineKotlinTestGenerated.java index b6d45676af8..164f9c0e2d7 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrCompileKotlinAgainstInlineKotlinTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrCompileKotlinAgainstInlineKotlinTestGenerated.java @@ -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");