[JS IR BE] Fix reusing expression instances

This commit is contained in:
Svyatoslav Kuzmich
2018-10-19 21:54:50 +03:00
parent 47b0f94903
commit 38b31e8f7d
2 changed files with 20 additions and 12 deletions
@@ -79,14 +79,17 @@ class MultipleCatchesLowering(val context: JsIrBackendContext) : FileLoweringPas
val type = catch.catchParameter.type val type = catch.catchParameter.type
val typeSymbol = type.classifierOrNull val typeSymbol = type.classifierOrNull
val castedPendingException = if (type !is IrDynamicType) val castedPendingException = {
buildImplicitCast(pendingException, type, typeSymbol!!) if (type !is IrDynamicType)
else pendingException buildImplicitCast(pendingException, type, typeSymbol!!)
else
pendingException
}
val catchBody = catch.result.transform(object : IrElementTransformer<IrValueSymbol> { val catchBody = catch.result.transform(object : IrElementTransformer<IrValueSymbol> {
override fun visitGetValue(expression: IrGetValue, data: IrValueSymbol) = override fun visitGetValue(expression: IrGetValue, data: IrValueSymbol) =
if (expression.symbol == data) if (expression.symbol == data)
castedPendingException castedPendingException()
else else
expression expression
}, catch.catchParameter.symbol) }, catch.catchParameter.symbol)
@@ -22,7 +22,11 @@ import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.IrReturnableBlockSymbol import org.jetbrains.kotlin.ir.symbols.IrReturnableBlockSymbol
import org.jetbrains.kotlin.ir.symbols.IrValueParameterSymbol import org.jetbrains.kotlin.ir.symbols.IrValueParameterSymbol
import org.jetbrains.kotlin.ir.symbols.IrVariableSymbol 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.* import org.jetbrains.kotlin.ir.visitors.*
class SuspendState(type: IrType) { class SuspendState(type: IrType) {
@@ -62,7 +66,7 @@ class StateMachineBuilder(
private val exceptionSymbol: IrProperty, private val exceptionSymbol: IrProperty,
private val exStateSymbol: IrProperty, private val exStateSymbol: IrProperty,
private val stateSymbol: IrProperty, private val stateSymbol: IrProperty,
thisSymbol: IrValueParameterSymbol, private val thisSymbol: IrValueParameterSymbol,
private val suspendResult: IrVariableSymbol private val suspendResult: IrVariableSymbol
) : IrElementVisitorVoid { ) : IrElementVisitorVoid {
@@ -73,7 +77,7 @@ class StateMachineBuilder(
private val booleanNotSymbol = context.irBuiltIns.booleanNotSymbol private val booleanNotSymbol = context.irBuiltIns.booleanNotSymbol
private val eqeqeqSymbol = context.irBuiltIns.eqeqeqSymbol private val eqeqeqSymbol = context.irBuiltIns.eqeqeqSymbol
private val thisReceiver = JsIrBuilder.buildGetValue(thisSymbol) private val thisReceiver get() = JsIrBuilder.buildGetValue(thisSymbol)
private var hasExceptions = false private var hasExceptions = false
@@ -454,7 +458,9 @@ class StateMachineBuilder(
irVar.apply { initializer = it } irVar.apply { initializer = it }
} }
JsIrBuilder.buildGetValue(irVar.symbol) JsIrBuilder.buildGetValue(irVar.symbol)
} else arg } else {
arg?.deepCopyWithSymbols(function.owner)
}
} }
return newArguments return newArguments
@@ -606,13 +612,12 @@ class StateMachineBuilder(
setupExceptionState(enclosingCatch) setupExceptionState(enclosingCatch)
} }
val ex = pendingException()
var rethrowNeeded = true var rethrowNeeded = true
for (catch in aTry.catches) { for (catch in aTry.catches) {
val type = catch.catchParameter.type 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 { val irVar = catch.catchParameter.also {
it.initializer = initializer it.initializer = initializer
} }
@@ -631,7 +636,7 @@ class StateMachineBuilder(
maybeDoDispatch(exitDispatch) maybeDoDispatch(exitDispatch)
} else { } else {
val check = buildIsCheck(ex, type) val check = buildIsCheck(pendingException(), type)
val branchBlock = JsIrBuilder.buildComposite(catchResult.type) val branchBlock = JsIrBuilder.buildComposite(catchResult.type)
@@ -654,7 +659,7 @@ class StateMachineBuilder(
if (rethrowNeeded) { if (rethrowNeeded) {
addExceptionEdge() addExceptionEdge()
addStatement(JsIrBuilder.buildThrow(nothing, ex)) addStatement(JsIrBuilder.buildThrow(nothing, pendingException()))
} }
if (tryState.finallyState == null) { if (tryState.finallyState == null) {