From 5483ce09337752d07dfc48c01121e43cc76a536a Mon Sep 17 00:00:00 2001 From: Ilmir Usmanov Date: Thu, 29 Nov 2018 16:59:18 +0300 Subject: [PATCH] Do not map this when remapping parameters of static function inside inline lambda. #KT-23543 Fixed --- .../inline/AnonymousObjectTransformer.kt | 6 +- .../codegen/inline/ParametersBuilder.kt | 6 +- .../localFunInLambda/defaultParam.kt | 84 +++++++++++++++++++ .../BlackBoxInlineCodegenTestGenerated.java | 5 ++ ...otlinAgainstInlineKotlinTestGenerated.java | 5 ++ .../IrBlackBoxInlineCodegenTestGenerated.java | 5 ++ 6 files changed, 107 insertions(+), 4 deletions(-) create mode 100644 compiler/testData/codegen/boxInline/localFunInLambda/defaultParam.kt diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/AnonymousObjectTransformer.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/AnonymousObjectTransformer.kt index fffe5ad393e..e330c8a5d0d 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/AnonymousObjectTransformer.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/AnonymousObjectTransformer.kt @@ -380,7 +380,11 @@ class AnonymousObjectTransformer( } private fun getMethodParametersWithCaptured(capturedBuilder: ParametersBuilder, sourceNode: MethodNode): Parameters { - val builder = ParametersBuilder.initializeBuilderFrom(oldObjectType, sourceNode.desc) + val builder = ParametersBuilder.initializeBuilderFrom( + oldObjectType, + sourceNode.desc, + isStatic = sourceNode.access and Opcodes.ACC_STATIC != 0 + ) for (param in capturedBuilder.listCaptured()) { builder.addCapturedParamCopy(param) } diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/ParametersBuilder.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/ParametersBuilder.kt index c3811ec1da0..a28369e7dfc 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/ParametersBuilder.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/ParametersBuilder.kt @@ -1,5 +1,5 @@ /* - * Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the license/LICENSE.txt file. */ @@ -110,10 +110,10 @@ class ParametersBuilder private constructor() { @JvmOverloads @JvmStatic fun initializeBuilderFrom( - objectType: Type, descriptor: String, inlineLambda: LambdaInfo? = null + objectType: Type, descriptor: String, inlineLambda: LambdaInfo? = null, isStatic: Boolean = false ): ParametersBuilder { val builder = newBuilder() - if (inlineLambda?.hasDispatchReceiver != false) { + if (inlineLambda?.hasDispatchReceiver != false && !isStatic) { //skipped this for inlined lambda cause it will be removed builder.addThis(objectType, inlineLambda != null).lambda = inlineLambda } diff --git a/compiler/testData/codegen/boxInline/localFunInLambda/defaultParam.kt b/compiler/testData/codegen/boxInline/localFunInLambda/defaultParam.kt new file mode 100644 index 00000000000..314302a9652 --- /dev/null +++ b/compiler/testData/codegen/boxInline/localFunInLambda/defaultParam.kt @@ -0,0 +1,84 @@ +// IGNORE_BACKEND: JVM_IR +// FILE: 1.kt + +inline fun run(c: () -> T): T = c() + +// FILE: 2.kt +// NO_CHECK_LAMBDA_INLINING + +interface Runnable { + fun run(): String +} + +interface RunnableString { + fun run(s: String): String +} + +fun fooInt(): String { + val r = "O" + val a = run { + fun f(x: Int, y: String? = null): String = r + x + y + f(1, "K") + } + return a +} + +fun fooLong(): String { + val r = "O" + val a = run { + fun f(x: Long, y: String? = null): String = r + x + y + f(2, "K") + } + return a +} + +fun fooLongInsideObject(): String { + val r = "O" + val a = object: Runnable { + override fun run(): String { + fun f(x: Long, y: String? = null): String = r + x + y + return f(3, "K") + } + } + return a.run() +} + +fun fooLongCallableReference(): String { + val r = "O" + val a = run { + fun f(x: Long, y: String? = null): String = r + x + y + (::f)(4, "K") + } + return a +} + +class A { + fun fooLongSyntheticAccessor(capt: Int): String { + val o: RunnableString = run { + object: RunnableString { + override fun run(captured: String): String { + return { + callPrivate(capt, captured) + }() + } + + private fun callPrivate(x: Int, y: String?): String = "O" + x + y + } + } + return o.run("K") + } +} + +fun box(): String { + var res = fooInt() + if (res != "O1K") return res + res = fooLong() + if (res != "O2K") return res + res = fooLongInsideObject() + if (res != "O3K") return res + res = fooLongCallableReference() + if (res != "O4K") return res + res = A().fooLongSyntheticAccessor(5) + if (res != "O5K") return res + return "OK" +} diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java index 329cb7cc9cb..e92d9650ee0 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java @@ -1761,6 +1761,11 @@ public class BlackBoxInlineCodegenTestGenerated extends AbstractBlackBoxInlineCo KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/boxInline/localFunInLambda"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); } + @TestMetadata("defaultParam.kt") + public void testDefaultParam() throws Exception { + runTest("compiler/testData/codegen/boxInline/localFunInLambda/defaultParam.kt"); + } + @TestMetadata("lambdaInLambdaCapturesAnotherFun.kt") public void testLambdaInLambdaCapturesAnotherFun() throws Exception { runTest("compiler/testData/codegen/boxInline/localFunInLambda/lambdaInLambdaCapturesAnotherFun.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java index 12e5d2b3b63..0391a31d0f0 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java @@ -1761,6 +1761,11 @@ public class CompileKotlinAgainstInlineKotlinTestGenerated extends AbstractCompi KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/boxInline/localFunInLambda"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); } + @TestMetadata("defaultParam.kt") + public void testDefaultParam() throws Exception { + runTest("compiler/testData/codegen/boxInline/localFunInLambda/defaultParam.kt"); + } + @TestMetadata("lambdaInLambdaCapturesAnotherFun.kt") public void testLambdaInLambdaCapturesAnotherFun() throws Exception { runTest("compiler/testData/codegen/boxInline/localFunInLambda/lambdaInLambdaCapturesAnotherFun.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxInlineCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxInlineCodegenTestGenerated.java index 3ce8bc30610..41db1e5a255 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxInlineCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxInlineCodegenTestGenerated.java @@ -1761,6 +1761,11 @@ public class IrBlackBoxInlineCodegenTestGenerated extends AbstractIrBlackBoxInli KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/boxInline/localFunInLambda"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM_IR, true); } + @TestMetadata("defaultParam.kt") + public void testDefaultParam() throws Exception { + runTest("compiler/testData/codegen/boxInline/localFunInLambda/defaultParam.kt"); + } + @TestMetadata("lambdaInLambdaCapturesAnotherFun.kt") public void testLambdaInLambdaCapturesAnotherFun() throws Exception { runTest("compiler/testData/codegen/boxInline/localFunInLambda/lambdaInLambdaCapturesAnotherFun.kt");