From 903a5d69a40f0ec019413fd1e6dc2932751a87ab Mon Sep 17 00:00:00 2001 From: Pavel Mikhailovskii Date: Tue, 27 Sep 2022 15:49:14 +0200 Subject: [PATCH] KT-53146 Don't coerce Nothing? returned from "catch" clauses At first, I tried to modify the logic in PromisedValue.materializeAt to get rid of all casts from Nothing? to other types, but that broke a number of tests containing casts of java.lang.Void to other types, so I decided to fix only this particular case with try...catch (for some reason, here we get a null of type java.lang.Object). --- .../FirBlackBoxCodegenTestGenerated.java | 6 ++++++ .../backend/jvm/codegen/ExpressionCodegen.kt | 4 +++- compiler/testData/codegen/box/casts/kt53146.kt | 18 ++++++++++++++++++ .../IrBlackBoxCodegenTestGenerated.java | 6 ++++++ 4 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/codegen/box/casts/kt53146.kt diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java index 4674a7c9101..ebb3ef5017c 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java @@ -4781,6 +4781,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/casts/kt50577.kt"); } + @Test + @TestMetadata("kt53146.kt") + public void testKt53146() throws Exception { + runTest("compiler/testData/codegen/box/casts/kt53146.kt"); + } + @Test @TestMetadata("kt53677.kt") public void testKt53677() throws Exception { diff --git a/compiler/ir/backend.jvm/codegen/src/org/jetbrains/kotlin/backend/jvm/codegen/ExpressionCodegen.kt b/compiler/ir/backend.jvm/codegen/src/org/jetbrains/kotlin/backend/jvm/codegen/ExpressionCodegen.kt index 061bb85d07f..52ffc12aa51 100644 --- a/compiler/ir/backend.jvm/codegen/src/org/jetbrains/kotlin/backend/jvm/codegen/ExpressionCodegen.kt +++ b/compiler/ir/backend.jvm/codegen/src/org/jetbrains/kotlin/backend/jvm/codegen/ExpressionCodegen.kt @@ -1225,7 +1225,9 @@ class ExpressionCodegen( val catchResult = catchBody.accept(this, catchBlockInfo) if (savedValue != null) { - catchResult.materializeAt(tryAsmType, aTry.type, true) + if (catchResult.irType != context.irBuiltIns.nothingNType) { + catchResult.materializeAt(tryAsmType, aTry.type, true) + } mv.store(savedValue, tryAsmType) } else { catchResult.discard() diff --git a/compiler/testData/codegen/box/casts/kt53146.kt b/compiler/testData/codegen/box/casts/kt53146.kt new file mode 100644 index 00000000000..7c6b066c4fd --- /dev/null +++ b/compiler/testData/codegen/box/casts/kt53146.kt @@ -0,0 +1,18 @@ +// TARGET_BACKEND: JVM_IR +// WITH_STDLIB +// FULL_JDK +// CHECK_BYTECODE_TEXT + +class A + +fun box(): String { + val a = try { + A() + } catch (e: NoClassDefFoundError) { + null + } + + return "OK" +} + +// 0 CHECKCAST \ No newline at end of file diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java index ec0d5643a42..e9dd7d431e8 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java @@ -4781,6 +4781,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/casts/kt50577.kt"); } + @Test + @TestMetadata("kt53146.kt") + public void testKt53146() throws Exception { + runTest("compiler/testData/codegen/box/casts/kt53146.kt"); + } + @Test @TestMetadata("kt53677.kt") public void testKt53677() throws Exception {