Implement proper name interpretation for suspend functions

#KT-57313 Fixed
This commit is contained in:
Ivan Kylchik
2023-04-08 22:44:37 +02:00
committed by Space Team
parent 82589f2506
commit 08ba63df90
10 changed files with 92 additions and 7 deletions
@@ -28277,6 +28277,12 @@ public class FirLightTreeBlackBoxCodegenTestGenerated extends AbstractFirLightTr
runTest("compiler/testData/codegen/box/involvesIrInterpreter/kt56215.kt");
}
@Test
@TestMetadata("kt57313.kt")
public void testKt57313() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/kt57313.kt");
}
@Test
@TestMetadata("referenceNameFromStaticInDifferentModule.kt")
public void testReferenceNameFromStaticInDifferentModule() throws Exception {
@@ -28277,6 +28277,12 @@ public class FirPsiBlackBoxCodegenTestGenerated extends AbstractFirPsiBlackBoxCo
runTest("compiler/testData/codegen/box/involvesIrInterpreter/kt56215.kt");
}
@Test
@TestMetadata("kt57313.kt")
public void testKt57313() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/kt57313.kt");
}
@Test
@TestMetadata("referenceNameFromStaticInDifferentModule.kt")
public void testReferenceNameFromStaticInDifferentModule() throws Exception {
@@ -198,6 +198,13 @@ internal class Wrapper(val value: Any, override val irClass: IrClass, environmen
}
}
private fun Int?.getCorrespondingFunction(): Class<*> {
return when {
this == null || this >= BuiltInFunctionArity.BIG_ARITY -> Class.forName("kotlin.jvm.functions.FunctionN")
else -> Class.forName("kotlin.jvm.functions.Function$this")
}
}
private fun IrType.getClass(asObject: Boolean): Class<out Any> {
val owner = this.classOrNull?.owner
val fqName = owner?.fqName
@@ -221,15 +228,12 @@ internal class Wrapper(val value: Any, override val irClass: IrClass, environmen
notNullType.isThrowable() -> Throwable::class.java
notNullType.isIterable() -> Iterable::class.java
notNullType.isKFunction() -> Class.forName("kotlin.reflect.KFunction")
notNullType.isKFunction() || notNullType.isKSuspendFunction() -> Class.forName("kotlin.reflect.KFunction")
notNullType.isFunction() -> {
val arity = fqName?.removePrefix("kotlin.Function")?.toIntOrNull()
return when {
arity == null || arity >= BuiltInFunctionArity.BIG_ARITY -> Class.forName("kotlin.jvm.functions.FunctionN")
else -> Class.forName("kotlin.jvm.functions.${fqName.removePrefix("kotlin.")}")
}
return arity.getCorrespondingFunction()
}
//notNullType.isSuspendFunction() || notNullType.isKSuspendFunction() -> throw AssertionError() //TODO
notNullType.isSuspendFunction() -> error("Interpretation of $fqName is not supported")
fqName == "kotlin.Enum" -> Enum::class.java
fqName == "kotlin.collections.Collection" || fqName == "kotlin.collections.MutableCollection" -> Collection::class.java
@@ -4,19 +4,22 @@
class A(val OK: Int, val somePropertyWithLongName: String) {
fun foo() {}
suspend fun bar() {}
}
val topLevelProp = 1
const val propertyName1 = A::OK.name
const val propertyName2 = A::somePropertyWithLongName.name
const val methodName = A::foo.name
const val suspendMethodName = A::bar.name
const val className = ::A.name
const val topLevelPropName = ::topLevelProp.name
fun box(): String {
if (propertyName1 != "OK") return "Fail 1"
if (propertyName2 != "somePropertyWithLongName") return "Fail 2"
if (methodName != "foo") return "Fail 3"
if (methodName != "foo") return "Fail 3.1"
if (suspendMethodName != "bar") return "Fail 3.2"
if (className != "<init>") return "Fail 4"
if (topLevelPropName != "topLevelProp") return "Fail 5"
return "OK"
@@ -0,0 +1,22 @@
// TARGET_BACKEND: JVM_IR
abstract class AsyncJob {
abstract suspend fun execute(lifetime: AsyncLifetime, attempt: Int, due: DateTime, context: JobContext): JobContext
}
class OrgBootstrapRequest
class AsyncLifetime
class DateTime
class JobContext
class OrgBootstrapTriggerJob(val orgId: Long, val bootstrap: OrgBootstrapRequest, val jetSalesSync: Boolean?) : AsyncJob() {
override suspend fun execute(lifetime: AsyncLifetime, attempt: Int, due: DateTime, context: JobContext): JobContext {
return JobContext()
}
}
val name = "${OrgBootstrapTriggerJob::class.simpleName}.${OrgBootstrapTriggerJob::execute.name}"
fun box(): String {
return "OK"
}
@@ -0,0 +1,20 @@
@CompileTimeCalculation
class A(val a: Int) {
fun foo(): Int {
return a
}
suspend fun baz(): Int {
return a
}
}
const val functionName = <!EVALUATED: `foo`!>A::foo.name<!>
const val functionInvoke = <!EVALUATED: `1`!>A::foo.invoke(A(1))<!>
const val functionWithReceiverName = <!EVALUATED: `foo`!>A(2)::foo.name<!>
const val functionWithReceiverInvoke = <!EVALUATED: `2`!>A(2)::foo.invoke()<!>
// THIS IS WRONG, suspend fun must be called from coroutine ot another suspend function.
// It is used here just for test purposes.
const val suspendFunctionInvoke = <!EVALUATED: `3`!>A::baz.invoke(A(3))<!>
@@ -28277,6 +28277,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/involvesIrInterpreter/kt56215.kt");
}
@Test
@TestMetadata("kt57313.kt")
public void testKt57313() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/kt57313.kt");
}
@Test
@TestMetadata("referenceNameFromStaticInDifferentModule.kt")
public void testReferenceNameFromStaticInDifferentModule() throws Exception {
@@ -28277,6 +28277,12 @@ public class IrBlackBoxCodegenWithIrInlinerTestGenerated extends AbstractIrBlack
runTest("compiler/testData/codegen/box/involvesIrInterpreter/kt56215.kt");
}
@Test
@TestMetadata("kt57313.kt")
public void testKt57313() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/kt57313.kt");
}
@Test
@TestMetadata("referenceNameFromStaticInDifferentModule.kt")
public void testReferenceNameFromStaticInDifferentModule() throws Exception {
@@ -706,6 +706,12 @@ public class JvmIrInterpreterAfterFirPsi2IrTestGenerated extends AbstractJvmIrIn
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/ir/interpreter/reference"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@Test
@TestMetadata("functionReference.kt")
public void testFunctionReference() throws Exception {
runTest("compiler/testData/ir/interpreter/reference/functionReference.kt");
}
@Test
@TestMetadata("getClass.kt")
public void testGetClass() throws Exception {
@@ -706,6 +706,12 @@ public class JvmIrInterpreterAfterPsi2IrTestGenerated extends AbstractJvmIrInter
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/ir/interpreter/reference"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@Test
@TestMetadata("functionReference.kt")
public void testFunctionReference() throws Exception {
runTest("compiler/testData/ir/interpreter/reference/functionReference.kt");
}
@Test
@TestMetadata("getClass.kt")
public void testGetClass() throws Exception {