From 2c386854860769753b094e50673baff744744fab Mon Sep 17 00:00:00 2001 From: Ivan Kylchik Date: Wed, 15 Nov 2023 15:43:13 +0100 Subject: [PATCH] [IR] Report backend error if annotation's arg can't be evaluated Just a safety measure to ensure that we will not produce the wrong code. --- .../transformer/IrConstAnnotationTransformer.kt | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/transformer/IrConstAnnotationTransformer.kt b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/transformer/IrConstAnnotationTransformer.kt index ef02e1746eb..7410099f348 100644 --- a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/transformer/IrConstAnnotationTransformer.kt +++ b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/transformer/IrConstAnnotationTransformer.kt @@ -17,8 +17,10 @@ import org.jetbrains.kotlin.ir.interpreter.IrInterpreter import org.jetbrains.kotlin.ir.interpreter.checker.EvaluationMode import org.jetbrains.kotlin.ir.interpreter.checker.IrInterpreterChecker import org.jetbrains.kotlin.ir.interpreter.isPrimitiveArray -import org.jetbrains.kotlin.ir.util.toIrConst import org.jetbrains.kotlin.ir.types.* +import org.jetbrains.kotlin.ir.util.dump +import org.jetbrains.kotlin.ir.util.isAnnotation +import org.jetbrains.kotlin.ir.util.toIrConst internal abstract class IrConstAnnotationTransformer( interpreter: IrInterpreter, @@ -67,12 +69,15 @@ internal abstract class IrConstAnnotationTransformer( } private fun IrExpression.transformSingleArg(expectedType: IrType): IrExpression { - if (this.canBeInterpreted()) { - return this.interpret(failAsError = true).convertToConstIfPossible(expectedType) - } else if (this is IrConstructorCall) { - transformAnnotation(this) + return when { + this is IrGetEnumValue || this is IrClassReference -> this + this is IrConstructorCall && this.type.isAnnotation() -> { + transformAnnotation(this) + this + } + this.canBeInterpreted() -> this.interpret(failAsError = true).convertToConstIfPossible(expectedType) + else -> error("Cannot evaluate IR expression in annotation:\n ${this.dump()}") } - return this } private fun IrExpression.convertToConstIfPossible(type: IrType): IrExpression {