Support early return from constructor interpretation

This commit is contained in:
Ivan Kylchik
2021-06-23 17:28:48 +03:00
committed by TeamCityServer
parent 96cc74a752
commit 0c5fca31ec
4 changed files with 25 additions and 13 deletions
@@ -319,11 +319,7 @@ class IrInterpreter(internal val environment: IrInterpreterEnvironment, internal
}
private fun interpretReturn(expression: IrReturn) {
val function = expression.returnTargetSymbol.owner as? IrFunction
function?.tryResetFunctionBody()
if (function.checkCast(environment)) {
callStack.returnFromFrameWithResult(expression)
}
callStack.returnFromFrameWithResult(expression)
}
private fun interpretWhile(loop: IrWhileLoop) {
@@ -6,6 +6,7 @@
package org.jetbrains.kotlin.ir.interpreter.stack
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.declarations.IrConstructor
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.declarations.IrFunction
import org.jetbrains.kotlin.ir.expressions.*
@@ -55,8 +56,9 @@ internal class CallStack {
fun returnFromFrameWithResult(irReturn: IrReturn) {
val result = popState()
val returnTarget = irReturn.returnTargetSymbol.owner
var frameOwner = currentFrameOwner
while (frameOwner != irReturn.returnTargetSymbol.owner) {
while (frameOwner != returnTarget) {
when (frameOwner) {
is IrTry -> {
dropSubFrame()
@@ -75,16 +77,15 @@ internal class CallStack {
}
else -> {
dropSubFrame()
if (currentFrame.hasNoSubFrames() && frameOwner != irReturn.returnTargetSymbol.owner) dropFrame()
if (currentFrame.hasNoSubFrames() && frameOwner != returnTarget) dropFrame()
frameOwner = currentFrameOwner
}
}
}
dropFrame()
// check that last frame is not a function itself; use case for proxyInterpret
if (frames.size == 0) newFrame(irReturn) // just stub frame
pushState(result)
currentFrame.dropInstructions()
addInstruction(SimpleInstruction(returnTarget))
if (returnTarget !is IrConstructor) pushState(result)
}
fun unrollInstructionsForBreakContinue(breakOrContinue: IrBreakContinue) {
@@ -47,13 +47,13 @@ const val b2 = <!EVALUATED: `2`!>safeClassCast("10")<!>
const val c1 = <!EVALUATED: `1`!>unsafeClassCast<Int>()<!>
const val c2 = <!WAS_NOT_EVALUATED: `
Exception java.lang.ClassCastException: kotlin.Int cannot be cast to kotlin.String
at ClassCastExceptionKt.unsafeClassCast(classCastException.kt:21)
at ClassCastExceptionKt.unsafeClassCast(classCastException.kt:19)
at ClassCastExceptionKt.<clinit>(classCastException.kt:48)`!>unsafeClassCast<String>()<!>
const val d1 = A<Int>().<!EVALUATED: `1`!>unsafeCast()<!>
const val d2 = A<String>().<!WAS_NOT_EVALUATED: `
Exception java.lang.ClassCastException: kotlin.Int cannot be cast to kotlin.String
at ClassCastExceptionKt.A.unsafeCast(classCastException.kt:36)
at ClassCastExceptionKt.A.unsafeCast(classCastException.kt:35)
at ClassCastExceptionKt.<clinit>(classCastException.kt:51)`!>unsafeCast()<!>
const val stringList = getIntList<List<String>>().<!WAS_NOT_EVALUATED: `
@@ -31,3 +31,18 @@ const val c2 = Person().<!EVALUATED: `<NOT_GIVEN> <NULL>`!>wholeName<!>
const val d1 = Person("Ivan", 20).<!EVALUATED: `20`!>age<!>
const val d2 = Person("Ivan", 20).<!EVALUATED: `Ivan <NULL>`!>wholeName<!>
@CompileTimeCalculation
class A {
val prop: Int
constructor(arg: Boolean) {
if (arg) {
prop = 1
return
}
prop = 2
}
}
const val e1 = A(true).<!EVALUATED: `1`!>prop<!>
const val e2 = A(false).<!EVALUATED: `2`!>prop<!>