From f4dd75e7695b18e3a374124f9692122d7836e61a Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Mon, 23 Aug 2021 21:00:55 +0300 Subject: [PATCH] JVM_IR don't inline temporary val for 'when' subject --- .../kotlin/backend/jvm/lower/JvmOptimizationLowering.kt | 8 ++++++++ compiler/testData/debug/stepping/whenConstant.kt | 7 +------ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/JvmOptimizationLowering.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/JvmOptimizationLowering.kt index dd2dc72c628..837168de29b 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/JvmOptimizationLowering.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/JvmOptimizationLowering.kt @@ -393,6 +393,14 @@ class JvmOptimizationLowering(val context: JvmBackendContext) : FileLoweringPass dontTouchTemporaryVals.add(safeCall.tmpVal) } + if (expression.origin == IrStatementOrigin.WHEN) { + // Don't optimize out 'when' subject - otherwise we might get somewhat weird debugging behavior. + val subject = expression.statements.firstOrNull() + if (subject is IrVariable) { + dontTouchTemporaryVals.add(subject) + } + } + expression.transformChildren(this, data) removeUnnecessaryTemporaryVariables(expression.statements) return expression diff --git a/compiler/testData/debug/stepping/whenConstant.kt b/compiler/testData/debug/stepping/whenConstant.kt index f21b7db062e..a849cd6b5f8 100644 --- a/compiler/testData/debug/stepping/whenConstant.kt +++ b/compiler/testData/debug/stepping/whenConstant.kt @@ -12,14 +12,9 @@ fun box() { } // JVM_IR and JVM backends have different heuristics for when to use a switch. -// JVM_IR does not use a switch in this case and therefore steps to the evaluation -// of the condition for each of the cases. +// JVM_IR inlines constant value subject and folds unreachable branches. // LINENUMBERS // test.kt:4 box -// LINENUMBERS JVM_IR -// test.kt:5 box -// test.kt:7 box -// LINENUMBERS // test.kt:10 box // test.kt:12 box \ No newline at end of file