IC & Coroutines: Unbox inline classes of suspend lambdas

inside 'invoke' if 'create' does not override 'create' from
BaseContinuationImpl. In other words, when suspend lambda accepts more
than one parameter (including receiver).

Do that only if we do not generate bridge 'invoke' method, since
inline classes are unboxed in the bridge.

Use mangled name for 'create' function in this case inside 'invoke'.

 #KT-43249 In progress
 #KT-39847 Fixed
 #KT-38937 Fixed
This commit is contained in:
Ilmir Usmanov
2020-11-25 22:26:51 +01:00
parent 0a0b5b5d2b
commit eba260f681
11 changed files with 202 additions and 5 deletions
@@ -294,7 +294,7 @@ class CoroutineCodegenForLambda private constructor(
functionCodegen.generateMethod(JvmDeclarationOrigin.NO_ORIGIN, funDescriptor,
object : FunctionGenerationStrategy.CodegenBased(state) {
override fun doGenerateBody(codegen: ExpressionCodegen, signature: JvmMethodSignature) {
codegen.v.generateInvokeMethod(signature)
codegen.v.generateInvokeMethod(signature, funDescriptor)
}
})
}
@@ -316,13 +316,13 @@ class CoroutineCodegenForLambda private constructor(
)
mv.visitCode()
with(InstructionAdapter(mv)) {
generateInvokeMethod(jvmMethodSignature)
generateInvokeMethod(jvmMethodSignature, untypedDescriptor)
}
FunctionCodegen.endVisit(mv, "invoke", element)
}
private fun InstructionAdapter.generateInvokeMethod(signature: JvmMethodSignature) {
private fun InstructionAdapter.generateInvokeMethod(signature: JvmMethodSignature, descriptor: FunctionDescriptor) {
// this
load(0, AsmTypes.OBJECT_TYPE)
val parameterTypes = signature.valueParameters.map { it.asmType }
@@ -348,16 +348,23 @@ class CoroutineCodegenForLambda private constructor(
load(arraySlot, AsmTypes.OBJECT_TYPE)
} else {
var index = 0
val fromKotlinTypes =
if (!generateErasedCreate && doNotGenerateInvokeBridge) funDescriptor.allValueParameterTypes()
else funDescriptor.allValueParameterTypes().map { funDescriptor.module.builtIns.nullableAnyType }
val toKotlinTypes =
if (!generateErasedCreate && doNotGenerateInvokeBridge) createCoroutineDescriptor.allValueParameterTypes()
else descriptor.allValueParameterTypes()
parameterTypes.withVariableIndices().forEach { (varIndex, type) ->
load(varIndex + 1, type)
StackValue.coerce(type, createArgumentTypes[index++], this)
StackValue.coerce(type, fromKotlinTypes[index], createArgumentTypes[index], toKotlinTypes[index], this)
index++
}
}
// this.create(..)
invokevirtual(
v.thisName,
createCoroutineDescriptor.name.identifier,
typeMapper.mapFunctionName(createCoroutineDescriptor, null),
Type.getMethodDescriptor(
languageVersionSettings.continuationAsmType(),
*createArgumentTypes.toTypedArray()
@@ -831,3 +838,6 @@ private object FailingFunctionGenerationStrategy : FunctionGenerationStrategy()
fun reportSuspensionPointInsideMonitor(element: KtElement, state: GenerationState, stackTraceElement: String) {
state.diagnostics.report(ErrorsJvm.SUSPENSION_POINT_INSIDE_MONITOR.on(element, stackTraceElement))
}
private fun FunctionDescriptor.allValueParameterTypes(): List<KotlinType> =
(listOfNotNull(extensionReceiverParameter?.type)) + valueParameters.map { it.type }
@@ -7743,6 +7743,11 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/direct/covariantOverrideSuspendFun_Int.kt");
}
@TestMetadata("createMangling.kt")
public void testCreateMangling() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/direct/createMangling.kt");
}
@TestMetadata("genericOverrideSuspendFun.kt")
public void testGenericOverrideSuspendFun() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/direct/genericOverrideSuspendFun.kt");
@@ -7960,6 +7965,11 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/resume/covariantOverrideSuspendFun_Int.kt");
}
@TestMetadata("createMangling.kt")
public void testCreateMangling() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/resume/createMangling.kt");
}
@TestMetadata("genericOverrideSuspendFun.kt")
public void testGenericOverrideSuspendFun() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/resume/genericOverrideSuspendFun.kt");
@@ -8172,6 +8182,11 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/resumeWithException/covariantOverrideSuspendFun_Int.kt");
}
@TestMetadata("createMangling.kt")
public void testCreateMangling() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/resumeWithException/createMangling.kt");
}
@TestMetadata("genericOverrideSuspendFun.kt")
public void testGenericOverrideSuspendFun() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/resumeWithException/genericOverrideSuspendFun.kt");
@@ -0,0 +1,22 @@
// WITH_RUNTIME
import kotlin.coroutines.*
fun builder(c: suspend () -> Unit) {
c.startCoroutine(Continuation(EmptyCoroutineContext) {
it.getOrThrow()
})
}
inline class IC(val s: String)
fun box(): String {
var res = "FAIL"
val lambda: suspend (IC, IC) -> String = { a, b ->
a.s + b.s
}
builder {
res = lambda(IC("O"), IC("K"))
}
return res
}
@@ -0,0 +1,29 @@
// WITH_RUNTIME
import kotlin.coroutines.*
fun builder(c: suspend () -> Unit) {
c.startCoroutine(Continuation(EmptyCoroutineContext) {
it.getOrThrow()
})
}
inline class IC(val s: String)
var c: Continuation<Any>? = null
var res = "FAIL"
fun box(): String {
val lambda: suspend (IC, IC) -> String = { _, _ ->
suspendCoroutine<String> {
@Suppress("UNCHECKED_CAST")
c = it as Continuation<Any>
}
}
builder {
res = lambda(IC("_"), IC("_"))
}
c?.resume("OK")
return res
}
@@ -0,0 +1,31 @@
// WITH_RUNTIME
// WITH_COROUTINES
import kotlin.coroutines.*
import helpers.*
var result = "FAIL"
fun builder(c: suspend () -> Unit) {
c.startCoroutine(handleExceptionContinuation {
result = it.message!!
})
}
inline class IC(val s: String)
var c: Continuation<Any>? = null
fun box(): String {
val lambda: suspend (IC, IC) -> String = { _, _ ->
suspendCoroutine<String> {
@Suppress("UNCHECKED_CAST")
c = it as Continuation<Any>
}
}
builder {
lambda(IC("O"), IC("K"))
}
c?.resumeWithException(IllegalStateException("OK"))
return result
}
@@ -8513,6 +8513,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/direct/covariantOverrideSuspendFun_Int.kt");
}
@TestMetadata("createMangling.kt")
public void testCreateMangling() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/direct/createMangling.kt");
}
@TestMetadata("genericOverrideSuspendFun.kt")
public void testGenericOverrideSuspendFun() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/direct/genericOverrideSuspendFun.kt");
@@ -8815,6 +8820,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/resume/covariantOverrideSuspendFun_Int.kt");
}
@TestMetadata("createMangling.kt")
public void testCreateMangling() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/resume/createMangling.kt");
}
@TestMetadata("genericOverrideSuspendFun.kt")
public void testGenericOverrideSuspendFun() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/resume/genericOverrideSuspendFun.kt");
@@ -9112,6 +9122,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/resumeWithException/covariantOverrideSuspendFun_Int.kt");
}
@TestMetadata("createMangling.kt")
public void testCreateMangling() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/resumeWithException/createMangling.kt");
}
@TestMetadata("genericOverrideSuspendFun.kt")
public void testGenericOverrideSuspendFun() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/resumeWithException/genericOverrideSuspendFun.kt");
@@ -8513,6 +8513,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/direct/covariantOverrideSuspendFun_Int.kt");
}
@TestMetadata("createMangling.kt")
public void testCreateMangling() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/direct/createMangling.kt");
}
@TestMetadata("genericOverrideSuspendFun.kt")
public void testGenericOverrideSuspendFun() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/direct/genericOverrideSuspendFun.kt");
@@ -8815,6 +8820,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/resume/covariantOverrideSuspendFun_Int.kt");
}
@TestMetadata("createMangling.kt")
public void testCreateMangling() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/resume/createMangling.kt");
}
@TestMetadata("genericOverrideSuspendFun.kt")
public void testGenericOverrideSuspendFun() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/resume/genericOverrideSuspendFun.kt");
@@ -9112,6 +9122,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/resumeWithException/covariantOverrideSuspendFun_Int.kt");
}
@TestMetadata("createMangling.kt")
public void testCreateMangling() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/resumeWithException/createMangling.kt");
}
@TestMetadata("genericOverrideSuspendFun.kt")
public void testGenericOverrideSuspendFun() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/resumeWithException/genericOverrideSuspendFun.kt");
@@ -7743,6 +7743,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/direct/covariantOverrideSuspendFun_Int.kt");
}
@TestMetadata("createMangling.kt")
public void testCreateMangling() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/direct/createMangling.kt");
}
@TestMetadata("genericOverrideSuspendFun.kt")
public void testGenericOverrideSuspendFun() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/direct/genericOverrideSuspendFun.kt");
@@ -7960,6 +7965,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/resume/covariantOverrideSuspendFun_Int.kt");
}
@TestMetadata("createMangling.kt")
public void testCreateMangling() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/resume/createMangling.kt");
}
@TestMetadata("genericOverrideSuspendFun.kt")
public void testGenericOverrideSuspendFun() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/resume/genericOverrideSuspendFun.kt");
@@ -8172,6 +8182,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/resumeWithException/covariantOverrideSuspendFun_Int.kt");
}
@TestMetadata("createMangling.kt")
public void testCreateMangling() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/resumeWithException/createMangling.kt");
}
@TestMetadata("genericOverrideSuspendFun.kt")
public void testGenericOverrideSuspendFun() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/resumeWithException/genericOverrideSuspendFun.kt");
@@ -6498,6 +6498,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/direct/covariantOverrideSuspendFun_Int.kt");
}
@TestMetadata("createMangling.kt")
public void testCreateMangling() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/direct/createMangling.kt");
}
@TestMetadata("genericOverrideSuspendFun.kt")
public void testGenericOverrideSuspendFun() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/direct/genericOverrideSuspendFun.kt");
@@ -6715,6 +6720,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/resume/covariantOverrideSuspendFun_Int.kt");
}
@TestMetadata("createMangling.kt")
public void testCreateMangling() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/resume/createMangling.kt");
}
@TestMetadata("genericOverrideSuspendFun.kt")
public void testGenericOverrideSuspendFun() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/resume/genericOverrideSuspendFun.kt");
@@ -6927,6 +6937,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/resumeWithException/covariantOverrideSuspendFun_Int.kt");
}
@TestMetadata("createMangling.kt")
public void testCreateMangling() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/resumeWithException/createMangling.kt");
}
@TestMetadata("genericOverrideSuspendFun.kt")
public void testGenericOverrideSuspendFun() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/resumeWithException/genericOverrideSuspendFun.kt");
@@ -6498,6 +6498,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/direct/covariantOverrideSuspendFun_Int.kt");
}
@TestMetadata("createMangling.kt")
public void testCreateMangling() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/direct/createMangling.kt");
}
@TestMetadata("genericOverrideSuspendFun.kt")
public void testGenericOverrideSuspendFun() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/direct/genericOverrideSuspendFun.kt");
@@ -6715,6 +6720,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/resume/covariantOverrideSuspendFun_Int.kt");
}
@TestMetadata("createMangling.kt")
public void testCreateMangling() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/resume/createMangling.kt");
}
@TestMetadata("genericOverrideSuspendFun.kt")
public void testGenericOverrideSuspendFun() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/resume/genericOverrideSuspendFun.kt");
@@ -6927,6 +6937,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/resumeWithException/covariantOverrideSuspendFun_Int.kt");
}
@TestMetadata("createMangling.kt")
public void testCreateMangling() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/resumeWithException/createMangling.kt");
}
@TestMetadata("genericOverrideSuspendFun.kt")
public void testGenericOverrideSuspendFun() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/resumeWithException/genericOverrideSuspendFun.kt");
@@ -6498,6 +6498,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/direct/covariantOverrideSuspendFun_Int.kt");
}
@TestMetadata("createMangling.kt")
public void testCreateMangling() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/direct/createMangling.kt");
}
@TestMetadata("genericOverrideSuspendFun.kt")
public void testGenericOverrideSuspendFun() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/direct/genericOverrideSuspendFun.kt");
@@ -6715,6 +6720,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/resume/covariantOverrideSuspendFun_Int.kt");
}
@TestMetadata("createMangling.kt")
public void testCreateMangling() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/resume/createMangling.kt");
}
@TestMetadata("genericOverrideSuspendFun.kt")
public void testGenericOverrideSuspendFun() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/resume/genericOverrideSuspendFun.kt");
@@ -6927,6 +6937,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/resumeWithException/covariantOverrideSuspendFun_Int.kt");
}
@TestMetadata("createMangling.kt")
public void testCreateMangling() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/resumeWithException/createMangling.kt");
}
@TestMetadata("genericOverrideSuspendFun.kt")
public void testGenericOverrideSuspendFun() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/inlineClasses/resumeWithException/genericOverrideSuspendFun.kt");