diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java index 0879af22682..b644f51715d 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java @@ -2714,14 +2714,16 @@ public class ExpressionCodegen extends KtVisitor impleme private StackValue generateExtensionReceiver(@NotNull CallableDescriptor descriptor) { KotlinType coroutineControllerType = CoroutineUtilKt.getControllerTypeIfCoroutine(descriptor); if (coroutineControllerType != null) { - ClassDescriptor thisDescriptor = context.getThisDescriptor(); + ClassDescriptor classDescriptor = bindingContext.get(CodegenBinding.CLASS_FOR_CALLABLE, descriptor); + assert classDescriptor != null : "class descriptor for coroutine " + descriptor + " should not be null"; + StackValue coroutineReceiver = StackValue.thisOrOuter(this, classDescriptor, /* isSuper =*/ false, /* castReceiver */ false); return StackValue.field( - FieldInfo.createForHiddenField( - typeMapper.mapClass(thisDescriptor), + FieldInfo.createForHiddenField( + typeMapper.mapClass(classDescriptor), typeMapper.mapType(coroutineControllerType), CoroutineCodegenUtilKt.COROUTINE_CONTROLLER_FIELD_NAME), - StackValue.thisOrOuter(this, thisDescriptor, /* isSuper =*/ false, /* castReceiver */ false)); + coroutineReceiver); } return context.generateReceiver(descriptor, state, false); diff --git a/compiler/testData/codegen/box/coroutines/suspendFromInlineLambda.kt b/compiler/testData/codegen/box/coroutines/suspendFromInlineLambda.kt new file mode 100644 index 00000000000..05af6e01711 --- /dev/null +++ b/compiler/testData/codegen/box/coroutines/suspendFromInlineLambda.kt @@ -0,0 +1,31 @@ +class Controller { + suspend fun suspendHere(v: Int, x: Continuation) { + x.resume(v * 2) + } +} + +fun builder(coroutine c: Controller.() -> Continuation) { + c(Controller()).resume(Unit) +} + +inline fun foo(x: (Int) -> Unit) { + for (i in 1..2) { + x(i) + } +} + +fun box(): String { + var result = "" + + builder { + result += "-" + foo { + result += suspendHere(it).toString() + } + result += "+" + } + + if (result != "-24+") return "fail: $result" + + return "OK" +} diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 5d1fa6e1019..cfeecf1c04c 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -4147,6 +4147,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { doTest(fileName); } + @TestMetadata("suspendFromInlineLambda.kt") + public void testSuspendFromInlineLambda() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/suspendFromInlineLambda.kt"); + doTest(fileName); + } + @TestMetadata("suspendInCycle.kt") public void testSuspendInCycle() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/suspendInCycle.kt");