[JVM_IR] Do not compile-time evaluate apiVersionIsAtLeast in stdlib

Function `apiVersionIsAtLeast` was introduced to be able to have
different inline function content inlined to user code call sites
depending on their api version settings. Thus, it should not be
compile-time evaluated when being called in the body of inline stdlib
function.

^KT-59452: Fixed
This commit is contained in:
vladislav.grechko
2023-06-17 14:44:12 +02:00
committed by Space Team
parent adab552928
commit e4f1c10bc1
18 changed files with 125 additions and 14 deletions
@@ -201,16 +201,7 @@ abstract class InlineCodegen<out T : BaseExpressionCodegen>(
protected abstract fun generateAssertField()
private fun isInlinedToInlineFunInKotlinRuntime(): Boolean {
val codegen = this.codegen as? ExpressionCodegen ?: return false
val caller = codegen.context.functionDescriptor
if (!caller.isInline) return false
val callerPackage = DescriptorUtils.getParentOfType(caller, PackageFragmentDescriptor::class.java) ?: return false
return callerPackage.fqName.asString().let {
// package either equals to 'kotlin' or starts with 'kotlin.'
it.startsWith("kotlin") && (it.length <= 6 || it[6] == '.')
}
}
protected abstract fun isInlinedToInlineFunInKotlinRuntime(): Boolean
protected fun rememberClosure(parameterType: Type, index: Int, lambdaInfo: LambdaInfo) {
invocationParamBuilder.addNextValueParameter(parameterType, true, null, index).functionalArgument = lambdaInfo
@@ -17,6 +17,7 @@ import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
import org.jetbrains.kotlin.psi.psiUtil.isAncestor
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.calls.util.getResolvedCallWithAssert
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
import org.jetbrains.kotlin.resolve.inline.InlineUtil
@@ -185,6 +186,14 @@ class PsiInlineCodegen(
putCapturedToLocalVal(stackValue, activeLambda!!.capturedVars[paramIndex], stackValue.kotlinType)
override fun reorderArgumentsIfNeeded(actualArgsWithDeclIndex: List<ArgumentAndDeclIndex>, valueParameterTypes: List<Type>) = Unit
override fun isInlinedToInlineFunInKotlinRuntime(): Boolean {
val codegen = this.codegen as? ExpressionCodegen ?: return false
val caller = codegen.context.functionDescriptor
if (!caller.isInline) return false
val callerPackage = DescriptorUtils.getParentOfType(caller, PackageFragmentDescriptor::class.java) ?: return false
return callerPackage.fqName.asString().startsWith("kotlin.")
}
}
private val FunctionDescriptor.explicitParameters
@@ -1065,6 +1065,12 @@ public class FirLightTreeBlackBoxInlineCodegenWithBytecodeInlinerTestGenerated e
public void testApiVersionAtLeast1() throws Exception {
runTest("compiler/testData/codegen/boxInline/bytecodePreprocessing/apiVersionAtLeast1.kt");
}
@Test
@TestMetadata("inlineApiVersionAtLeastInStdlibInlineFunction.kt")
public void testInlineApiVersionAtLeastInStdlibInlineFunction() throws Exception {
runTest("compiler/testData/codegen/boxInline/bytecodePreprocessing/inlineApiVersionAtLeastInStdlibInlineFunction.kt");
}
}
@Nested
@@ -1065,6 +1065,12 @@ public class FirLightTreeBlackBoxInlineCodegenWithIrInlinerTestGenerated extends
public void testApiVersionAtLeast1() throws Exception {
runTest("compiler/testData/codegen/boxInline/bytecodePreprocessing/apiVersionAtLeast1.kt");
}
@Test
@TestMetadata("inlineApiVersionAtLeastInStdlibInlineFunction.kt")
public void testInlineApiVersionAtLeastInStdlibInlineFunction() throws Exception {
runTest("compiler/testData/codegen/boxInline/bytecodePreprocessing/inlineApiVersionAtLeastInStdlibInlineFunction.kt");
}
}
@Nested
@@ -1065,6 +1065,12 @@ public class FirLightTreeSerializeCompileKotlinAgainstInlineKotlinTestGenerated
public void testApiVersionAtLeast1() throws Exception {
runTest("compiler/testData/codegen/boxInline/bytecodePreprocessing/apiVersionAtLeast1.kt");
}
@Test
@TestMetadata("inlineApiVersionAtLeastInStdlibInlineFunction.kt")
public void testInlineApiVersionAtLeastInStdlibInlineFunction() throws Exception {
runTest("compiler/testData/codegen/boxInline/bytecodePreprocessing/inlineApiVersionAtLeastInStdlibInlineFunction.kt");
}
}
@Nested
@@ -1065,6 +1065,12 @@ public class FirPsiBlackBoxInlineCodegenWithBytecodeInlinerTestGenerated extends
public void testApiVersionAtLeast1() throws Exception {
runTest("compiler/testData/codegen/boxInline/bytecodePreprocessing/apiVersionAtLeast1.kt");
}
@Test
@TestMetadata("inlineApiVersionAtLeastInStdlibInlineFunction.kt")
public void testInlineApiVersionAtLeastInStdlibInlineFunction() throws Exception {
runTest("compiler/testData/codegen/boxInline/bytecodePreprocessing/inlineApiVersionAtLeastInStdlibInlineFunction.kt");
}
}
@Nested
@@ -1065,6 +1065,12 @@ public class FirPsiBlackBoxInlineCodegenWithIrInlinerTestGenerated extends Abstr
public void testApiVersionAtLeast1() throws Exception {
runTest("compiler/testData/codegen/boxInline/bytecodePreprocessing/apiVersionAtLeast1.kt");
}
@Test
@TestMetadata("inlineApiVersionAtLeastInStdlibInlineFunction.kt")
public void testInlineApiVersionAtLeastInStdlibInlineFunction() throws Exception {
runTest("compiler/testData/codegen/boxInline/bytecodePreprocessing/inlineApiVersionAtLeastInStdlibInlineFunction.kt");
}
}
@Nested
@@ -1065,6 +1065,12 @@ public class FirPsiSerializeCompileKotlinAgainstInlineKotlinTestGenerated extend
public void testApiVersionAtLeast1() throws Exception {
runTest("compiler/testData/codegen/boxInline/bytecodePreprocessing/apiVersionAtLeast1.kt");
}
@Test
@TestMetadata("inlineApiVersionAtLeastInStdlibInlineFunction.kt")
public void testInlineApiVersionAtLeastInStdlibInlineFunction() throws Exception {
runTest("compiler/testData/codegen/boxInline/bytecodePreprocessing/inlineApiVersionAtLeastInStdlibInlineFunction.kt");
}
}
@Nested
@@ -9,6 +9,7 @@ import org.jetbrains.kotlin.backend.jvm.ir.isInlineOnly
import org.jetbrains.kotlin.backend.jvm.ir.isInlineParameter
import org.jetbrains.kotlin.backend.jvm.ir.unwrapInlineLambda
import org.jetbrains.kotlin.backend.jvm.mapping.IrCallableMethod
import org.jetbrains.kotlin.builtins.StandardNames
import org.jetbrains.kotlin.codegen.IrExpressionLambda
import org.jetbrains.kotlin.codegen.JvmKotlinType
import org.jetbrains.kotlin.codegen.StackValue
@@ -21,10 +22,7 @@ import org.jetbrains.kotlin.ir.declarations.IrValueParameter
import org.jetbrains.kotlin.ir.descriptors.toIrBasedKotlinType
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.util.dump
import org.jetbrains.kotlin.ir.util.explicitParameters
import org.jetbrains.kotlin.ir.util.getArgumentsWithIr
import org.jetbrains.kotlin.ir.util.isSuspendFunction
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.resolve.jvm.AsmTypes
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature
import org.jetbrains.kotlin.types.KotlinType
@@ -82,6 +80,11 @@ class IrInlineCodegen(
codegen.classCodegen.generateAssertFieldIfNeeded(isClInit)?.accept(codegen, BlockInfo())?.discard()
}
override fun isInlinedToInlineFunInKotlinRuntime(): Boolean {
val callee = codegen.irFunction
return callee.isInline && callee.getPackageFragment().packageFqName.startsWith(StandardNames.BUILT_INS_PACKAGE_NAME)
}
override fun genValueAndPut(
irValueParameter: IrValueParameter,
argumentExpression: IrExpression,
@@ -0,0 +1,24 @@
// TARGET_BACKEND: JVM
// Wrong function resolution after package renaming
// IGNORE_BACKEND: ANDROID
// WITH_STDLIB
// FILE: A.kt
package kotlin.internal
fun apiVersionIsAtLeast(epic: Int, major: Int, minor: Int): Boolean {
return false
}
inline fun versionDependentInlineFun() = if (apiVersionIsAtLeast(1, 1, 0)) "Fail" else "OK"
inline fun test() = versionDependentInlineFun()
// FILE: B.kt
import kotlin.internal.*
fun box(): String {
val clazz = Class.forName("kotlin.internal.AKt")
val func = clazz.methods.single { it.name == "test" }
return func.invoke(null) as String
}
@@ -1041,6 +1041,12 @@ public class BlackBoxInlineCodegenTestGenerated extends AbstractBlackBoxInlineCo
public void testApiVersionAtLeast1() throws Exception {
runTest("compiler/testData/codegen/boxInline/bytecodePreprocessing/apiVersionAtLeast1.kt");
}
@Test
@TestMetadata("inlineApiVersionAtLeastInStdlibInlineFunction.kt")
public void testInlineApiVersionAtLeastInStdlibInlineFunction() throws Exception {
runTest("compiler/testData/codegen/boxInline/bytecodePreprocessing/inlineApiVersionAtLeastInStdlibInlineFunction.kt");
}
}
@Nested
@@ -1041,6 +1041,12 @@ public class CompileKotlinAgainstInlineKotlinTestGenerated extends AbstractCompi
public void testApiVersionAtLeast1() throws Exception {
runTest("compiler/testData/codegen/boxInline/bytecodePreprocessing/apiVersionAtLeast1.kt");
}
@Test
@TestMetadata("inlineApiVersionAtLeastInStdlibInlineFunction.kt")
public void testInlineApiVersionAtLeastInStdlibInlineFunction() throws Exception {
runTest("compiler/testData/codegen/boxInline/bytecodePreprocessing/inlineApiVersionAtLeastInStdlibInlineFunction.kt");
}
}
@Nested
@@ -1065,6 +1065,12 @@ public class IrBlackBoxInlineCodegenWithBytecodeInlinerTestGenerated extends Abs
public void testApiVersionAtLeast1() throws Exception {
runTest("compiler/testData/codegen/boxInline/bytecodePreprocessing/apiVersionAtLeast1.kt");
}
@Test
@TestMetadata("inlineApiVersionAtLeastInStdlibInlineFunction.kt")
public void testInlineApiVersionAtLeastInStdlibInlineFunction() throws Exception {
runTest("compiler/testData/codegen/boxInline/bytecodePreprocessing/inlineApiVersionAtLeastInStdlibInlineFunction.kt");
}
}
@Nested
@@ -1065,6 +1065,12 @@ public class IrBlackBoxInlineCodegenWithIrInlinerTestGenerated extends AbstractI
public void testApiVersionAtLeast1() throws Exception {
runTest("compiler/testData/codegen/boxInline/bytecodePreprocessing/apiVersionAtLeast1.kt");
}
@Test
@TestMetadata("inlineApiVersionAtLeastInStdlibInlineFunction.kt")
public void testInlineApiVersionAtLeastInStdlibInlineFunction() throws Exception {
runTest("compiler/testData/codegen/boxInline/bytecodePreprocessing/inlineApiVersionAtLeastInStdlibInlineFunction.kt");
}
}
@Nested
@@ -1065,6 +1065,12 @@ public class IrCompileKotlinAgainstInlineKotlinTestGenerated extends AbstractIrC
public void testApiVersionAtLeast1() throws Exception {
runTest("compiler/testData/codegen/boxInline/bytecodePreprocessing/apiVersionAtLeast1.kt");
}
@Test
@TestMetadata("inlineApiVersionAtLeastInStdlibInlineFunction.kt")
public void testInlineApiVersionAtLeastInStdlibInlineFunction() throws Exception {
runTest("compiler/testData/codegen/boxInline/bytecodePreprocessing/inlineApiVersionAtLeastInStdlibInlineFunction.kt");
}
}
@Nested
@@ -1065,6 +1065,12 @@ public class IrSerializeCompileKotlinAgainstInlineKotlinTestGenerated extends Ab
public void testApiVersionAtLeast1() throws Exception {
runTest("compiler/testData/codegen/boxInline/bytecodePreprocessing/apiVersionAtLeast1.kt");
}
@Test
@TestMetadata("inlineApiVersionAtLeastInStdlibInlineFunction.kt")
public void testInlineApiVersionAtLeastInStdlibInlineFunction() throws Exception {
runTest("compiler/testData/codegen/boxInline/bytecodePreprocessing/inlineApiVersionAtLeastInStdlibInlineFunction.kt");
}
}
@Nested
@@ -1065,6 +1065,12 @@ public class JvmIrAgainstOldBoxInlineTestGenerated extends AbstractJvmIrAgainstO
public void testApiVersionAtLeast1() throws Exception {
runTest("compiler/testData/codegen/boxInline/bytecodePreprocessing/apiVersionAtLeast1.kt");
}
@Test
@TestMetadata("inlineApiVersionAtLeastInStdlibInlineFunction.kt")
public void testInlineApiVersionAtLeastInStdlibInlineFunction() throws Exception {
runTest("compiler/testData/codegen/boxInline/bytecodePreprocessing/inlineApiVersionAtLeastInStdlibInlineFunction.kt");
}
}
@Nested
@@ -1041,6 +1041,12 @@ public class JvmOldAgainstIrBoxInlineTestGenerated extends AbstractJvmOldAgainst
public void testApiVersionAtLeast1() throws Exception {
runTest("compiler/testData/codegen/boxInline/bytecodePreprocessing/apiVersionAtLeast1.kt");
}
@Test
@TestMetadata("inlineApiVersionAtLeastInStdlibInlineFunction.kt")
public void testInlineApiVersionAtLeastInStdlibInlineFunction() throws Exception {
runTest("compiler/testData/codegen/boxInline/bytecodePreprocessing/inlineApiVersionAtLeastInStdlibInlineFunction.kt");
}
}
@Nested