From 38b31e8f7ddf544361f5cfc90bd16226d124e4d8 Mon Sep 17 00:00:00 2001 From: Svyatoslav Kuzmich Date: Fri, 19 Oct 2018 21:54:50 +0300 Subject: [PATCH] [JS IR BE] Fix reusing expression instances --- .../js/lower/MultipleCatchesLowering.kt | 11 ++++++---- .../lower/coroutines/StateMachineBuilder.kt | 21 ++++++++++++------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/MultipleCatchesLowering.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/MultipleCatchesLowering.kt index 7a0892ceae4..90ef41fabc2 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/MultipleCatchesLowering.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/MultipleCatchesLowering.kt @@ -79,14 +79,17 @@ class MultipleCatchesLowering(val context: JsIrBackendContext) : FileLoweringPas val type = catch.catchParameter.type val typeSymbol = type.classifierOrNull - val castedPendingException = if (type !is IrDynamicType) - buildImplicitCast(pendingException, type, typeSymbol!!) - else pendingException + val castedPendingException = { + if (type !is IrDynamicType) + buildImplicitCast(pendingException, type, typeSymbol!!) + else + pendingException + } val catchBody = catch.result.transform(object : IrElementTransformer { override fun visitGetValue(expression: IrGetValue, data: IrValueSymbol) = if (expression.symbol == data) - castedPendingException + castedPendingException() else expression }, catch.catchParameter.symbol) diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/coroutines/StateMachineBuilder.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/coroutines/StateMachineBuilder.kt index bebe4ebddd5..41ac9e4390d 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/coroutines/StateMachineBuilder.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/coroutines/StateMachineBuilder.kt @@ -22,7 +22,11 @@ import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol import org.jetbrains.kotlin.ir.symbols.IrReturnableBlockSymbol import org.jetbrains.kotlin.ir.symbols.IrValueParameterSymbol import org.jetbrains.kotlin.ir.symbols.IrVariableSymbol -import org.jetbrains.kotlin.ir.types.* +import org.jetbrains.kotlin.ir.types.IrDynamicType +import org.jetbrains.kotlin.ir.types.IrType +import org.jetbrains.kotlin.ir.types.classifierOrNull +import org.jetbrains.kotlin.ir.types.isNothing +import org.jetbrains.kotlin.ir.util.deepCopyWithSymbols import org.jetbrains.kotlin.ir.visitors.* class SuspendState(type: IrType) { @@ -62,7 +66,7 @@ class StateMachineBuilder( private val exceptionSymbol: IrProperty, private val exStateSymbol: IrProperty, private val stateSymbol: IrProperty, - thisSymbol: IrValueParameterSymbol, + private val thisSymbol: IrValueParameterSymbol, private val suspendResult: IrVariableSymbol ) : IrElementVisitorVoid { @@ -73,7 +77,7 @@ class StateMachineBuilder( private val booleanNotSymbol = context.irBuiltIns.booleanNotSymbol private val eqeqeqSymbol = context.irBuiltIns.eqeqeqSymbol - private val thisReceiver = JsIrBuilder.buildGetValue(thisSymbol) + private val thisReceiver get() = JsIrBuilder.buildGetValue(thisSymbol) private var hasExceptions = false @@ -454,7 +458,9 @@ class StateMachineBuilder( irVar.apply { initializer = it } } JsIrBuilder.buildGetValue(irVar.symbol) - } else arg + } else { + arg?.deepCopyWithSymbols(function.owner) + } } return newArguments @@ -606,13 +612,12 @@ class StateMachineBuilder( setupExceptionState(enclosingCatch) } - val ex = pendingException() var rethrowNeeded = true for (catch in aTry.catches) { val type = catch.catchParameter.type - val initializer = if (type !is IrDynamicType) implicitCast(ex, type) else ex + val initializer = if (type !is IrDynamicType) implicitCast(pendingException(), type) else pendingException() val irVar = catch.catchParameter.also { it.initializer = initializer } @@ -631,7 +636,7 @@ class StateMachineBuilder( maybeDoDispatch(exitDispatch) } else { - val check = buildIsCheck(ex, type) + val check = buildIsCheck(pendingException(), type) val branchBlock = JsIrBuilder.buildComposite(catchResult.type) @@ -654,7 +659,7 @@ class StateMachineBuilder( if (rethrowNeeded) { addExceptionEdge() - addStatement(JsIrBuilder.buildThrow(nothing, ex)) + addStatement(JsIrBuilder.buildThrow(nothing, pendingException())) } if (tryState.finallyState == null) {