From b2606735c588504ed359a14dff5271f797933ac0 Mon Sep 17 00:00:00 2001 From: Mikhael Bogdanov Date: Tue, 5 Feb 2019 13:45:35 +0100 Subject: [PATCH] Properly capture local delegated properties in object constructor #KT-23117 Fixed --- .../kotlin/codegen/ImplementationBodyCodegen.java | 12 +++++++++--- .../codegen/box/delegatedProperty/local/kt23117.kt | 14 ++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 5 +++++ .../codegen/LightAnalysisModeTestGenerated.java | 5 +++++ .../codegen/ir/IrBlackBoxCodegenTestGenerated.java | 5 +++++ .../semantics/IrJsCodegenBoxTestGenerated.java | 5 +++++ .../test/semantics/JsCodegenBoxTestGenerated.java | 5 +++++ 7 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 compiler/testData/codegen/box/delegatedProperty/local/kt23117.kt diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ImplementationBodyCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ImplementationBodyCodegen.java index dcb0f662893..0d3c3b030d1 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ImplementationBodyCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ImplementationBodyCodegen.java @@ -67,6 +67,7 @@ import static org.jetbrains.kotlin.codegen.CodegenUtilKt.isGenericToArray; import static org.jetbrains.kotlin.codegen.CodegenUtilKt.isNonGenericToArray; import static org.jetbrains.kotlin.codegen.JvmCodegenUtil.*; import static org.jetbrains.kotlin.codegen.binding.CodegenBinding.enumEntryNeedSubclass; +import static org.jetbrains.kotlin.codegen.binding.CodegenBinding.getDelegatedLocalVariableMetadata; import static org.jetbrains.kotlin.codegen.inline.InlineCodegenUtils2Kt.initDefaultSourceMappingIfNeeded; import static org.jetbrains.kotlin.load.java.JvmAbi.*; import static org.jetbrains.kotlin.resolve.BindingContext.INDEXED_LVALUE_GET; @@ -975,7 +976,12 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen { ClassDescriptor classDescriptor = ((ConstructorDescriptor) containingDeclaration).getConstructedClass(); if (classDescriptor == ImplementationBodyCodegen.this.descriptor) return; } - lookupInContext(descriptor); + if (lookupInContext(descriptor)) { + if (isDelegatedLocalVariable(descriptor)) { + VariableDescriptor metadata = getDelegatedLocalVariableMetadata((VariableDescriptor) descriptor, bindingContext); + lookupInContext(metadata); + } + } } } @@ -998,8 +1004,8 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen { } } - private void lookupInContext(@NotNull DeclarationDescriptor toLookup) { - context.lookupInContext(toLookup, StackValue.LOCAL_0, state, true); + private boolean lookupInContext(@NotNull DeclarationDescriptor toLookup) { + return context.lookupInContext(toLookup, StackValue.LOCAL_0, state, true) != null; } @Override diff --git a/compiler/testData/codegen/box/delegatedProperty/local/kt23117.kt b/compiler/testData/codegen/box/delegatedProperty/local/kt23117.kt new file mode 100644 index 00000000000..1e1485470c7 --- /dev/null +++ b/compiler/testData/codegen/box/delegatedProperty/local/kt23117.kt @@ -0,0 +1,14 @@ +// IGNORE_BACKEND: JS_IR, JVM_IR +//WITH_RUNTIME +import kotlin.properties.Delegates.notNull + +fun box(): String { + var bunny by notNull() + + val obj = object { + val getBunny = { bunny } + } + + bunny = "OK" + return obj.getBunny() +} diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 69a577644e6..a2edfa6527c 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -9250,6 +9250,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/delegatedProperty/local/kt19690.kt"); } + @TestMetadata("kt23117.kt") + public void testKt23117() throws Exception { + runTest("compiler/testData/codegen/box/delegatedProperty/local/kt23117.kt"); + } + @TestMetadata("localVal.kt") public void testLocalVal() throws Exception { runTest("compiler/testData/codegen/box/delegatedProperty/local/localVal.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 7bd2c26dcf4..4f053e7692a 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -9250,6 +9250,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/delegatedProperty/local/kt19690.kt"); } + @TestMetadata("kt23117.kt") + public void testKt23117() throws Exception { + runTest("compiler/testData/codegen/box/delegatedProperty/local/kt23117.kt"); + } + @TestMetadata("localVal.kt") public void testLocalVal() throws Exception { runTest("compiler/testData/codegen/box/delegatedProperty/local/localVal.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index 4dd46066cc9..5483381da1e 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -9250,6 +9250,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/delegatedProperty/local/kt19690.kt"); } + @TestMetadata("kt23117.kt") + public void testKt23117() throws Exception { + runTest("compiler/testData/codegen/box/delegatedProperty/local/kt23117.kt"); + } + @TestMetadata("localVal.kt") public void testLocalVal() throws Exception { runTest("compiler/testData/codegen/box/delegatedProperty/local/localVal.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrJsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrJsCodegenBoxTestGenerated.java index cdbfa50f0d5..b0c88167df8 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrJsCodegenBoxTestGenerated.java @@ -7135,6 +7135,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/delegatedProperty/local/kt19690.kt"); } + @TestMetadata("kt23117.kt") + public void testKt23117() throws Exception { + runTest("compiler/testData/codegen/box/delegatedProperty/local/kt23117.kt"); + } + @TestMetadata("localVal.kt") public void testLocalVal() throws Exception { runTest("compiler/testData/codegen/box/delegatedProperty/local/localVal.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 abc72551f43..45f74ca98c1 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 @@ -8225,6 +8225,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/delegatedProperty/local/kt19690.kt"); } + @TestMetadata("kt23117.kt") + public void testKt23117() throws Exception { + runTest("compiler/testData/codegen/box/delegatedProperty/local/kt23117.kt"); + } + @TestMetadata("localVal.kt") public void testLocalVal() throws Exception { runTest("compiler/testData/codegen/box/delegatedProperty/local/localVal.kt");