From b9409523b01f44a4ed665785240fbf6c87b5e94f Mon Sep 17 00:00:00 2001 From: Mikhael Bogdanov Date: Tue, 25 Apr 2017 14:46:25 +0200 Subject: [PATCH] Fix for KT-17588: Compiler error while optimizer tries to get rid of captured variable #KT-17588 Fixed --- ...pturedVarsOptimizationMethodTransformer.kt | 2 +- .../capturedVarsOptimization/kt17588.kt | 30 +++++++++++++++++++ .../ir/IrBlackBoxCodegenTestGenerated.java | 6 ++++ .../codegen/BlackBoxCodegenTestGenerated.java | 6 ++++ .../LightAnalysisModeTestGenerated.java | 6 ++++ .../semantics/JsCodegenBoxTestGenerated.java | 6 ++++ 6 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/codegen/box/closures/capturedVarsOptimization/kt17588.kt diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/optimization/captured/CapturedVarsOptimizationMethodTransformer.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/optimization/captured/CapturedVarsOptimizationMethodTransformer.kt index 6525870e03d..9988ea626d5 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/optimization/captured/CapturedVarsOptimizationMethodTransformer.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/optimization/captured/CapturedVarsOptimizationMethodTransformer.kt @@ -230,7 +230,7 @@ class CapturedVarsOptimizationMethodTransformer : MethodTransformer() { private fun findCleanInstructions(refValue: CapturedVarDescriptor, oldVarIndex: Int, instructions: InsnList): List { val cleanInstructions = InsnSequence(instructions).filterIsInstance().filter { - it.`var` == oldVarIndex + it.opcode == Opcodes.ASTORE && it.`var` == oldVarIndex }.filter { it.previous?.opcode == Opcodes.ACONST_NULL }.filter { diff --git a/compiler/testData/codegen/box/closures/capturedVarsOptimization/kt17588.kt b/compiler/testData/codegen/box/closures/capturedVarsOptimization/kt17588.kt new file mode 100644 index 00000000000..44a84014aa5 --- /dev/null +++ b/compiler/testData/codegen/box/closures/capturedVarsOptimization/kt17588.kt @@ -0,0 +1,30 @@ +//WITH_RUNTIME +class Test { + + data class Style( + val color: Int? = null, + val underlined: Boolean? = null, + val separator: String = "" + ) + + init { + var flag: Boolean? = null + + val receiver: String = "123" + try { + receiver.let { a2 -> + flag = false + } + } finally { + receiver.hashCode() + } + val style = Style(null, flag, "123") + } +} + + +fun box(): String { + Test() + + return "OK" +} \ No newline at end of file diff --git a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index 16abdbfbecd..8e13a38d2d1 100644 --- a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -3865,6 +3865,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes doTest(fileName); } + @TestMetadata("kt17588.kt") + public void testKt17588() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/closures/capturedVarsOptimization/kt17588.kt"); + doTest(fileName); + } + @TestMetadata("sharedSlotsWithCapturedVars.kt") public void testSharedSlotsWithCapturedVars() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/closures/capturedVarsOptimization/sharedSlotsWithCapturedVars.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index f0d32b74e1b..90280c4b93b 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -3865,6 +3865,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { doTest(fileName); } + @TestMetadata("kt17588.kt") + public void testKt17588() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/closures/capturedVarsOptimization/kt17588.kt"); + doTest(fileName); + } + @TestMetadata("sharedSlotsWithCapturedVars.kt") public void testSharedSlotsWithCapturedVars() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/closures/capturedVarsOptimization/sharedSlotsWithCapturedVars.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 0dcee1995eb..5fedb7037f3 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -3865,6 +3865,12 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes doTest(fileName); } + @TestMetadata("kt17588.kt") + public void testKt17588() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/closures/capturedVarsOptimization/kt17588.kt"); + doTest(fileName); + } + @TestMetadata("sharedSlotsWithCapturedVars.kt") public void testSharedSlotsWithCapturedVars() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/closures/capturedVarsOptimization/sharedSlotsWithCapturedVars.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java index 2009f25c997..57994e5c995 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java @@ -4538,6 +4538,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { doTest(fileName); } + @TestMetadata("kt17588.kt") + public void testKt17588() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/closures/capturedVarsOptimization/kt17588.kt"); + doTest(fileName); + } + @TestMetadata("sharedSlotsWithCapturedVars.kt") public void testSharedSlotsWithCapturedVars() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/closures/capturedVarsOptimization/sharedSlotsWithCapturedVars.kt");