diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/StackValue.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/StackValue.java index bc8005b5319..e2413292673 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/StackValue.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/StackValue.java @@ -810,7 +810,9 @@ public abstract class StackValue { v.aconst(value); } - coerceTo(type, v); + if (value != null || AsmUtil.isPrimitive(type)) { + coerceTo(type, v); + } } } diff --git a/compiler/testData/codegen/bytecodeText/checkcast/kt15411.kt b/compiler/testData/codegen/bytecodeText/checkcast/kt15411.kt new file mode 100644 index 00000000000..80e17b0c1a0 --- /dev/null +++ b/compiler/testData/codegen/bytecodeText/checkcast/kt15411.kt @@ -0,0 +1,15 @@ +// KT-15411 Unnecessary CHECKCAST bytecode when dealing with null + +fun test1(): String? { + return null +} + +fun test2(): BooleanArray? { + return null +} + +fun test3(): Unit? { + return null +} + +// 0 CHECKCAST diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java index e9344bd067a..fefc2dd9855 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java @@ -612,6 +612,21 @@ public class BytecodeTextTestGenerated extends AbstractBytecodeTextTest { } } + @TestMetadata("compiler/testData/codegen/bytecodeText/checkcast") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Checkcast extends AbstractBytecodeTextTest { + public void testAllFilesPresentInCheckcast() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/bytecodeText/checkcast"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); + } + + @TestMetadata("kt15411.kt") + public void testKt15411() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/checkcast/kt15411.kt"); + doTest(fileName); + } + } + @TestMetadata("compiler/testData/codegen/bytecodeText/coercionToUnitOptimization") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)