Skip to content

Commit 300d865

Browse files
JSMonkSpace Team
authored andcommitted
[K/JS] Remove the possibility to have negative lines and columns in Source Maps
1 parent 6199bd2 commit 300d865

File tree

4 files changed

+28
-11
lines changed

4 files changed

+28
-11
lines changed

compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/coroutines/AddContinuationToFunctionsLowering.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ private fun transformSuspendFunction(context: CommonBackendContext, function: Ir
7979
newBody.statements.lastOrNull() !is IrReturn
8080
) {
8181
// Adding explicit return of Unit.
82-
// Set both offsets of the IrReturn to body.endOffset - 1 so that a breakpoint set at the closing brace of a lambda expression
83-
// could be hit.
82+
// Set both offsets of the IrReturn to body.endOffset.previousOffset (check the description of the `previousOffset` method)
83+
// so that a breakpoint set at the closing brace of a lambda expression could be hit.
8484
newBody.statements += context.createIrBuilder(
8585
newFunctionWithContinuation.symbol,
86-
startOffset = newBody.endOffset - 1,
87-
endOffset = newBody.endOffset - 1
86+
startOffset = newBody.endOffset.previousOffset,
87+
endOffset = newBody.endOffset.previousOffset
8888
).irReturnUnit()
8989
}
9090

compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/coroutines/AbstractSuspendFunctionsLowering.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -487,9 +487,13 @@ abstract class AbstractSuspendFunctionsLowering<C : CommonBackendContext>(val co
487487
else delegatingCall
488488
val body = irFunction.body as IrBlockBody
489489

490-
// Set both offsets to body.endOffset - 1 so that a breakpoint set at the closing brace of a lambda expression
491-
// could be hit.
492-
context.createIrBuilder(irFunction.symbol, startOffset = body.endOffset - 1, endOffset = body.endOffset - 1).run {
490+
// Set both offsets to body.endOffset.previousOffset (check the description of the `previousOffset` method)
491+
// so that a breakpoint set at the closing brace of a lambda expression could be hit.
492+
context.createIrBuilder(
493+
irFunction.symbol,
494+
startOffset = body.endOffset.previousOffset,
495+
endOffset = body.endOffset.previousOffset
496+
).run {
493497
val statements = body.statements
494498
val lastStatement = statements.last()
495499
assert(lastStatement == delegatingCall || lastStatement is IrReturn) { "Unexpected statement $lastStatement" }

compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/coroutines/StateMachineBuilder.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import org.jetbrains.kotlin.ir.types.isUnit
2929
import org.jetbrains.kotlin.ir.util.deepCopyWithSymbols
3030
import org.jetbrains.kotlin.ir.util.isElseBranch
3131
import org.jetbrains.kotlin.ir.util.isSuspend
32+
import org.jetbrains.kotlin.ir.util.previousOffset
3233
import org.jetbrains.kotlin.ir.visitors.*
3334

3435
class SuspendState(type: IrType) {
@@ -96,13 +97,13 @@ class StateMachineBuilder(
9697
fun finalizeStateMachine() {
9798
globalCatch = buildGlobalCatch()
9899
if (currentBlock.statements.lastOrNull() !is IrReturn) {
99-
// Set both offsets to rootLoop.endOffset - 1 so that a breakpoint set at the closing brace of a lambda expression
100-
// could be hit.
100+
// Set both offsets to rootLoop.endOffset.previousOffset (check the description of the `previousOffset` method)
101+
// so that a breakpoint set at the closing brace of a lambda expression could be hit.
101102
// NOTE: rootLoop's offsets are the same as in the original function.
102103
addStatement(
103104
IrReturnImpl(
104-
startOffset = rootLoop.endOffset - 1,
105-
endOffset = rootLoop.endOffset - 1,
105+
startOffset = rootLoop.endOffset.previousOffset,
106+
endOffset = rootLoop.endOffset.previousOffset,
106107
nothing,
107108
function,
108109
unitValue

compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/IrUtils.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,3 +1369,15 @@ val IrFunction.isValueClassTypedEquals: Boolean
13691369
&& contextReceiverParametersCount == 0 && extensionReceiverParameter == null
13701370
&& (parentClass.isValue)
13711371
}
1372+
1373+
/**
1374+
* The method is used to calculate the previous offset from the current one to prevent situations when it can calculate
1375+
* [UNDEFINED_OFFSET] from 0 offset and -2 offset from the [UNDEFINED OFFSET]
1376+
*/
1377+
val Int.previousOffset
1378+
get(): Int =
1379+
when (compareTo(0)) {
1380+
0 -> 0
1381+
-1 -> UNDEFINED_OFFSET
1382+
else -> minus(1)
1383+
}

0 commit comments

Comments
 (0)