From 9292022f880c6313ec856f0a35818468717ce885 Mon Sep 17 00:00:00 2001 From: Ilmir Usmanov Date: Mon, 9 Dec 2019 19:50:50 +0300 Subject: [PATCH] JVM_IR: Support inner lambdas which capture crossinline --- .../jetbrains/kotlin/codegen/inline/MethodInliner.kt | 6 ++++++ .../backend/jvm/lower/AddContinuationLowering.kt | 11 ++++++++++- .../codegen/boxInline/suspend/capturedVariables.kt | 2 -- .../boxInline/suspend/maxStackWithCrossinline.kt | 2 -- .../boxInline/suspend/stateMachine/innerLambda.kt | 2 -- .../suspend/stateMachine/innerMadnessCallSite.kt | 2 -- .../boxInline/suspend/stateMachine/passLambda.kt | 1 - 7 files changed, 16 insertions(+), 10 deletions(-) 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 3d7bd951cb7..e662bc5b14a 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/MethodInliner.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/MethodInliner.kt @@ -131,6 +131,12 @@ class MethodInliner( processReturns(resultNode, returnLabelOwner, remapReturn, end) //flush transformed node to output + // TODO: HACK resultNode's invisibleParameterAnnotations' size is greater than adapter's + if (resultNode.name == "" && resultNode.desc.contains("Lkotlin/coroutines/Continuation;") && + inliningContext.root.state.isIrBackend + ) { + resultNode.invisibleParameterAnnotations = null + } resultNode.accept(SkipMaxAndEndVisitor(adapter)) sourceMapper.endMapping() diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/AddContinuationLowering.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/AddContinuationLowering.kt index 67ad8abcc87..945b22cf13e 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/AddContinuationLowering.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/AddContinuationLowering.kt @@ -652,11 +652,20 @@ private class AddContinuationLowering(private val context: JvmBackendContext) : expression.acceptChildrenVoid(this) if (expression.isSuspend && expression !in inlineLambdas && expression.origin == IrStatementOrigin.LAMBDA) { + var expressionCapturesCrossinline = false + for (i in 0 until expression.valueArgumentsCount) { + val getValue = expression.getValueArgument(i) as? IrGetValue ?: continue + val owner = getValue.symbol.owner as? IrValueParameter ?: continue + if (owner.isCrossinline) { + expressionCapturesCrossinline = true + break + } + } suspendLambdas += SuspendLambdaInfo( expression.symbol.owner, (expression.type as IrSimpleType).arguments.size - 1, expression, - expression in capturesCrossinline + expressionCapturesCrossinline || expression in capturesCrossinline ) } } diff --git a/compiler/testData/codegen/boxInline/suspend/capturedVariables.kt b/compiler/testData/codegen/boxInline/suspend/capturedVariables.kt index 7b93accd885..e653ec6d307 100644 --- a/compiler/testData/codegen/boxInline/suspend/capturedVariables.kt +++ b/compiler/testData/codegen/boxInline/suspend/capturedVariables.kt @@ -1,5 +1,3 @@ -// IGNORE_BACKEND: JVM_IR -// IGNORE_BACKEND_MULTI_MODULE: JVM_IR // FILE: test.kt // COMMON_COROUTINES_TEST // WITH_RUNTIME diff --git a/compiler/testData/codegen/boxInline/suspend/maxStackWithCrossinline.kt b/compiler/testData/codegen/boxInline/suspend/maxStackWithCrossinline.kt index c570c170eb0..bd7723034fa 100644 --- a/compiler/testData/codegen/boxInline/suspend/maxStackWithCrossinline.kt +++ b/compiler/testData/codegen/boxInline/suspend/maxStackWithCrossinline.kt @@ -1,5 +1,3 @@ -// IGNORE_BACKEND: JVM_IR -// IGNORE_BACKEND_MULTI_MODULE: JVM_IR // FILE: inlined.kt // WITH_RUNTIME // NO_CHECK_LAMBDA_INLINING diff --git a/compiler/testData/codegen/boxInline/suspend/stateMachine/innerLambda.kt b/compiler/testData/codegen/boxInline/suspend/stateMachine/innerLambda.kt index c09ddee2bf5..462d21bd780 100644 --- a/compiler/testData/codegen/boxInline/suspend/stateMachine/innerLambda.kt +++ b/compiler/testData/codegen/boxInline/suspend/stateMachine/innerLambda.kt @@ -1,5 +1,3 @@ -// IGNORE_BACKEND: JVM_IR -// IGNORE_BACKEND_MULTI_MODULE: JVM_IR // FILE: inlined.kt // COMMON_COROUTINES_TEST // WITH_RUNTIME diff --git a/compiler/testData/codegen/boxInline/suspend/stateMachine/innerMadnessCallSite.kt b/compiler/testData/codegen/boxInline/suspend/stateMachine/innerMadnessCallSite.kt index 5d2c4c78e9f..54f4e40cbce 100644 --- a/compiler/testData/codegen/boxInline/suspend/stateMachine/innerMadnessCallSite.kt +++ b/compiler/testData/codegen/boxInline/suspend/stateMachine/innerMadnessCallSite.kt @@ -1,5 +1,3 @@ -// IGNORE_BACKEND: JVM_IR -// IGNORE_BACKEND_MULTI_MODULE: JVM_IR // FILE: inlined.kt // COMMON_COROUTINES_TEST // WITH_RUNTIME diff --git a/compiler/testData/codegen/boxInline/suspend/stateMachine/passLambda.kt b/compiler/testData/codegen/boxInline/suspend/stateMachine/passLambda.kt index 00378a83c22..c4971a4f0e0 100644 --- a/compiler/testData/codegen/boxInline/suspend/stateMachine/passLambda.kt +++ b/compiler/testData/codegen/boxInline/suspend/stateMachine/passLambda.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND: JVM_IR // IGNORE_BACKEND_MULTI_MODULE: JVM_IR // COMMON_COROUTINES_TEST // WITH_RUNTIME