From cc52832943ee84102d5e3c0dfc0c3add46ef4a4b Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Tue, 31 Aug 2021 02:38:50 +0200 Subject: [PATCH] JVM IR: fix incorrect type of IrCheckNotNull intrinsic It is not correct to assume that arg0 has been generated to have the same IrType as the whole expression. The reason it only backfired in throwing exceptions probably has to do with the fact that visitThrow might be the only place in ExpressionCodegen right now which uses the IrType from PromisedValue to make a decision on whether to generate checkcast. It seems suspicious that ExpressionCodegen.visitFieldAccess/visitCall return PromisedValue whose IrType mentions type parameters which are declared outside of the call site, but that should probably be investigated separately. #KT-48440 Fixed --- .../FirBlackBoxCodegenTestGenerated.java | 12 ++++++++++ .../backend/jvm/intrinsics/IrCheckNotNull.kt | 2 +- .../testData/codegen/box/exclExcl/kt48440.kt | 21 ++++++++++++++++++ .../codegen/box/exclExcl/kt48440_2.kt | 22 +++++++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 12 ++++++++++ .../IrBlackBoxCodegenTestGenerated.java | 12 ++++++++++ .../LightAnalysisModeTestGenerated.java | 10 +++++++++ .../IrJsCodegenBoxES6TestGenerated.java | 10 +++++++++ .../IrJsCodegenBoxTestGenerated.java | 10 +++++++++ .../semantics/JsCodegenBoxTestGenerated.java | 10 +++++++++ .../IrCodegenBoxWasmTestGenerated.java | 10 +++++++++ 11 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/codegen/box/exclExcl/kt48440.kt create mode 100644 compiler/testData/codegen/box/exclExcl/kt48440_2.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 e2b23e97e89..410222ab017 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 @@ -15454,6 +15454,18 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/exclExcl/genericNull.kt"); } + @Test + @TestMetadata("kt48440.kt") + public void testKt48440() throws Exception { + runTest("compiler/testData/codegen/box/exclExcl/kt48440.kt"); + } + + @Test + @TestMetadata("kt48440_2.kt") + public void testKt48440_2() throws Exception { + runTest("compiler/testData/codegen/box/exclExcl/kt48440_2.kt"); + } + @Test @TestMetadata("primitive.kt") public void testPrimitive() throws Exception { diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/intrinsics/IrCheckNotNull.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/intrinsics/IrCheckNotNull.kt index 3ca3f25b515..3f42cdf295b 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/intrinsics/IrCheckNotNull.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/intrinsics/IrCheckNotNull.kt @@ -17,7 +17,7 @@ object IrCheckNotNull : IntrinsicMethod() { override fun invoke(expression: IrFunctionAccessExpression, codegen: ExpressionCodegen, data: BlockInfo): PromisedValue? { val arg0 = expression.getValueArgument(0)!!.accept(codegen, data) if (AsmUtil.isPrimitive(arg0.type)) return arg0 - return object : PromisedValue(codegen, arg0.type, expression.type) { + return object : PromisedValue(codegen, arg0.type, arg0.irType) { override fun materializeAt(target: Type, irTarget: IrType, castForReified: Boolean) = arg0.materialized().also { codegen.checkTopValueForNull() }.materializeAt(target, irTarget, castForReified) diff --git a/compiler/testData/codegen/box/exclExcl/kt48440.kt b/compiler/testData/codegen/box/exclExcl/kt48440.kt new file mode 100644 index 00000000000..758513a6ab0 --- /dev/null +++ b/compiler/testData/codegen/box/exclExcl/kt48440.kt @@ -0,0 +1,21 @@ +// IGNORE_BACKEND: WASM + +fun test(): String { + var exception: Throwable? = null + val f = { + exception = IllegalStateException("OK") + } + f() + + if (exception != null) { + throw exception!! + } else { + return "Fail" + } +} + +fun box(): String = try { + test() +} catch (e: IllegalStateException) { + e.message!! +} diff --git a/compiler/testData/codegen/box/exclExcl/kt48440_2.kt b/compiler/testData/codegen/box/exclExcl/kt48440_2.kt new file mode 100644 index 00000000000..72176014434 --- /dev/null +++ b/compiler/testData/codegen/box/exclExcl/kt48440_2.kt @@ -0,0 +1,22 @@ +// IGNORE_BACKEND: WASM + +fun id(t: T): T = t + +fun test(b: Boolean): String { + var exception: Throwable? = null + if (b) { + exception = IllegalStateException("OK") + } + + if (exception != null) { + throw id(exception)!! + } else { + return "Fail" + } +} + +fun box(): String = try { + test(true) +} catch (e: IllegalStateException) { + e.message!! +} diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java index c555d2b5f1e..0cbddd601fc 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java @@ -15346,6 +15346,18 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/exclExcl/genericNull.kt"); } + @Test + @TestMetadata("kt48440.kt") + public void testKt48440() throws Exception { + runTest("compiler/testData/codegen/box/exclExcl/kt48440.kt"); + } + + @Test + @TestMetadata("kt48440_2.kt") + public void testKt48440_2() throws Exception { + runTest("compiler/testData/codegen/box/exclExcl/kt48440_2.kt"); + } + @Test @TestMetadata("primitive.kt") public void testPrimitive() throws Exception { 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 9557de8f945..45bb45bd62b 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 @@ -15454,6 +15454,18 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/exclExcl/genericNull.kt"); } + @Test + @TestMetadata("kt48440.kt") + public void testKt48440() throws Exception { + runTest("compiler/testData/codegen/box/exclExcl/kt48440.kt"); + } + + @Test + @TestMetadata("kt48440_2.kt") + public void testKt48440_2() throws Exception { + runTest("compiler/testData/codegen/box/exclExcl/kt48440_2.kt"); + } + @Test @TestMetadata("primitive.kt") public void testPrimitive() throws Exception { diff --git a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index ccf9c1f8f23..8620b6f4df1 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -12600,6 +12600,16 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/exclExcl/genericNull.kt"); } + @TestMetadata("kt48440.kt") + public void testKt48440() throws Exception { + runTest("compiler/testData/codegen/box/exclExcl/kt48440.kt"); + } + + @TestMetadata("kt48440_2.kt") + public void testKt48440_2() throws Exception { + runTest("compiler/testData/codegen/box/exclExcl/kt48440_2.kt"); + } + @TestMetadata("primitive.kt") public void testPrimitive() throws Exception { runTest("compiler/testData/codegen/box/exclExcl/primitive.kt"); diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java index b881432fe36..a0b5e278927 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java @@ -11129,6 +11129,16 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes runTest("compiler/testData/codegen/box/exclExcl/genericNull.kt"); } + @TestMetadata("kt48440.kt") + public void testKt48440() throws Exception { + runTest("compiler/testData/codegen/box/exclExcl/kt48440.kt"); + } + + @TestMetadata("kt48440_2.kt") + public void testKt48440_2() throws Exception { + runTest("compiler/testData/codegen/box/exclExcl/kt48440_2.kt"); + } + @TestMetadata("primitive.kt") public void testPrimitive() throws Exception { runTest("compiler/testData/codegen/box/exclExcl/primitive.kt"); diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java index 26dec553100..96e4cd22668 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java @@ -10535,6 +10535,16 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/exclExcl/genericNull.kt"); } + @TestMetadata("kt48440.kt") + public void testKt48440() throws Exception { + runTest("compiler/testData/codegen/box/exclExcl/kt48440.kt"); + } + + @TestMetadata("kt48440_2.kt") + public void testKt48440_2() throws Exception { + runTest("compiler/testData/codegen/box/exclExcl/kt48440_2.kt"); + } + @TestMetadata("primitive.kt") public void testPrimitive() throws Exception { runTest("compiler/testData/codegen/box/exclExcl/primitive.kt"); diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java index 2eda5f27f32..3174d5e70ba 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java @@ -10535,6 +10535,16 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/exclExcl/genericNull.kt"); } + @TestMetadata("kt48440.kt") + public void testKt48440() throws Exception { + runTest("compiler/testData/codegen/box/exclExcl/kt48440.kt"); + } + + @TestMetadata("kt48440_2.kt") + public void testKt48440_2() throws Exception { + runTest("compiler/testData/codegen/box/exclExcl/kt48440_2.kt"); + } + @TestMetadata("primitive.kt") public void testPrimitive() throws Exception { runTest("compiler/testData/codegen/box/exclExcl/primitive.kt"); diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/wasm/semantics/IrCodegenBoxWasmTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/wasm/semantics/IrCodegenBoxWasmTestGenerated.java index 8ed3495e527..63ed3efc7e8 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/wasm/semantics/IrCodegenBoxWasmTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/wasm/semantics/IrCodegenBoxWasmTestGenerated.java @@ -5268,6 +5268,16 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/exclExcl"), Pattern.compile("^([^_](.+))\\.kt$"), null, TargetBackend.WASM, true); } + @TestMetadata("kt48440.kt") + public void testKt48440() throws Exception { + runTest("compiler/testData/codegen/box/exclExcl/kt48440.kt"); + } + + @TestMetadata("kt48440_2.kt") + public void testKt48440_2() throws Exception { + runTest("compiler/testData/codegen/box/exclExcl/kt48440_2.kt"); + } + @TestMetadata("primitive.kt") public void testPrimitive() throws Exception { runTest("compiler/testData/codegen/box/exclExcl/primitive.kt");