[JS IR] Improve debug info for suspend functions

- Map generated explicit Unit returns to the closing brace of
  the original body
- Name the continuation parameter as `$completion` to match the JVM BE,
  and generate debug info for it (so that it appears in the 'names'
  array in sourcemaps)
- Don't generate debug info for coroutine instantiation ceremony
  (so that the user doesn't need to step in many times to get where they
  want)

#KT-46276
This commit is contained in:
Sergej Jaskiewicz
2022-10-25 15:37:10 +02:00
committed by Space Team
parent 4d9c2d3d14
commit ec18dce7cb
6 changed files with 56 additions and 29 deletions
@@ -27,6 +27,7 @@ import org.jetbrains.kotlin.ir.types.typeWith
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
import org.jetbrains.kotlin.name.Name
/**
* Replaces suspend functions with regular non-suspend functions with additional
@@ -78,7 +79,13 @@ private fun transformSuspendFunction(context: CommonBackendContext, function: Ir
newBody.statements.lastOrNull() !is IrReturn
) {
// Adding explicit return of Unit.
newBody.statements += context.createIrBuilder(newFunctionWithContinuation.symbol).irReturnUnit()
// Set both offsets of the IrReturn to body.endOffset - 1 so that a breakpoint set at the closing brace of a lambda expression
// could be hit.
newBody.statements += context.createIrBuilder(
newFunctionWithContinuation.symbol,
startOffset = newBody.endOffset - 1,
endOffset = newBody.endOffset - 1
).irReturnUnit()
}
newFunctionWithContinuation.body = newBody
@@ -123,11 +130,13 @@ private fun IrSimpleFunction.createSuspendFunctionStub(context: CommonBackendCon
val remapper = ValueRemapper(mapping)
function.valueParameters.forEach { it.defaultValue = it.defaultValue?.transform(remapper, null) }
function.addValueParameter(
"\$cont",
continuationType(context).substitute(substitutionMap),
IrDeclarationOrigin.CONTINUATION
)
function.addValueParameter {
startOffset = function.startOffset
endOffset = function.endOffset
origin = IrDeclarationOrigin.CONTINUATION
name = Name.identifier("\$completion")
type = continuationType(context).substitute(substitutionMap)
}
}
}
@@ -136,4 +145,4 @@ private fun IrFunction.continuationType(context: CommonBackendContext): IrType {
}
fun loweredSuspendFunctionReturnType(function: IrFunction, irBuiltIns: IrBuiltIns): IrType =
if (function.returnType.isNullable()) irBuiltIns.anyNType else irBuiltIns.anyType
if (function.returnType.isNullable()) irBuiltIns.anyNType else irBuiltIns.anyType