From 9ab60622955776f1b0a69d2d66f3230c7c68b05a Mon Sep 17 00:00:00 2001 From: Mikhael Bogdanov Date: Tue, 5 Feb 2019 11:37:23 +0100 Subject: [PATCH] Properly capture extension receiver for array convention expressions in object constructor #KT-19389 Fixed --- .../codegen/ImplementationBodyCodegen.java | 18 +++++++++++++ .../testData/codegen/box/closures/kt19389.kt | 21 +++++++++++++++ .../codegen/box/closures/kt19389_set.kt | 27 +++++++++++++++++++ .../boxInline/anonymousObject/kt19389.kt | 24 +++++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 10 +++++++ .../BlackBoxInlineCodegenTestGenerated.java | 5 ++++ ...otlinAgainstInlineKotlinTestGenerated.java | 5 ++++ .../LightAnalysisModeTestGenerated.java | 10 +++++++ .../ir/IrBlackBoxCodegenTestGenerated.java | 10 +++++++ .../IrBlackBoxInlineCodegenTestGenerated.java | 5 ++++ .../IrJsCodegenBoxTestGenerated.java | 10 +++++++ .../semantics/JsCodegenBoxTestGenerated.java | 10 +++++++ 12 files changed, 155 insertions(+) create mode 100644 compiler/testData/codegen/box/closures/kt19389.kt create mode 100644 compiler/testData/codegen/box/closures/kt19389_set.kt create mode 100644 compiler/testData/codegen/boxInline/anonymousObject/kt19389.kt diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ImplementationBodyCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ImplementationBodyCodegen.java index e635d9fda90..dcb0f662893 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ImplementationBodyCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ImplementationBodyCodegen.java @@ -69,6 +69,8 @@ import static org.jetbrains.kotlin.codegen.JvmCodegenUtil.*; import static org.jetbrains.kotlin.codegen.binding.CodegenBinding.enumEntryNeedSubclass; 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; +import static org.jetbrains.kotlin.resolve.BindingContext.INDEXED_LVALUE_SET; import static org.jetbrains.kotlin.resolve.BindingContextUtils.getNotNull; import static org.jetbrains.kotlin.resolve.DescriptorToSourceUtils.descriptorToDeclaration; import static org.jetbrains.kotlin.resolve.DescriptorUtils.*; @@ -1021,6 +1023,22 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen { public void visitSuperExpression(@NotNull KtSuperExpression expression) { lookupInContext(ExpressionCodegen.getSuperCallLabelTarget(context, expression)); } + + @Override + public void visitArrayAccessExpression(@NotNull KtArrayAccessExpression expression) { + ResolvedCall resolvedGetCall = bindingContext.get(INDEXED_LVALUE_GET, expression); + if (resolvedGetCall != null) { + ReceiverValue receiver = resolvedGetCall.getDispatchReceiver(); + lookupReceiver(receiver); + } + + ResolvedCall resolvedSetCall = bindingContext.get(INDEXED_LVALUE_SET, expression); + if (resolvedSetCall != null) { + ReceiverValue receiver = resolvedSetCall.getDispatchReceiver(); + lookupReceiver(receiver); + } + super.visitArrayAccessExpression(expression); + } }; for (KtDeclaration declaration : myClass.getDeclarations()) { diff --git a/compiler/testData/codegen/box/closures/kt19389.kt b/compiler/testData/codegen/box/closures/kt19389.kt new file mode 100644 index 00000000000..7b8a1fc687b --- /dev/null +++ b/compiler/testData/codegen/box/closures/kt19389.kt @@ -0,0 +1,21 @@ +public fun myWith(receiver: T, block: T.() -> R): R { + return receiver.block() +} + +object Foo2 { + operator fun Any?.get(key: String) = "OK" +} + +object Main { + fun bar() = myWith(Foo2) { + + val x = object { + val y = 38["Hello!"] + } + x.y + } +} + +fun box(): String { + return Main.bar() +} \ No newline at end of file diff --git a/compiler/testData/codegen/box/closures/kt19389_set.kt b/compiler/testData/codegen/box/closures/kt19389_set.kt new file mode 100644 index 00000000000..104d35ef972 --- /dev/null +++ b/compiler/testData/codegen/box/closures/kt19389_set.kt @@ -0,0 +1,27 @@ +var result = "fail" + +public fun myWith(receiver: T, block: T.() -> R): R { + return receiver.block() +} + +object Foo2 { + operator fun Any?.get(key: String) = "OK" + operator fun Any?.set(key: String, value: String) { + result = value + } +} + +object Main { + fun bar() = myWith(Foo2) { + val x = object { + init { + 38["Hello!"] = "OK" + } + } + result + } +} + +fun box(): String { + return Main.bar() +} \ No newline at end of file diff --git a/compiler/testData/codegen/boxInline/anonymousObject/kt19389.kt b/compiler/testData/codegen/boxInline/anonymousObject/kt19389.kt new file mode 100644 index 00000000000..17cd0a8fc78 --- /dev/null +++ b/compiler/testData/codegen/boxInline/anonymousObject/kt19389.kt @@ -0,0 +1,24 @@ +// FILE: 1.kt +public inline fun myWith(receiver: T, block: T.() -> R): R { + return receiver.block() +} + +// FILE: 2.kt +//NO_CHECK_LAMBDA_INLINING +object Foo2 { + operator fun Any?.get(key: String) = "OK" +} + +object Main { + fun bar() = myWith(Foo2) { + + val x = object { + val y = 38["Hello!"] + } + x.y + } +} + +fun box(): String { + return Main.bar() +} \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 74b15a2dfa2..69a577644e6 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -3736,6 +3736,16 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/closures/kt11634_4.kt"); } + @TestMetadata("kt19389.kt") + public void testKt19389() throws Exception { + runTest("compiler/testData/codegen/box/closures/kt19389.kt"); + } + + @TestMetadata("kt19389_set.kt") + public void testKt19389_set() throws Exception { + runTest("compiler/testData/codegen/box/closures/kt19389_set.kt"); + } + @TestMetadata("kt2151.kt") public void testKt2151() throws Exception { runTest("compiler/testData/codegen/box/closures/kt2151.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java index 76fbb64d72e..47d63535b50 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java @@ -186,6 +186,11 @@ public class BlackBoxInlineCodegenTestGenerated extends AbstractBlackBoxInlineCo runTest("compiler/testData/codegen/boxInline/anonymousObject/kt17972_super3.kt"); } + @TestMetadata("kt19389.kt") + public void testKt19389() throws Exception { + runTest("compiler/testData/codegen/boxInline/anonymousObject/kt19389.kt"); + } + @TestMetadata("kt19399.kt") public void testKt19399() throws Exception { runTest("compiler/testData/codegen/boxInline/anonymousObject/kt19399.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java index a40bb1dfb5e..5b19fbc1a43 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java @@ -186,6 +186,11 @@ public class CompileKotlinAgainstInlineKotlinTestGenerated extends AbstractCompi runTest("compiler/testData/codegen/boxInline/anonymousObject/kt17972_super3.kt"); } + @TestMetadata("kt19389.kt") + public void testKt19389() throws Exception { + runTest("compiler/testData/codegen/boxInline/anonymousObject/kt19389.kt"); + } + @TestMetadata("kt19399.kt") public void testKt19399() throws Exception { runTest("compiler/testData/codegen/boxInline/anonymousObject/kt19399.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index c3100cc8547..7bd2c26dcf4 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -3736,6 +3736,16 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/closures/kt11634_4.kt"); } + @TestMetadata("kt19389.kt") + public void testKt19389() throws Exception { + runTest("compiler/testData/codegen/box/closures/kt19389.kt"); + } + + @TestMetadata("kt19389_set.kt") + public void testKt19389_set() throws Exception { + runTest("compiler/testData/codegen/box/closures/kt19389_set.kt"); + } + @TestMetadata("kt2151.kt") public void testKt2151() throws Exception { runTest("compiler/testData/codegen/box/closures/kt2151.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index 4264a245e56..4dd46066cc9 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -3736,6 +3736,16 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/closures/kt11634_4.kt"); } + @TestMetadata("kt19389.kt") + public void testKt19389() throws Exception { + runTest("compiler/testData/codegen/box/closures/kt19389.kt"); + } + + @TestMetadata("kt19389_set.kt") + public void testKt19389_set() throws Exception { + runTest("compiler/testData/codegen/box/closures/kt19389_set.kt"); + } + @TestMetadata("kt2151.kt") public void testKt2151() throws Exception { runTest("compiler/testData/codegen/box/closures/kt2151.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxInlineCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxInlineCodegenTestGenerated.java index e3f4ba18b87..2fb953242d2 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxInlineCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxInlineCodegenTestGenerated.java @@ -186,6 +186,11 @@ public class IrBlackBoxInlineCodegenTestGenerated extends AbstractIrBlackBoxInli runTest("compiler/testData/codegen/boxInline/anonymousObject/kt17972_super3.kt"); } + @TestMetadata("kt19389.kt") + public void testKt19389() throws Exception { + runTest("compiler/testData/codegen/boxInline/anonymousObject/kt19389.kt"); + } + @TestMetadata("kt19399.kt") public void testKt19399() throws Exception { runTest("compiler/testData/codegen/boxInline/anonymousObject/kt19399.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 d2f8c55df29..cdbfa50f0d5 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 @@ -3136,6 +3136,16 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/closures/kt11634_4.kt"); } + @TestMetadata("kt19389.kt") + public void testKt19389() throws Exception { + runTest("compiler/testData/codegen/box/closures/kt19389.kt"); + } + + @TestMetadata("kt19389_set.kt") + public void testKt19389_set() throws Exception { + runTest("compiler/testData/codegen/box/closures/kt19389_set.kt"); + } + @TestMetadata("kt2151.kt") public void testKt2151() throws Exception { runTest("compiler/testData/codegen/box/closures/kt2151.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 12ca9f91692..abc72551f43 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 @@ -3136,6 +3136,16 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/closures/kt11634_4.kt"); } + @TestMetadata("kt19389.kt") + public void testKt19389() throws Exception { + runTest("compiler/testData/codegen/box/closures/kt19389.kt"); + } + + @TestMetadata("kt19389_set.kt") + public void testKt19389_set() throws Exception { + runTest("compiler/testData/codegen/box/closures/kt19389_set.kt"); + } + @TestMetadata("kt2151.kt") public void testKt2151() throws Exception { runTest("compiler/testData/codegen/box/closures/kt2151.kt");