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 aabe8979fdb..715412eba47 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 @@ -1638,6 +1638,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/boxingOptimization/kt19767_chain.kt"); } + @Test + @TestMetadata("kt46859.kt") + public void testKt46859() throws Exception { + runTest("compiler/testData/codegen/box/boxingOptimization/kt46859.kt"); + } + @Test @TestMetadata("kt5493.kt") public void testKt5493() throws Exception { diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/optimizations/FoldConstantLowering.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/optimizations/FoldConstantLowering.kt index d987d48727b..9070c20f622 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/optimizations/FoldConstantLowering.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/optimizations/FoldConstantLowering.kt @@ -130,6 +130,7 @@ class FoldConstantLowering( operationName == "toString" -> constToString(operand) // Disable toFloat folding on K/JS till `toFloat` is fixed (KT-35422) operationName == "toFloat" && floatSpecial -> return call + operand.kind == IrConstKind.Null -> return call else -> evaluateUnary( operationName, operand.kind.toString(), @@ -161,6 +162,8 @@ class FoldConstantLowering( val lhs = coerceToDouble(call.dispatchReceiver as? IrConst<*> ?: return call) val rhs = coerceToDouble(call.getValueArgument(0) as? IrConst<*> ?: return call) + if (lhs.kind == IrConstKind.Null || rhs.kind == IrConstKind.Null) return call + val evaluated = try { evaluateBinary( call.symbol.owner.name.toString(), @@ -189,6 +192,8 @@ class FoldConstantLowering( val lhs = call.getValueArgument(0) as? IrConst<*> ?: return call val rhs = call.getValueArgument(1) as? IrConst<*> ?: return call + if (lhs.kind == IrConstKind.Null || rhs.kind == IrConstKind.Null) return call + val evaluated = try { val evaluator = BINARY_OP_TO_EVALUATOR[BinaryOp(lhs.kind.toString(), rhs.kind.toString(), call.symbol.owner.name.toString())] ?: return call diff --git a/compiler/testData/codegen/box/boxingOptimization/kt46859.kt b/compiler/testData/codegen/box/boxingOptimization/kt46859.kt new file mode 100644 index 00000000000..c2090395658 --- /dev/null +++ b/compiler/testData/codegen/box/boxingOptimization/kt46859.kt @@ -0,0 +1,33 @@ +fun box(): String { + return "OK" +} + +fun foo() { + foldingUnary(null) + + foldingBinary(null, null) + + foldingBuiltinBinary(null, null) +} + +class Foo { + fun foo() {} + fun foo(foo: Foo?) {} +} + +inline fun foldingUnary(foo: Foo?) { + foo!! + foo.foo() +} + +inline fun foldingBinary(foo1: Foo?, foo2: Foo?) { + foo1!! + foo2!! + foo1.foo(foo2) +} + +inline fun foldingBuiltinBinary(int1: Int?, int2: Int?) { + int1!! + int2!! + int1 < int2 +} \ No newline at end of file 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 d1788fd4488..3ec55ff8213 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 @@ -1638,6 +1638,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/boxingOptimization/kt19767_chain.kt"); } + @Test + @TestMetadata("kt46859.kt") + public void testKt46859() throws Exception { + runTest("compiler/testData/codegen/box/boxingOptimization/kt46859.kt"); + } + @Test @TestMetadata("kt5493.kt") public void testKt5493() 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 a11c9f8ca38..f6c1f1b61b7 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 @@ -1638,6 +1638,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/boxingOptimization/kt19767_chain.kt"); } + @Test + @TestMetadata("kt46859.kt") + public void testKt46859() throws Exception { + runTest("compiler/testData/codegen/box/boxingOptimization/kt46859.kt"); + } + @Test @TestMetadata("kt5493.kt") public void testKt5493() 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 73bca3d67b6..29982920137 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -1444,6 +1444,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/boxingOptimization/kt19767_chain.kt"); } + @TestMetadata("kt46859.kt") + public void testKt46859() throws Exception { + runTest("compiler/testData/codegen/box/boxingOptimization/kt46859.kt"); + } + @TestMetadata("kt5493.kt") public void testKt5493() throws Exception { runTest("compiler/testData/codegen/box/boxingOptimization/kt5493.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 042b6788701..1309f1c445c 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 @@ -939,6 +939,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes runTest("compiler/testData/codegen/box/boxingOptimization/kt19767_chain.kt"); } + @TestMetadata("kt46859.kt") + public void testKt46859() throws Exception { + runTest("compiler/testData/codegen/box/boxingOptimization/kt46859.kt"); + } + @TestMetadata("kt5493.kt") public void testKt5493() throws Exception { runTest("compiler/testData/codegen/box/boxingOptimization/kt5493.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 5e410811623..bc59c665378 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 @@ -939,6 +939,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/boxingOptimization/kt19767_chain.kt"); } + @TestMetadata("kt46859.kt") + public void testKt46859() throws Exception { + runTest("compiler/testData/codegen/box/boxingOptimization/kt46859.kt"); + } + @TestMetadata("kt5493.kt") public void testKt5493() throws Exception { runTest("compiler/testData/codegen/box/boxingOptimization/kt5493.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 a3f7741f532..a2bba36d781 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 @@ -939,6 +939,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/boxingOptimization/kt19767_chain.kt"); } + @TestMetadata("kt46859.kt") + public void testKt46859() throws Exception { + runTest("compiler/testData/codegen/box/boxingOptimization/kt46859.kt"); + } + @TestMetadata("kt5493.kt") public void testKt5493() throws Exception { runTest("compiler/testData/codegen/box/boxingOptimization/kt5493.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 072e43ee881..784d99d6cf9 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 @@ -824,6 +824,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest runTest("compiler/testData/codegen/box/boxingOptimization/kt19767_chain.kt"); } + @TestMetadata("kt46859.kt") + public void testKt46859() throws Exception { + runTest("compiler/testData/codegen/box/boxingOptimization/kt46859.kt"); + } + @TestMetadata("kt5588.kt") public void testKt5588() throws Exception { runTest("compiler/testData/codegen/box/boxingOptimization/kt5588.kt");