Fix StackOverflow in interpreter for case with recursive enum init
#KT-54884 Fixed
This commit is contained in:
+6
@@ -76,6 +76,12 @@ public class LoweredIrInterpreterTestGenerated extends AbstractLoweredIrInterpre
|
|||||||
runTest("compiler/testData/ir/loweredIr/interpreter/enumName.kt");
|
runTest("compiler/testData/ir/loweredIr/interpreter/enumName.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@TestMetadata("enumRecursiveName.kt")
|
||||||
|
public void testEnumRecursiveName() throws Exception {
|
||||||
|
runTest("compiler/testData/ir/loweredIr/interpreter/enumRecursiveName.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestMetadata("floatOperations.kt")
|
@TestMetadata("floatOperations.kt")
|
||||||
public void testFloatOperations() throws Exception {
|
public void testFloatOperations() throws Exception {
|
||||||
|
|||||||
+10
-5
@@ -31,8 +31,9 @@ class IrCompileTimeChecker(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun <R> IrCall.saveContext(block: () -> R): R {
|
private fun <R> IrCall.saveContext(block: () -> R): R {
|
||||||
|
val oldContext = contextExpression
|
||||||
contextExpression = this
|
contextExpression = this
|
||||||
return block().apply { contextExpression = null }
|
return block().apply { contextExpression = oldContext }
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun visitElement(element: IrElement, data: Nothing?) = false
|
override fun visitElement(element: IrElement, data: Nothing?) = false
|
||||||
@@ -53,7 +54,7 @@ class IrCompileTimeChecker(
|
|||||||
|
|
||||||
private fun visitConstructor(expression: IrFunctionAccessExpression): Boolean {
|
private fun visitConstructor(expression: IrFunctionAccessExpression): Boolean {
|
||||||
return when {
|
return when {
|
||||||
!visitValueParameters(expression, null) || !mode.canEvaluateFunction(expression.symbol.owner, contextExpression) -> false
|
!visitValueArguments(expression, null) || !mode.canEvaluateFunction(expression.symbol.owner, contextExpression) -> false
|
||||||
else -> expression.symbol.owner.visitBodyIfNeeded()
|
else -> expression.symbol.owner.visitBodyIfNeeded()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -69,7 +70,7 @@ class IrCompileTimeChecker(
|
|||||||
return expression.saveContext {
|
return expression.saveContext {
|
||||||
val dispatchReceiverComputable = expression.dispatchReceiver?.accept(this, null) ?: true
|
val dispatchReceiverComputable = expression.dispatchReceiver?.accept(this, null) ?: true
|
||||||
val extensionReceiverComputable = expression.extensionReceiver?.accept(this, null) ?: true
|
val extensionReceiverComputable = expression.extensionReceiver?.accept(this, null) ?: true
|
||||||
if (!visitValueParameters(expression, null)) return@saveContext false
|
if (!visitValueArguments(expression, null)) return@saveContext false
|
||||||
val bodyComputable = owner.visitBodyIfNeeded()
|
val bodyComputable = owner.visitBodyIfNeeded()
|
||||||
return@saveContext dispatchReceiverComputable && extensionReceiverComputable && bodyComputable
|
return@saveContext dispatchReceiverComputable && extensionReceiverComputable && bodyComputable
|
||||||
}
|
}
|
||||||
@@ -79,7 +80,7 @@ class IrCompileTimeChecker(
|
|||||||
return declaration.initializer?.accept(this, data) ?: true
|
return declaration.initializer?.accept(this, data) ?: true
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun visitValueParameters(expression: IrFunctionAccessExpression, data: Nothing?): Boolean {
|
private fun visitValueArguments(expression: IrFunctionAccessExpression, data: Nothing?): Boolean {
|
||||||
return (0 until expression.valueArgumentsCount)
|
return (0 until expression.valueArgumentsCount)
|
||||||
.map { expression.getValueArgument(it) }
|
.map { expression.getValueArgument(it) }
|
||||||
.none { it?.accept(this, data) == false }
|
.none { it?.accept(this, data) == false }
|
||||||
@@ -151,7 +152,11 @@ class IrCompileTimeChecker(
|
|||||||
|
|
||||||
override fun visitGetEnumValue(expression: IrGetEnumValue, data: Nothing?): Boolean {
|
override fun visitGetEnumValue(expression: IrGetEnumValue, data: Nothing?): Boolean {
|
||||||
if (!mode.canEvaluateEnumValue(expression, contextExpression)) return false
|
if (!mode.canEvaluateEnumValue(expression, contextExpression)) return false
|
||||||
return expression.symbol.owner.initializerExpression?.accept(this, data) == true
|
// we want to avoid recursion in cases like "enum class E(val srt: String) { OK(OK.name) }"
|
||||||
|
if (visitedStack.contains(expression)) return true
|
||||||
|
return expression.asVisited {
|
||||||
|
expression.symbol.owner.initializerExpression?.accept(this, data) == true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun visitGetValue(expression: IrGetValue, data: Nothing?): Boolean {
|
override fun visitGetValue(expression: IrGetValue, data: Nothing?): Boolean {
|
||||||
|
|||||||
+14
-5
@@ -7,14 +7,12 @@ package org.jetbrains.kotlin.ir.interpreter.checker
|
|||||||
|
|
||||||
import org.jetbrains.kotlin.ir.IrElement
|
import org.jetbrains.kotlin.ir.IrElement
|
||||||
import org.jetbrains.kotlin.ir.IrStatement
|
import org.jetbrains.kotlin.ir.IrStatement
|
||||||
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
|
|
||||||
import org.jetbrains.kotlin.ir.declarations.IrAnnotationContainer
|
import org.jetbrains.kotlin.ir.declarations.IrAnnotationContainer
|
||||||
import org.jetbrains.kotlin.ir.declarations.IrDeclarationBase
|
import org.jetbrains.kotlin.ir.declarations.IrDeclarationBase
|
||||||
import org.jetbrains.kotlin.ir.declarations.IrField
|
import org.jetbrains.kotlin.ir.declarations.IrField
|
||||||
import org.jetbrains.kotlin.ir.declarations.IrFile
|
import org.jetbrains.kotlin.ir.declarations.IrFile
|
||||||
import org.jetbrains.kotlin.ir.expressions.*
|
import org.jetbrains.kotlin.ir.expressions.*
|
||||||
import org.jetbrains.kotlin.ir.expressions.impl.IrConstImpl
|
import org.jetbrains.kotlin.ir.expressions.impl.IrConstImpl
|
||||||
import org.jetbrains.kotlin.ir.expressions.impl.IrErrorExpressionImpl
|
|
||||||
import org.jetbrains.kotlin.ir.expressions.impl.IrVarargImpl
|
import org.jetbrains.kotlin.ir.expressions.impl.IrVarargImpl
|
||||||
import org.jetbrains.kotlin.ir.interpreter.IrInterpreter
|
import org.jetbrains.kotlin.ir.interpreter.IrInterpreter
|
||||||
import org.jetbrains.kotlin.ir.interpreter.isPrimitiveArray
|
import org.jetbrains.kotlin.ir.interpreter.isPrimitiveArray
|
||||||
@@ -51,6 +49,17 @@ class IrConstTransformer(
|
|||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun IrExpression.canBeInterpreted(containingDeclaration: IrElement? = null): Boolean {
|
||||||
|
return try {
|
||||||
|
this.accept(IrCompileTimeChecker(containingDeclaration, mode = mode), null)
|
||||||
|
} catch (e: Throwable) {
|
||||||
|
if (suppressExceptions) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
throw AssertionError("Error occurred while optimizing an expression:\n${this.dump()}", e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun IrExpression.interpret(failAsError: Boolean): IrExpression {
|
private fun IrExpression.interpret(failAsError: Boolean): IrExpression {
|
||||||
val result = try {
|
val result = try {
|
||||||
interpreter.interpret(this, irFile)
|
interpreter.interpret(this, irFile)
|
||||||
@@ -65,7 +74,7 @@ class IrConstTransformer(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun visitCall(expression: IrCall): IrExpression {
|
override fun visitCall(expression: IrCall): IrExpression {
|
||||||
if (expression.accept(IrCompileTimeChecker(mode = mode), null)) {
|
if (expression.canBeInterpreted()) {
|
||||||
return expression.interpret(failAsError = false)
|
return expression.interpret(failAsError = false)
|
||||||
}
|
}
|
||||||
return super.visitCall(expression)
|
return super.visitCall(expression)
|
||||||
@@ -78,7 +87,7 @@ class IrConstTransformer(
|
|||||||
val expression = initializer?.expression ?: return declaration
|
val expression = initializer?.expression ?: return declaration
|
||||||
if (expression is IrConst<*>) return declaration
|
if (expression is IrConst<*>) return declaration
|
||||||
val isConst = declaration.correspondingPropertySymbol?.owner?.isConst == true
|
val isConst = declaration.correspondingPropertySymbol?.owner?.isConst == true
|
||||||
if (isConst && expression.accept(IrCompileTimeChecker(declaration, mode), null)) {
|
if (isConst && expression.canBeInterpreted(declaration)) {
|
||||||
initializer.expression = expression.interpret(failAsError = true)
|
initializer.expression = expression.interpret(failAsError = true)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -124,7 +133,7 @@ class IrConstTransformer(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun IrExpression.transformSingleArg(expectedType: IrType): IrExpression {
|
private fun IrExpression.transformSingleArg(expectedType: IrType): IrExpression {
|
||||||
if (this.accept(IrCompileTimeChecker(mode = mode), null)) {
|
if (this.canBeInterpreted()) {
|
||||||
return this.interpret(failAsError = true).convertToConstIfPossible(expectedType)
|
return this.interpret(failAsError = true).convertToConstIfPossible(expectedType)
|
||||||
} else if (this is IrConstructorCall) {
|
} else if (this is IrConstructorCall) {
|
||||||
transformAnnotation(this)
|
transformAnnotation(this)
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
// TARGET_BACKEND: JVM
|
||||||
|
// IGNORE_FIR_DIAGNOSTICS
|
||||||
|
// !DIAGNOSTICS: -UNINITIALIZED_ENUM_ENTRY
|
||||||
|
|
||||||
|
enum class TestEnum(val testNaming: String) {
|
||||||
|
OK(OK.name),
|
||||||
|
}
|
||||||
|
|
||||||
|
const val name = TestEnum.OK.name
|
||||||
|
|
||||||
|
fun box(): String {
|
||||||
|
return name
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user