From 7a1625fc9e1d2645852fa721c727977429abda1f Mon Sep 17 00:00:00 2001 From: Michael Bogdanov Date: Mon, 8 Jun 2015 12:48:38 +0300 Subject: [PATCH] Support extension lambda inlining #KT-7962 Fixed --- .../jetbrains/kotlin/codegen/inline/LambdaInfo.java | 12 ++++++++++-- .../codegen/boxInline/simple/extensionLambda.1.kt | 9 +++++++++ .../codegen/boxInline/simple/extensionLambda.2.kt | 3 +++ .../BlackBoxInlineCodegenTestGenerated.java | 6 ++++++ ...ompileKotlinAgainstInlineKotlinTestGenerated.java | 6 ++++++ 5 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 compiler/testData/codegen/boxInline/simple/extensionLambda.1.kt create mode 100644 compiler/testData/codegen/boxInline/simple/extensionLambda.2.kt diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/LambdaInfo.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/LambdaInfo.java index bb72d658571..e8e6656557c 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/LambdaInfo.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/LambdaInfo.java @@ -24,11 +24,11 @@ import org.jetbrains.kotlin.codegen.context.EnclosedValueDescriptor; import org.jetbrains.kotlin.codegen.state.JetTypeMapper; import org.jetbrains.kotlin.descriptors.ClassDescriptor; import org.jetbrains.kotlin.descriptors.FunctionDescriptor; +import org.jetbrains.kotlin.descriptors.ReceiverParameterDescriptor; import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor; import org.jetbrains.kotlin.psi.JetExpression; import org.jetbrains.kotlin.psi.JetFunctionLiteralExpression; import org.jetbrains.kotlin.resolve.BindingContext; -import org.jetbrains.kotlin.resolve.DescriptorUtils; import org.jetbrains.kotlin.resolve.jvm.AsmTypes; import org.jetbrains.org.objectweb.asm.Type; import org.jetbrains.org.objectweb.asm.tree.FieldInsnNode; @@ -156,7 +156,14 @@ public class LambdaInfo implements CapturedParamOwner, LabelOwner { //add skipped this cause inlined lambda doesn't have it builder.addThis(AsmTypes.OBJECT_TYPE, true).setLambda(this); - List valueParameters = getFunctionDescriptor().getValueParameters(); + FunctionDescriptor lambdaDescriptor = getFunctionDescriptor(); + ReceiverParameterDescriptor extensionParameter = lambdaDescriptor.getExtensionReceiverParameter(); + if (extensionParameter != null) { + Type type = typeMapper.mapType(extensionParameter.getType()); + builder.addNextParameter(type, false, null); + } + + List valueParameters = lambdaDescriptor.getValueParameters(); for (ValueParameterDescriptor parameter : valueParameters) { Type type = typeMapper.mapType(parameter.getType()); builder.addNextParameter(type, false, null); @@ -181,3 +188,4 @@ public class LambdaInfo implements CapturedParamOwner, LabelOwner { } } + diff --git a/compiler/testData/codegen/boxInline/simple/extensionLambda.1.kt b/compiler/testData/codegen/boxInline/simple/extensionLambda.1.kt new file mode 100644 index 00000000000..c341a09eb6c --- /dev/null +++ b/compiler/testData/codegen/boxInline/simple/extensionLambda.1.kt @@ -0,0 +1,9 @@ +import test.* + +fun box(): String { + val p = "".test(50.0) { + it + } + + return if (p == 50.0) "OK" else "fail $p" +} \ No newline at end of file diff --git a/compiler/testData/codegen/boxInline/simple/extensionLambda.2.kt b/compiler/testData/codegen/boxInline/simple/extensionLambda.2.kt new file mode 100644 index 00000000000..1c3b1968ade --- /dev/null +++ b/compiler/testData/codegen/boxInline/simple/extensionLambda.2.kt @@ -0,0 +1,3 @@ +package test + +inline fun String.test(default: T, cb: String.(T) -> T): T = cb(default) \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxInlineCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxInlineCodegenTestGenerated.java index f31c55effcc..9792515275f 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxInlineCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxInlineCodegenTestGenerated.java @@ -823,6 +823,12 @@ public class BlackBoxInlineCodegenTestGenerated extends AbstractBlackBoxInlineCo doTestMultiFileWithInlineCheck(fileName); } + @TestMetadata("extensionLambda.1.kt") + public void testExtensionLambda() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/simple/extensionLambda.1.kt"); + doTestMultiFileWithInlineCheck(fileName); + } + @TestMetadata("params.1.kt") public void testParams() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/simple/params.1.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileKotlinAgainstInlineKotlinTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileKotlinAgainstInlineKotlinTestGenerated.java index a745ddc9644..66e597a9598 100644 --- a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileKotlinAgainstInlineKotlinTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileKotlinAgainstInlineKotlinTestGenerated.java @@ -823,6 +823,12 @@ public class CompileKotlinAgainstInlineKotlinTestGenerated extends AbstractCompi doBoxTestWithInlineCheck(fileName); } + @TestMetadata("extensionLambda.1.kt") + public void testExtensionLambda() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/simple/extensionLambda.1.kt"); + doBoxTestWithInlineCheck(fileName); + } + @TestMetadata("params.1.kt") public void testParams() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/simple/params.1.kt");