From d9dc2bd443933f1060b8934f31225c75a2f7762a Mon Sep 17 00:00:00 2001 From: Mikhael Bogdanov Date: Tue, 16 May 2017 14:21:05 +0200 Subject: [PATCH] Minor. Fix review remarks --- .../inline/AnonymousObjectTransformer.kt | 2 +- .../kotlin/codegen/inline/InliningContext.kt | 2 +- .../kotlin/codegen/inline/MethodInliner.kt | 13 +++++----- .../kotlin/codegen/inline/TypeRemapper.kt | 2 +- .../lambdaInlining/constuctorReference.kt | 20 ++++++++++++++++ .../functionImportedFromObject.kt | 24 +++++++++++++++++++ .../propertyImportedFromObject.kt | 21 ++++++++++++++++ .../BlackBoxInlineCodegenTestGenerated.java | 18 ++++++++++++++ ...otlinAgainstInlineKotlinTestGenerated.java | 18 ++++++++++++++ 9 files changed, 110 insertions(+), 10 deletions(-) create mode 100644 compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/constuctorReference.kt create mode 100644 compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/functionImportedFromObject.kt create mode 100644 compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/propertyImportedFromObject.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 840d2f34845..24ff74959fd 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/AnonymousObjectTransformer.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/AnonymousObjectTransformer.kt @@ -183,7 +183,7 @@ class AnonymousObjectTransformer( capturedBuilder: ParametersBuilder, isConstructor: Boolean ): InlineResult { - val typeParametersToReify = inliningContext.root.inlineMethodReificator.reifyInstructions(sourceNode) + val typeParametersToReify = inliningContext.root.inlineMethodReifier.reifyInstructions(sourceNode) val parameters = if (isConstructor) capturedBuilder.buildParameters() else getMethodParametersWithCaptured(capturedBuilder, sourceNode) val remapper = RegeneratedLambdaFieldRemapper( diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InliningContext.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InliningContext.kt index 0c95089daf3..91c21fccd52 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InliningContext.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InliningContext.kt @@ -25,7 +25,7 @@ class RootInliningContext( nameGenerator: NameGenerator, val callElement: KtElement, override val callSiteInfo: InlineCallSiteInfo, - val inlineMethodReificator: ReifiedTypeInliner, + val inlineMethodReifier: ReifiedTypeInliner, typeParameterMappings: TypeParameterMappings ) : InliningContext( null, expressionMap, state, nameGenerator, TypeRemapper.createRoot(typeParameterMappings), null, false diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/MethodInliner.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/MethodInliner.kt index bad224533e3..2d1f762d4a0 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/MethodInliner.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/MethodInliner.kt @@ -75,11 +75,9 @@ class MethodInliner( ): InlineResult { //analyze body var transformedNode = markPlacesForInlineAndRemoveInlinable(node, labelOwner, finallyDeepShift) - if (inliningContext.isInliningLambda && - inliningContext.lambdaInfo is DefaultLambda && - inliningContext.lambdaInfo.needReification) { + if (inliningContext.isInliningLambda && isDefaultLambdaWithReification(inliningContext.lambdaInfo!!)) { //TODO maybe move reification in one place - inliningContext.root.inlineMethodReificator.reifyInstructions(transformedNode) + inliningContext.root.inlineMethodReifier.reifyInstructions(transformedNode) } //substitute returns with "goto end" instruction to keep non local returns in lambdas @@ -277,9 +275,7 @@ class MethodInliner( super.visitMethodInsn(opcode, owner, name, desc, itf) } } - else if ((!inliningContext.isInliningLambda || - inliningContext.lambdaInfo is DefaultLambda && - inliningContext.lambdaInfo.needReification) && + else if ((!inliningContext.isInliningLambda || isDefaultLambdaWithReification(inliningContext.lambdaInfo!!)) && ReifiedTypeInliner.isNeedClassReificationMarker(MethodInsnNode(opcode, owner, name, desc, false))) { //we shouldn't process here content of inlining lambda it should be reified at external level except default lambdas } @@ -306,6 +302,9 @@ class MethodInliner( return resultNode } + private fun isDefaultLambdaWithReification(lambdaInfo: LambdaInfo) = + lambdaInfo is DefaultLambda && lambdaInfo.needReification + private fun prepareNode(node: MethodNode, finallyDeepShift: Int): MethodNode { node.instructions.resetLabels() diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/TypeRemapper.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/TypeRemapper.kt index 968246afdf6..9aae494805f 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/TypeRemapper.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/TypeRemapper.kt @@ -26,7 +26,7 @@ class TypeRemapper private constructor( val parent: TypeRemapper? = null, val isRootInlineLambda: Boolean = false ) { - private var additionalMappings = hashMapOf() + private val additionalMappings = hashMapOf() private val typeParametersMapping = hashMapOf() fun addMapping(type: String, newType: String) { diff --git a/compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/constuctorReference.kt b/compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/constuctorReference.kt new file mode 100644 index 00000000000..b953e910015 --- /dev/null +++ b/compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/constuctorReference.kt @@ -0,0 +1,20 @@ +// FILE: 1.kt +// LANGUAGE_VERSION: 1.2 +// SKIP_INLINE_CHECK_IN: inlineFun$default +package test + +class A(val value: String) { + fun ok() = value +} + +inline fun inlineFun(a: String, lambda: (String) -> A = ::A): A { + return lambda(a) +} + +// FILE: 2.kt + +import test.* + +fun box(): String { + return inlineFun("OK").value +} \ No newline at end of file diff --git a/compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/functionImportedFromObject.kt b/compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/functionImportedFromObject.kt new file mode 100644 index 00000000000..98b9e147958 --- /dev/null +++ b/compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/functionImportedFromObject.kt @@ -0,0 +1,24 @@ +// FILE: 1.kt +package test + +fun ok() = "OK" + +object A { + fun ok() = "OK" +} + +inline fun stub() {} + + +// FILE: 2.kt +// LANGUAGE_VERSION: 1.2 +// SKIP_INLINE_CHECK_IN: inlineFun$default +import test.A.ok + +inline fun inlineFun(lambda: () -> String = ::ok): String { + return lambda() +} + +fun box(): String { + return inlineFun() +} \ No newline at end of file diff --git a/compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/propertyImportedFromObject.kt b/compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/propertyImportedFromObject.kt new file mode 100644 index 00000000000..c09d36b5d90 --- /dev/null +++ b/compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/propertyImportedFromObject.kt @@ -0,0 +1,21 @@ +// FILE: 1.kt +package test + +object A { + val ok = "OK" +} +inline fun stub() {} + + +// FILE: 2.kt +// LANGUAGE_VERSION: 1.2 +// SKIP_INLINE_CHECK_IN: inlineFun$default +import test.A.ok + +inline fun inlineFun(lambda: () -> String = ::ok): String { + return lambda() +} + +fun box(): String { + return inlineFun() +} \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java index bda9327fd1e..b8b0bf391d6 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java @@ -973,12 +973,24 @@ public class BlackBoxInlineCodegenTestGenerated extends AbstractBlackBoxInlineCo doTest(fileName); } + @TestMetadata("constuctorReference.kt") + public void testConstuctorReference() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/constuctorReference.kt"); + doTest(fileName); + } + @TestMetadata("defaultLambdaInNoInline.kt") public void testDefaultLambdaInNoInline() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/defaultLambdaInNoInline.kt"); doTest(fileName); } + @TestMetadata("functionImportedFromObject.kt") + public void testFunctionImportedFromObject() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/functionImportedFromObject.kt"); + doTest(fileName); + } + @TestMetadata("functionReference.kt") public void testFunctionReference() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/functionReference.kt"); @@ -1027,6 +1039,12 @@ public class BlackBoxInlineCodegenTestGenerated extends AbstractBlackBoxInlineCo doTest(fileName); } + @TestMetadata("propertyImportedFromObject.kt") + public void testPropertyImportedFromObject() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/propertyImportedFromObject.kt"); + doTest(fileName); + } + @TestMetadata("propertyReference.kt") public void testPropertyReference() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/propertyReference.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java index 1b450d18d01..9f15be9028f 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java @@ -973,12 +973,24 @@ public class CompileKotlinAgainstInlineKotlinTestGenerated extends AbstractCompi doTest(fileName); } + @TestMetadata("constuctorReference.kt") + public void testConstuctorReference() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/constuctorReference.kt"); + doTest(fileName); + } + @TestMetadata("defaultLambdaInNoInline.kt") public void testDefaultLambdaInNoInline() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/defaultLambdaInNoInline.kt"); doTest(fileName); } + @TestMetadata("functionImportedFromObject.kt") + public void testFunctionImportedFromObject() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/functionImportedFromObject.kt"); + doTest(fileName); + } + @TestMetadata("functionReference.kt") public void testFunctionReference() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/functionReference.kt"); @@ -1027,6 +1039,12 @@ public class CompileKotlinAgainstInlineKotlinTestGenerated extends AbstractCompi doTest(fileName); } + @TestMetadata("propertyImportedFromObject.kt") + public void testPropertyImportedFromObject() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/propertyImportedFromObject.kt"); + doTest(fileName); + } + @TestMetadata("propertyReference.kt") public void testPropertyReference() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/propertyReference.kt");