From bde6df8ca1d278b1ab80d12215a53b1c3b919fe0 Mon Sep 17 00:00:00 2001 From: Ivan Kylchik Date: Tue, 20 Jun 2023 18:15:03 +0200 Subject: [PATCH] [IR] Correctly transform `KCallable.name` call to optimize it later We need to transform call from `KFunction(N).name` to `KFunction(N+1).name`. This way we keep IR correct and if something goes wrong during interpretation, we still will have compilable code. This commit suppose to fix three failing tests on Native aggregate build: 1. `FirNativePartialLinkageTestGenerated. testReferencingUnusableDeclarations` 2. `FirNativeCodegenBoxTestGenerated$Box$CallableReference$Bound. testKCallableNameIntrinsic` 3. `FirNativeCodegenBoxTestNoPLGenerated$Box$CallableReference$Bound .testKCallableNameIntrinsic` --- .../preprocessor/IrInterpreterKCallableNamePreprocessor.kt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/preprocessor/IrInterpreterKCallableNamePreprocessor.kt b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/preprocessor/IrInterpreterKCallableNamePreprocessor.kt index b2b28e566eb..2879a34ce9a 100644 --- a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/preprocessor/IrInterpreterKCallableNamePreprocessor.kt +++ b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/preprocessor/IrInterpreterKCallableNamePreprocessor.kt @@ -21,6 +21,7 @@ import org.jetbrains.kotlin.ir.types.typeOrNull import org.jetbrains.kotlin.ir.types.typeWith import org.jetbrains.kotlin.ir.util.isKFunction import org.jetbrains.kotlin.ir.util.isSubclassOf +import org.jetbrains.kotlin.ir.util.properties import org.jetbrains.kotlin.name.SpecialNames // Note: this class still will not allow us to evaluate things like `A()::a.name + `A()::b.name`. @@ -41,6 +42,9 @@ class IrInterpreterKCallableNamePreprocessor : IrInterpreterPreprocessor { callableReference.type = newType } + // We want to change symbol to keep IR correct. If something goes wrong during interpretation, we still will have compilable code. + expression.symbol = data.irBuiltIns.kCallableClass.owner.properties.single { it.name.asString() == "name" }.getter!!.symbol + callableReference.dispatchReceiver = null callableReference.extensionReceiver = null if (receiver is IrGetValue && receiver.symbol.owner.name == SpecialNames.THIS) return expression