Skip to content

Commit 6d4c52d

Browse files
aShalyginSpace Team
authored andcommitted
[Tests] KT-74529 call functions with context parameters in reflect
1 parent 79238d8 commit 6d4c52d

File tree

13 files changed

+386
-2
lines changed

13 files changed

+386
-2
lines changed

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

Lines changed: 30 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: 30 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: 30 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: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// LANGUAGE: +ContextParameters
2+
// OPT_IN: kotlin.ExperimentalContextParameters
3+
// TARGET_BACKEND: JVM
4+
// IGNORE_BACKEND_K1: ANY
5+
// WITH_REFLECT
6+
7+
import kotlin.reflect.KCallable
8+
9+
class TestClass {
10+
context(a: (() -> String) -> String) fun superFunWithContextLambda(b: () -> String) = a.invoke(b)
11+
}
12+
13+
fun box(): String {
14+
15+
val f = TestClass::class.members.single { it.name == "superFunWithContextLambda" } as KCallable<String>
16+
val contextParam: Function1<Function0<String>, String> = { a: () -> String ->
17+
a.invoke()
18+
}
19+
20+
21+
return f.call(
22+
TestClass(),
23+
contextParam,
24+
{ "OK" }
25+
)
26+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// LANGUAGE: +ContextParameters
2+
// OPT_IN: kotlin.ExperimentalContextParameters
3+
// TARGET_BACKEND: JVM
4+
// IGNORE_BACKEND_K1: ANY
5+
// WITH_REFLECT
6+
// WITH_COROUTINES
7+
8+
9+
import helpers.*
10+
import kotlin.coroutines.*
11+
import kotlin.coroutines.intrinsics.*
12+
import kotlin.reflect.KCallable
13+
import kotlin.reflect.full.callSuspend
14+
15+
suspend fun suspendHere(): String = suspendCoroutineUninterceptedOrReturn { x ->
16+
x.resume("")
17+
COROUTINE_SUSPENDED
18+
}
19+
20+
fun builder(c: suspend () -> Unit) {
21+
c.startCoroutine(EmptyContinuation)
22+
}
23+
24+
class TestClass {
25+
suspend context(a: suspend (suspend () -> String) -> String) fun superSuspendWithSuspendContextLambda(b: suspend () -> String): String {
26+
suspendHere()
27+
return a.invoke(b)
28+
}
29+
}
30+
31+
fun box(): String {
32+
var result = ""
33+
34+
val f = TestClass::class.members.single { it.name == "superSuspendWithSuspendContextLambda" } as KCallable<String>
35+
val contextParam: SuspendFunction1<SuspendFunction0<String>, String> = { a: suspend () -> String ->
36+
suspendHere()
37+
a.invoke()
38+
}
39+
40+
builder {
41+
f.callSuspend(
42+
TestClass(),
43+
contextParam,
44+
suspend {
45+
suspendHere()
46+
result += "OK"
47+
}
48+
)
49+
}
50+
51+
return result
52+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// LANGUAGE: +ContextParameters
2+
// OPT_IN: kotlin.ExperimentalContextParameters
3+
// TARGET_BACKEND: JVM
4+
// IGNORE_BACKEND_K1: ANY
5+
// WITH_REFLECT
6+
7+
import kotlin.reflect.KCallable
8+
9+
class TestClass {
10+
context(a: (() -> String) -> String) fun superFunWithContextLambda(b: () -> String) = a.invoke(b)
11+
}
12+
13+
fun box(): String {
14+
15+
val f = TestClass::class.members.single { it.name == "superFunWithContextLambda" } as KCallable<String>
16+
val contextParam: Function1<Function0<String>, String> = { a: () -> String ->
17+
a.invoke()
18+
}
19+
20+
21+
return f.callBy(
22+
mapOf(
23+
f.parameters[0] to TestClass(),
24+
f.parameters[1] to contextParam,
25+
f.parameters[2] to { "OK" } as Function0<String>
26+
)
27+
)
28+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// LANGUAGE: +ContextParameters
2+
// OPT_IN: kotlin.ExperimentalContextParameters
3+
// TARGET_BACKEND: JVM
4+
// IGNORE_BACKEND_K1: ANY
5+
// WITH_REFLECT
6+
// WITH_COROUTINES
7+
8+
9+
import helpers.*
10+
import kotlin.coroutines.*
11+
import kotlin.coroutines.intrinsics.*
12+
import kotlin.reflect.KCallable
13+
import kotlin.reflect.full.callSuspendBy
14+
15+
suspend fun suspendHere(): String = suspendCoroutineUninterceptedOrReturn { x ->
16+
x.resume("")
17+
COROUTINE_SUSPENDED
18+
}
19+
20+
fun builder(c: suspend () -> Unit) {
21+
c.startCoroutine(EmptyContinuation)
22+
}
23+
24+
class TestClass {
25+
suspend context(a: suspend (suspend () -> String) -> String) fun superSuspendWithSuspendContextLambda(b: suspend () -> String): String {
26+
suspendHere()
27+
return a.invoke(b)
28+
}
29+
}
30+
31+
fun box(): String {
32+
var result = ""
33+
34+
val f = TestClass::class.members.single { it.name == "superSuspendWithSuspendContextLambda" } as KCallable<String>
35+
val contextParam: SuspendFunction1<SuspendFunction0<String>, String> = { a: suspend () -> String ->
36+
suspendHere()
37+
a.invoke()
38+
}
39+
40+
builder {
41+
f.callSuspendBy(
42+
mapOf(
43+
f.parameters[0] to TestClass(),
44+
f.parameters[1] to contextParam,
45+
f.parameters[2] to suspend {
46+
suspendHere()
47+
result += "OK"
48+
} as SuspendFunction0<String>
49+
)
50+
)
51+
}
52+
return result
53+
}

compiler/testData/codegen/box/reflection/parameters/anonymousContextParameter.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import kotlin.reflect.KParameter.Kind.*
99
import kotlin.test.assertEquals
1010

1111
class Z {
12-
context(_: String, _: Number)
12+
context(_: String, named: Int, _: Number)
1313
fun f() {}
1414
}
1515

1616
fun box(): String {
1717
val f = Z::class.members.single { it.name == "f" }
18-
assertEquals(listOf(null, null, null), f.parameters.map { it.name })
18+
assertEquals(listOf(null, null, "named", null), f.parameters.map { it.name })
1919
return "OK"
2020
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// LANGUAGE: +ContextParameters
2+
// OPT_IN: kotlin.ExperimentalContextParameters
3+
// TARGET_BACKEND: JVM
4+
// IGNORE_BACKEND_K1: ANY
5+
// WITH_REFLECT
6+
7+
import kotlin.reflect.KProperty
8+
9+
class TestClass {
10+
context(s: String, i: Int) val Any?.prop: String
11+
get() = if(i == 2) this.toString() + s else "FAIL"
12+
}
13+
14+
fun box() = (TestClass::class.members.single { it.name == "prop" } as KProperty<*>)
15+
.getter.call(TestClass(), "K", 2, "O")

0 commit comments

Comments
 (0)