From 786ac46fa6459586ada4ffd7535c90c8dbb3f243 Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Mon, 19 Jun 2017 17:28:44 +0300 Subject: [PATCH] Do not store nulls for captured variables going out of scope These values can't be read after going out of scope. JVM implementation can take care of such object references on its own. Ref objects for captured variables are not different from any other objects stored in local variables, so there's really no reason to nullify these references explicitly. #KT-18478 Fixed Target versions 1.1.4 --- .../kotlin/codegen/ExpressionCodegen.java | 4 ---- .../bytecodeText/doNotStoreNullsForCapturedVars.kt | 14 ++++++++++++++ .../kotlin/codegen/BytecodeTextTestGenerated.java | 6 ++++++ 3 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 compiler/testData/codegen/bytecodeText/doNotStoreNullsForCapturedVars.kt diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java index fd60708a39e..d31f303077e 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java @@ -1296,10 +1296,6 @@ public class ExpressionCodegen extends KtVisitor impleme int index = myFrameMap.leave(variableDescriptor); - if (isSharedVarType(type)) { - v.aconst(null); - v.store(index, OBJECT_TYPE); - } v.visitLocalVariable(variableDescriptor.getName().asString(), type.getDescriptor(), null, scopeStart, blockEnd, index); return null; }); diff --git a/compiler/testData/codegen/bytecodeText/doNotStoreNullsForCapturedVars.kt b/compiler/testData/codegen/bytecodeText/doNotStoreNullsForCapturedVars.kt new file mode 100644 index 00000000000..88fc147ab04 --- /dev/null +++ b/compiler/testData/codegen/bytecodeText/doNotStoreNullsForCapturedVars.kt @@ -0,0 +1,14 @@ +fun runNoInline(block: ()-> Unit): Unit { + block() +} + +fun use(x: Int) {} + +fun test(): Unit { + var x = 0 + runNoInline { + use(x) + } +} + +// 0 ACONST_NULL \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java index 79b503dd224..00397e98cc5 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java @@ -132,6 +132,12 @@ public class BytecodeTextTestGenerated extends AbstractBytecodeTextTest { doTest(fileName); } + @TestMetadata("doNotStoreNullsForCapturedVars.kt") + public void testDoNotStoreNullsForCapturedVars() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/doNotStoreNullsForCapturedVars.kt"); + doTest(fileName); + } + @TestMetadata("falseSmartCast.kt") public void testFalseSmartCast() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/falseSmartCast.kt");