[K/JS] Remove the possibility to have negative lines and columns in Source Maps

This commit is contained in:
Artem Kobzar
2023-03-06 16:55:26 +00:00
committed by Space Team
parent f2c1145462
commit 26f6661d1e
4 changed files with 29 additions and 12 deletions
@@ -79,12 +79,12 @@ private fun transformSuspendFunction(context: CommonBackendContext, function: Ir
newBody.statements.lastOrNull() !is IrReturn newBody.statements.lastOrNull() !is IrReturn
) { ) {
// Adding explicit return of Unit. // Adding explicit return of Unit.
// Set both offsets of the IrReturn to body.endOffset - 1 so that a breakpoint set at the closing brace of a lambda expression // Set both offsets of the IrReturn to body.endOffset.previousOffset (check the description of the `previousOffset` method)
// could be hit. // so that a breakpoint set at the closing brace of a lambda expression could be hit.
newBody.statements += context.createIrBuilder( newBody.statements += context.createIrBuilder(
newFunctionWithContinuation.symbol, newFunctionWithContinuation.symbol,
startOffset = newBody.endOffset - 1, startOffset = newBody.endOffset.previousOffset,
endOffset = newBody.endOffset - 1 endOffset = newBody.endOffset.previousOffset
).irReturnUnit() ).irReturnUnit()
} }
@@ -487,9 +487,13 @@ abstract class AbstractSuspendFunctionsLowering<C : CommonBackendContext>(val co
else delegatingCall else delegatingCall
val body = irFunction.body as IrBlockBody val body = irFunction.body as IrBlockBody
// Set both offsets to body.endOffset - 1 so that a breakpoint set at the closing brace of a lambda expression // Set both offsets to body.endOffset.previousOffset (check the description of the `previousOffset` method)
// could be hit. // so that a breakpoint set at the closing brace of a lambda expression could be hit.
context.createIrBuilder(irFunction.symbol, startOffset = body.endOffset - 1, endOffset = body.endOffset - 1).run { context.createIrBuilder(
irFunction.symbol,
startOffset = body.endOffset.previousOffset,
endOffset = body.endOffset.previousOffset
).run {
val statements = body.statements val statements = body.statements
val lastStatement = statements.last() val lastStatement = statements.last()
assert(lastStatement == delegatingCall || lastStatement is IrReturn) { "Unexpected statement $lastStatement" } assert(lastStatement == delegatingCall || lastStatement is IrReturn) { "Unexpected statement $lastStatement" }
@@ -29,6 +29,7 @@ import org.jetbrains.kotlin.ir.types.isUnit
import org.jetbrains.kotlin.ir.util.deepCopyWithSymbols import org.jetbrains.kotlin.ir.util.deepCopyWithSymbols
import org.jetbrains.kotlin.ir.util.isElseBranch import org.jetbrains.kotlin.ir.util.isElseBranch
import org.jetbrains.kotlin.ir.util.isSuspend import org.jetbrains.kotlin.ir.util.isSuspend
import org.jetbrains.kotlin.ir.util.previousOffset
import org.jetbrains.kotlin.ir.visitors.* import org.jetbrains.kotlin.ir.visitors.*
class SuspendState(type: IrType) { class SuspendState(type: IrType) {
@@ -96,13 +97,13 @@ class StateMachineBuilder(
fun finalizeStateMachine() { fun finalizeStateMachine() {
globalCatch = buildGlobalCatch() globalCatch = buildGlobalCatch()
if (currentBlock.statements.lastOrNull() !is IrReturn) { if (currentBlock.statements.lastOrNull() !is IrReturn) {
// Set both offsets to rootLoop.endOffset - 1 so that a breakpoint set at the closing brace of a lambda expression // Set both offsets to rootLoop.endOffset.previousOffset (check the description of the `previousOffset` method)
// could be hit. // so that a breakpoint set at the closing brace of a lambda expression could be hit.
// NOTE: rootLoop's offsets are the same as in the original function. // NOTE: rootLoop's offsets are the same as in the original function.
addStatement( addStatement(
IrReturnImpl( IrReturnImpl(
startOffset = rootLoop.endOffset - 1, startOffset = rootLoop.endOffset.previousOffset,
endOffset = rootLoop.endOffset - 1, endOffset = rootLoop.endOffset.previousOffset,
nothing, nothing,
function, function,
unitValue unitValue
@@ -1395,4 +1395,16 @@ fun IrFunction.getAdapteeFromAdaptedForReferenceFunction() : IrFunction? {
if (call is IrReturnableBlock) return (call.inlineFunctionSymbol ?: unknownStructure()).owner if (call is IrReturnableBlock) return (call.inlineFunctionSymbol ?: unknownStructure()).owner
if (call !is IrFunctionAccessExpression) { unknownStructure() } if (call !is IrFunctionAccessExpression) { unknownStructure() }
return call.symbol.owner return call.symbol.owner
} }
/**
* The method is used to calculate the previous offset from the current one to prevent situations when it can calculate
* [UNDEFINED_OFFSET] from 0 offset and -2 offset from the [UNDEFINED OFFSET]
*/
val Int.previousOffset
get(): Int =
when (compareTo(0)) {
0 -> 0
-1 -> UNDEFINED_OFFSET
else -> minus(1)
}