Do not duplicate $$forInline counterpart of inline capturing function

This commit is contained in:
Ilmir Usmanov
2019-04-15 20:29:04 +03:00
parent 20b9d8b2f3
commit 7c14f4c6ae
10 changed files with 229 additions and 35 deletions
@@ -65,8 +65,10 @@ open class SuspendFunctionGenerationStrategy(
)
val forInline = state.bindingContext[CodegenBinding.CAPTURES_CROSSINLINE_LAMBDA, originalSuspendDescriptor] == true
// Yegor Bugayenko style
return if (forInline)
// Both capturing and inline functions share the same suffix, however, inline functions can also be capturing
// they are already covered by SuspendInlineFunctionGenerationStrategy, thus, if we generate yet another copy,
// we will get name+descriptor clash
return if (forInline && !originalSuspendDescriptor.isInline)
AddConstructorCallForCoroutineRegeneration(
MethodNodeCopyingMethodVisitor(
SurroundSuspendLambdaCallsWithSuspendMarkersMethodVisitor(
@@ -48,8 +48,6 @@ class CoroutineTransformer(
fun shouldGenerateStateMachine(node: MethodNode): Boolean {
// Continuations are similar to lambdas from bird's view, but we should never generate state machine for them
if (isContinuationNotLambda()) return false
// The method does not have state-machine, but should. Generate it
if (node.name.endsWith(FOR_INLINE_SUFFIX)) return true
// there can be suspend lambdas inside inline functions, which do not
// capture crossinline lambdas, thus, there is no need to transform them
return isSuspendFunctionWithFakeConstructorCall(node) || (isSuspendLambda(node) && !isStateMachine(node))
@@ -225,7 +223,7 @@ class SurroundSuspendLambdaCallsWithSuspendMarkersMethodVisitor(
if (insn.opcode != Opcodes.INVOKEINTERFACE) continue
insn as MethodInsnNode
if (!isInvokeOnLambda(insn.owner, insn.name)) continue
val frame = sourceFrames[insn.index()]
val frame = sourceFrames[insn.index()] ?: continue
val receiver = findReceiverOfInvoke(frame, insn).takeIf { it?.isSuspendLambda(insn) == true } as? FieldInsnNode ?: continue
val aload = receiver.findPreviousOrNull { it.opcode != Opcodes.GETFIELD } ?: error("GETFIELD cannot be the first instruction")
assert(aload.opcode == Opcodes.ALOAD) { "Before GETFIELD there shall be ALOAD" }
@@ -0,0 +1,192 @@
// IGNORE_BACKEND: JVM_IR
// TARGET_BACKEND: JVM
// COMMON_COROUTINES_TEST
// WITH_RUNTIME
// WITH_COROUTINES
// NO_CHECK_LAMBDA_INLINING
// CHECK_STATE_MACHINE
// FILE: inlineMe.kt
package test
import helpers.*
interface SuspendRunnable {
suspend fun run()
suspend fun run1()
suspend fun run2()
}
inline fun inlineMe(crossinline c: suspend () -> Unit) = object : SuspendRunnable {
override suspend fun run() {
c(); c()
}
override suspend fun run1() {
c(); c()
}
override suspend fun run2() {
inlineMeInner()
}
inline suspend fun inlineMeInner() {
StateMachineChecker.suspendHere()
StateMachineChecker.suspendHere()
}
// TODO: call it from run1
inline suspend fun inlineMeCapturing() {
c(); c()
}
}
inline fun inlineMe2(crossinline c: suspend () -> Unit) = inlineMe { c(); c() }
inline fun inlineMe3(crossinline c: suspend () -> Unit) = object: SuspendRunnable {
override suspend fun run() {
var sr = inlineMe {
c()
c()
}
sr.run()
sr.run1()
sr.run2()
}
override suspend fun run1() {
}
override suspend fun run2() {
}
}
// FILE: A.java
import test.InlineMeKt;
import helpers.CoroutineUtilKt;
import helpers.EmptyContinuation;
import kotlin.Unit;
public class A {
public static Object call() {
return InlineMeKt.inlineMe((continuation) -> CoroutineUtilKt.getStateMachineChecker().suspendHere(continuation));
}
public static Object call2() {
return InlineMeKt.inlineMe2((continuation) -> CoroutineUtilKt.getStateMachineChecker().suspendHere(continuation));
}
public static Object call3() {
return InlineMeKt.inlineMe3((continuation) -> CoroutineUtilKt.getStateMachineChecker().suspendHere(continuation));
}
}
// FILE: box.kt
import test.*
import helpers.*
import COROUTINES_PACKAGE.*
fun builder(c: suspend () -> Unit) {
c.startCoroutine(CheckStateMachineContinuation)
}
fun box(): String {
builder {
(A.call() as SuspendRunnable).run()
}
StateMachineChecker.check(2)
StateMachineChecker.reset()
builder {
(A.call() as SuspendRunnable).run1()
}
StateMachineChecker.check(2)
StateMachineChecker.reset()
builder {
(A.call() as SuspendRunnable).run2()
}
StateMachineChecker.check(2)
StateMachineChecker.reset()
builder {
(A.call2() as SuspendRunnable).run()
}
StateMachineChecker.check(4)
StateMachineChecker.reset()
builder {
(A.call2() as SuspendRunnable).run1()
}
StateMachineChecker.check(4)
StateMachineChecker.reset()
builder {
(A.call2() as SuspendRunnable).run2()
}
StateMachineChecker.check(2)
StateMachineChecker.reset()
builder {
(A.call3() as SuspendRunnable).run()
}
StateMachineChecker.check(10)
StateMachineChecker.reset()
builder {
inlineMe {
StateMachineChecker.suspendHere()
StateMachineChecker.suspendHere()
}.run()
}
StateMachineChecker.check(4)
StateMachineChecker.reset()
builder {
inlineMe {
StateMachineChecker.suspendHere()
StateMachineChecker.suspendHere()
}.run1()
}
StateMachineChecker.check(4)
StateMachineChecker.reset()
builder {
inlineMe {
StateMachineChecker.suspendHere()
StateMachineChecker.suspendHere()
}.run2()
}
StateMachineChecker.check(2)
StateMachineChecker.reset()
builder {
inlineMe2 {
StateMachineChecker.suspendHere()
StateMachineChecker.suspendHere()
}.run()
}
StateMachineChecker.check(8)
StateMachineChecker.reset()
builder {
inlineMe2 {
StateMachineChecker.suspendHere()
StateMachineChecker.suspendHere()
}.run1()
}
StateMachineChecker.check(8)
StateMachineChecker.reset()
builder {
inlineMe2 {
StateMachineChecker.suspendHere()
StateMachineChecker.suspendHere()
}.run2()
}
StateMachineChecker.check(2)
StateMachineChecker.reset()
builder {
inlineMe3 {
StateMachineChecker.suspendHere()
StateMachineChecker.suspendHere()
}.run()
}
StateMachineChecker.check(18)
return "OK"
}
@@ -1,4 +1,5 @@
// IGNORE_BACKEND: JVM_IR
// TARGET_BACKEND: JVM
// COMMON_COROUTINES_TEST
// WITH_RUNTIME
// WITH_COROUTINES
@@ -1,4 +1,5 @@
// IGNORE_BACKEND: JVM_IR
// TARGET_BACKEND: JVM
// COMMON_COROUTINES_TEST
// WITH_RUNTIME
// WITH_COROUTINES
@@ -7397,6 +7397,16 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/coroutines/javaInterop"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true);
}
@TestMetadata("objectWithSeveralSuspends.kt")
public void testObjectWithSeveralSuspends_1_2() throws Exception {
runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/javaInterop/objectWithSeveralSuspends.kt", "kotlin.coroutines.experimental");
}
@TestMetadata("objectWithSeveralSuspends.kt")
public void testObjectWithSeveralSuspends_1_3() throws Exception {
runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/javaInterop/objectWithSeveralSuspends.kt", "kotlin.coroutines");
}
@TestMetadata("returnLambda.kt")
public void testReturnLambda_1_2() throws Exception {
runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/javaInterop/returnLambda.kt", "kotlin.coroutines.experimental");
@@ -7397,6 +7397,16 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/coroutines/javaInterop"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true);
}
@TestMetadata("objectWithSeveralSuspends.kt")
public void testObjectWithSeveralSuspends_1_2() throws Exception {
runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/javaInterop/objectWithSeveralSuspends.kt", "kotlin.coroutines.experimental");
}
@TestMetadata("objectWithSeveralSuspends.kt")
public void testObjectWithSeveralSuspends_1_3() throws Exception {
runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/javaInterop/objectWithSeveralSuspends.kt", "kotlin.coroutines");
}
@TestMetadata("returnLambda.kt")
public void testReturnLambda_1_2() throws Exception {
runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/javaInterop/returnLambda.kt", "kotlin.coroutines.experimental");
@@ -7397,6 +7397,16 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/coroutines/javaInterop"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM_IR, true);
}
@TestMetadata("objectWithSeveralSuspends.kt")
public void testObjectWithSeveralSuspends_1_2() throws Exception {
runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/javaInterop/objectWithSeveralSuspends.kt", "kotlin.coroutines.experimental");
}
@TestMetadata("objectWithSeveralSuspends.kt")
public void testObjectWithSeveralSuspends_1_3() throws Exception {
runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/javaInterop/objectWithSeveralSuspends.kt", "kotlin.coroutines");
}
@TestMetadata("returnLambda.kt")
public void testReturnLambda_1_2() throws Exception {
runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/javaInterop/returnLambda.kt", "kotlin.coroutines.experimental");
@@ -5716,16 +5716,6 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
public void testAllFilesPresentInJavaInterop() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/coroutines/javaInterop"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JS_IR, true);
}
@TestMetadata("returnLambda.kt")
public void testReturnLambda_1_3() throws Exception {
runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/javaInterop/returnLambda.kt", "kotlin.coroutines");
}
@TestMetadata("returnObject.kt")
public void testReturnObject_1_3() throws Exception {
runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/javaInterop/returnObject.kt", "kotlin.coroutines");
}
}
@TestMetadata("compiler/testData/codegen/box/coroutines/localFunctions")
@@ -6456,26 +6456,6 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
public void testAllFilesPresentInJavaInterop() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/coroutines/javaInterop"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JS, true);
}
@TestMetadata("returnLambda.kt")
public void testReturnLambda_1_2() throws Exception {
runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/javaInterop/returnLambda.kt", "kotlin.coroutines.experimental");
}
@TestMetadata("returnLambda.kt")
public void testReturnLambda_1_3() throws Exception {
runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/javaInterop/returnLambda.kt", "kotlin.coroutines");
}
@TestMetadata("returnObject.kt")
public void testReturnObject_1_2() throws Exception {
runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/javaInterop/returnObject.kt", "kotlin.coroutines.experimental");
}
@TestMetadata("returnObject.kt")
public void testReturnObject_1_3() throws Exception {
runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/javaInterop/returnObject.kt", "kotlin.coroutines");
}
}
@TestMetadata("compiler/testData/codegen/box/coroutines/localFunctions")