From 37666980814ddeb6fbcbcebd05f01aeff6506a83 Mon Sep 17 00:00:00 2001 From: Pavel Mikhailovskii Date: Mon, 20 Jun 2022 18:51:46 +0200 Subject: [PATCH] KT-52743 IR: Fix null checks in Elvis operators --- .../runners/codegen/FirBlackBoxCodegenTestGenerated.java | 6 ++++++ .../backend/jvm/lower/JvmSafeCallChainFoldingLowering.kt | 4 ++-- .../ir.tree/src/org/jetbrains/kotlin/ir/util/IrUtils.kt | 4 ++++ compiler/testData/codegen/box/safeCall/kt52743.kt | 8 ++++++++ .../runners/codegen/BlackBoxCodegenTestGenerated.java | 6 ++++++ .../runners/codegen/IrBlackBoxCodegenTestGenerated.java | 6 ++++++ .../kotlin/codegen/LightAnalysisModeTestGenerated.java | 5 +++++ .../kotlin/js/test/JsCodegenBoxTestGenerated.java | 6 ++++++ .../kotlin/js/test/ir/IrJsCodegenBoxTestGenerated.java | 6 ++++++ .../wasm/semantics/IrCodegenBoxWasmTestGenerated.java | 5 +++++ .../konan/blackboxtest/NativeCodegenBoxTestGenerated.java | 6 ++++++ 11 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 compiler/testData/codegen/box/safeCall/kt52743.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 b2113a4c963..adf83a8a6fd 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 @@ -44747,6 +44747,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/safeCall/kt52580.kt"); } + @Test + @TestMetadata("kt52743.kt") + public void testKt52743() throws Exception { + runTest("compiler/testData/codegen/box/safeCall/kt52743.kt"); + } + @Test @TestMetadata("primitive.kt") public void testPrimitive() throws Exception { diff --git a/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/lower/JvmSafeCallChainFoldingLowering.kt b/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/lower/JvmSafeCallChainFoldingLowering.kt index 9446676d7c8..568d0b15dbf 100644 --- a/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/lower/JvmSafeCallChainFoldingLowering.kt +++ b/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/lower/JvmSafeCallChainFoldingLowering.kt @@ -17,7 +17,7 @@ import org.jetbrains.kotlin.ir.expressions.impl.* import org.jetbrains.kotlin.ir.types.* import org.jetbrains.kotlin.ir.util.dump import org.jetbrains.kotlin.ir.util.hasAnnotation -import org.jetbrains.kotlin.ir.util.isTrivial +import org.jetbrains.kotlin.ir.util.isConstantLike import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid import org.jetbrains.kotlin.load.java.JvmAnnotationNames @@ -121,7 +121,7 @@ class JvmSafeCallChainFoldingLowering(val context: JvmBackendContext) : FileLowe IrConstImpl.boolean(startOffset, endOffset, context.irBuiltIns.booleanType, false) private fun irValNotNull(startOffset: Int, endOffset: Int, irVariable: IrVariable): IrExpression = - if (irVariable.type.isJvmNullable() || irVariable.initializer?.isTrivial() != true) + if (irVariable.type.isJvmNullable() || irVariable.initializer?.isConstantLike != true) IrGetValueImpl(startOffset, endOffset, irVariable.symbol).irEqEqNull().irNot() else irTrue(startOffset, endOffset) diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/IrUtils.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/IrUtils.kt index 8e92a1f8ef6..666d0615c62 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/IrUtils.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/IrUtils.kt @@ -596,6 +596,10 @@ fun IrExpression.isTrivial() = this is IrGetObjectValue || this is IrErrorExpressionImpl +val IrExpression.isConstantLike: Boolean + get() = this is IrConst<*> || this is IrGetSingletonValue + || this is IrGetValue && this.symbol.owner.origin == IrDeclarationOrigin.INSTANCE_RECEIVER + fun IrExpression.shallowCopy(): IrExpression = shallowCopyOrNull() ?: error("Not a copyable expression: ${render()}") diff --git a/compiler/testData/codegen/box/safeCall/kt52743.kt b/compiler/testData/codegen/box/safeCall/kt52743.kt new file mode 100644 index 00000000000..ea6431f0a2c --- /dev/null +++ b/compiler/testData/codegen/box/safeCall/kt52743.kt @@ -0,0 +1,8 @@ +fun nullableFun(): T { + return null as T +} + +fun box(): String { + val t = nullableFun() + return if (t?.length == null) "OK" else "Fail" +} 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 1a26ce0589b..c24dc549dfa 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 @@ -44177,6 +44177,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/safeCall/kt52580.kt"); } + @Test + @TestMetadata("kt52743.kt") + public void testKt52743() throws Exception { + runTest("compiler/testData/codegen/box/safeCall/kt52743.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 ef6530063a8..553ebc1a10f 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 @@ -44747,6 +44747,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/safeCall/kt52580.kt"); } + @Test + @TestMetadata("kt52743.kt") + public void testKt52743() throws Exception { + runTest("compiler/testData/codegen/box/safeCall/kt52743.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 daa85c516aa..0ce64681c08 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -35687,6 +35687,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/safeCall/kt52580.kt"); } + @TestMetadata("kt52743.kt") + public void testKt52743() throws Exception { + runTest("compiler/testData/codegen/box/safeCall/kt52743.kt"); + } + @TestMetadata("primitive.kt") public void testPrimitive() throws Exception { runTest("compiler/testData/codegen/box/safeCall/primitive.kt"); diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/JsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/JsCodegenBoxTestGenerated.java index 5c424f9f670..c44a51d9f04 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/JsCodegenBoxTestGenerated.java @@ -32491,6 +32491,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/safeCall/kt52580.kt"); } + @Test + @TestMetadata("kt52743.kt") + public void testKt52743() throws Exception { + runTest("compiler/testData/codegen/box/safeCall/kt52743.kt"); + } + @Test @TestMetadata("primitive.kt") public void testPrimitive() throws Exception { diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsCodegenBoxTestGenerated.java index b2de8228ab8..f7e96a50332 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsCodegenBoxTestGenerated.java @@ -32593,6 +32593,12 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/safeCall/kt52580.kt"); } + @Test + @TestMetadata("kt52743.kt") + public void testKt52743() throws Exception { + runTest("compiler/testData/codegen/box/safeCall/kt52743.kt"); + } + @Test @TestMetadata("primitive.kt") public void testPrimitive() throws Exception { diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/testOld/wasm/semantics/IrCodegenBoxWasmTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/testOld/wasm/semantics/IrCodegenBoxWasmTestGenerated.java index 9753389341f..efad935e1ac 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/testOld/wasm/semantics/IrCodegenBoxWasmTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/testOld/wasm/semantics/IrCodegenBoxWasmTestGenerated.java @@ -29161,6 +29161,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest runTest("compiler/testData/codegen/box/safeCall/kt52580.kt"); } + @TestMetadata("kt52743.kt") + public void testKt52743() throws Exception { + runTest("compiler/testData/codegen/box/safeCall/kt52743.kt"); + } + @TestMetadata("primitive.kt") public void testPrimitive() throws Exception { runTest("compiler/testData/codegen/box/safeCall/primitive.kt"); diff --git a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeCodegenBoxTestGenerated.java b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeCodegenBoxTestGenerated.java index 031bee482e6..cb438491169 100644 --- a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeCodegenBoxTestGenerated.java +++ b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeCodegenBoxTestGenerated.java @@ -35586,6 +35586,12 @@ public class NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTest runTest("compiler/testData/codegen/box/safeCall/kt52580.kt"); } + @Test + @TestMetadata("kt52743.kt") + public void testKt52743() throws Exception { + runTest("compiler/testData/codegen/box/safeCall/kt52743.kt"); + } + @Test @TestMetadata("primitive.kt") public void testPrimitive() throws Exception {