Skip to content

Commit 1e41d14

Browse files
udalovSpace Team
authored andcommitted
Reflection: fix classifier and rendering of suspend function types
Also fix a mistake in the `JavaToKotlinClassMap.mapKotlinToJava`'s kdoc. #KT-79020 Fixed #KT-79206 Fixed
1 parent 339e6ff commit 1e41d14

File tree

14 files changed

+100
-5
lines changed

14 files changed

+100
-5
lines changed

analysis/low-level-api-fir/tests-gen/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLBlackBoxTestGenerated.java

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

analysis/low-level-api-fir/tests-gen/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLReversedBlackBoxTestGenerated.java

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenTestGenerated.java

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirPsiBlackBoxCodegenTestGenerated.java

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/testData/codegen/box/reflection/annotations/onTypes/differentArgumentTypes.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// WITH_REFLECT
33
package test
44

5+
import kotlin.coroutines.SuspendFunction0
56
import kotlin.reflect.KClass
67
import kotlin.test.assertEquals
78
import kotlin.test.assertTrue
@@ -60,7 +61,7 @@ fun f(): @Anno(
6061
AnnotationTarget.EXPRESSION,
6162
Nested("1"),
6263
["lmao"],
63-
[Double::class, Unit::class, LongArray::class, Array<String>::class],
64+
[Double::class, Unit::class, LongArray::class, Array<String>::class, Function0::class, SuspendFunction0::class],
6465
[AnnotationTarget.TYPEALIAS, AnnotationTarget.FIELD],
6566
[Nested("2"), Nested("3")]
6667
) Unit {}
@@ -71,7 +72,7 @@ fun box(): String {
7172
"@test.Anno(b=1, c=x, d=3.14, f=-2.72, i=42424242, j=239239239239239, s=42, z=true, " +
7273
"ba=[-1], ca=[y], da=[-3.14159], fa=[2.7218], ia=[424242], ja=[239239239239], sa=[-43], za=[false, true], " +
7374
"str=lol, k=class java.lang.Number, k2=class [I, e=EXPRESSION, [email protected](value=1), stra=[lmao], " +
74-
"ka=[class java.lang.Double, class kotlin.Unit, class [J, class [Ljava.lang.String;], " +
75+
"ka=[class java.lang.Double, class kotlin.Unit, class [J, class [Ljava.lang.String;, interface kotlin.jvm.functions.Function0, interface kotlin.jvm.functions.Function1], " +
7576
"ea=[TYPEALIAS, FIELD], aa=[@test.Nested(value=2), @test.Nested(value=3)])",
7677
anno.toString()
7778
)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// TARGET_BACKEND: JVM
2+
// WITH_REFLECT
3+
4+
import kotlin.reflect.*
5+
import kotlin.test.assertEquals
6+
import kotlin.test.assertTrue
7+
8+
class A {
9+
fun function0(): (() -> Unit) = null!!
10+
fun function1(): ((String) -> Unit)? = null
11+
fun function3(): ((Any, Int, Unit?) -> LongArray)? = null
12+
fun suspendFunction0(): (suspend () -> Unit)? = null
13+
fun suspendFunction1(): (suspend (String) -> Unit) = null!!
14+
fun suspendFunction3(): (suspend (Any, Int, Unit?) -> LongArray)? = null
15+
}
16+
17+
fun check(function: KCallable<*>, typeToString: String, classFqName: String, arguments: String) {
18+
val type = function.returnType
19+
assertEquals(typeToString.endsWith("?"), type.isMarkedNullable, "Fail: isMarkedNullable for $type")
20+
val classifier = type.classifier as? KClass<*> ?: error("Fail: no classifier for $type")
21+
assertEquals(classFqName, classifier.qualifiedName, "Fail: qualified name for $type")
22+
assertEquals(arguments, type.arguments.toString(), "Fail: arguments for $type")
23+
assertEquals(typeToString, type.toString())
24+
}
25+
26+
fun box(): String {
27+
check(A::function0, "() -> kotlin.Unit", "kotlin.Function0", "[kotlin.Unit]")
28+
check(A::function1, "((kotlin.String) -> kotlin.Unit)?", "kotlin.Function1", "[kotlin.String, kotlin.Unit]")
29+
check(A::function3, "((kotlin.Any, kotlin.Int, kotlin.Unit?) -> kotlin.LongArray)?", "kotlin.Function3", "[kotlin.Any, kotlin.Int, kotlin.Unit?, kotlin.LongArray]")
30+
// After KT-79225, classifier should be "kotlin.coroutines.SuspendFunction{arity}"
31+
check(A::suspendFunction0, "(suspend () -> kotlin.Unit)?", "kotlin.Function1", "[kotlin.Unit]")
32+
check(A::suspendFunction1, "suspend (kotlin.String) -> kotlin.Unit", "kotlin.Function2", "[kotlin.String, kotlin.Unit]")
33+
check(A::suspendFunction3, "(suspend (kotlin.Any, kotlin.Int, kotlin.Unit?) -> kotlin.LongArray)?", "kotlin.Function4", "[kotlin.Any, kotlin.Int, kotlin.Unit?, kotlin.LongArray]")
34+
35+
return "OK"
36+
}

compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/JvmAbiConsistencyTestBoxGenerated.java

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/inlineScopes/FirBlackBoxCodegenTestWithInlineScopesGenerated.java

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/compiler.common.jvm/src/org/jetbrains/kotlin/builtins/jvm/JavaToKotlinClassMap.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ object JavaToKotlinClassMap {
142142
* - kotlin.Nothing -> java.lang.Void
143143
* - kotlin.IntArray -> null
144144
* - kotlin.Function3 -> kotlin.jvm.functions.Function3
145-
* - kotlin.coroutines.SuspendFunction3 -> kotlin.jvm.functions.Function4
145+
* - kotlin.coroutines.SuspendFunction3 -> null
146+
* - NOT kotlin.jvm.functions.Function4, to make sure that Continuation type argument is correctly added manually at the call site.
146147
* - kotlin.Function42 -> kotlin.jvm.functions.FunctionN
147148
* - kotlin.coroutines.SuspendFunction42 -> kotlin.jvm.functions.FunctionN
148149
* - kotlin.reflect.KFunction3 -> kotlin.reflect.KFunction

0 commit comments

Comments
 (0)