Fix lowering of receiver access in IR scripting
#KT-53947 fixed without the fix, in the presence of the implicit receiver of the same type as the processed receive, the lowering was incorrectly substituting the correct receiver with the accessor to the implicit one
This commit is contained in:
+74
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package kotlin.script.experimental.jvmhost.test
|
||||
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
|
||||
import org.junit.Test
|
||||
import kotlin.script.experimental.api.*
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class CapturingTest {
|
||||
|
||||
@Test
|
||||
fun testScriptWithImplicitReceiverAndSimpleCapturing() {
|
||||
// Reproducing (a bit extended) scenario from KT-53947: without the fix, in the presence of the implicit receiver
|
||||
// of the same type as the receiver in the `apply` function body, the lowering was incorrectly substituting
|
||||
// the correct receiver with the accessor to the implicit one
|
||||
val res = evalString<ScriptWithImplicitReceiver>(
|
||||
"""
|
||||
class C {
|
||||
fun foo() = receiverString + "."
|
||||
}
|
||||
|
||||
C().foo()
|
||||
""".trimIndent()
|
||||
) {
|
||||
implicitReceivers(ImplicitReceiverClass("Ok"))
|
||||
}
|
||||
|
||||
assertTrue(
|
||||
res.safeAs<ResultWithDiagnostics.Success<EvaluationResult>>()?.value?.returnValue?.safeAs<ResultValue.Value>()?.value == "Ok.",
|
||||
"test failed:\n ${res.render()}"
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testScriptWithImplicitReceiverAndNoCapturing() {
|
||||
// Reproducing (a bit extended) scenario from KT-53947: without the fix, in the presence of the implicit receiver
|
||||
// of the same type as the receiver in the `C2.apply` function body, the lowering was incorrectly substituting
|
||||
// the correct receiver with the accessor to the implicit one
|
||||
// `C1.foo` tests the similar situation with extension receiver.
|
||||
val res = evalString<ScriptWithImplicitReceiver>(
|
||||
"""
|
||||
import kotlin.script.experimental.jvmhost.test.ImplicitReceiverClass
|
||||
|
||||
class C1 {
|
||||
fun run(receiver: ImplicitReceiverClass): String = receiver.foo()
|
||||
|
||||
fun ImplicitReceiverClass.foo() = "--" + receiverString
|
||||
}
|
||||
|
||||
class C2 {
|
||||
fun apply(receiver: ImplicitReceiverClass): String =
|
||||
"++" + receiver.receiverString
|
||||
}
|
||||
|
||||
C2().apply(
|
||||
ImplicitReceiverClass(
|
||||
C1().run(ImplicitReceiverClass("Ok"))
|
||||
)
|
||||
)
|
||||
""".trimIndent()
|
||||
) {
|
||||
implicitReceivers(ImplicitReceiverClass("Not Ok."))
|
||||
}
|
||||
|
||||
assertTrue(
|
||||
res.safeAs<ResultWithDiagnostics.Success<EvaluationResult>>()?.value?.returnValue?.safeAs<ResultValue.Value>()?.value == "++--Ok",
|
||||
"test failed:\n ${res.render()}"
|
||||
)
|
||||
}
|
||||
}
|
||||
+4
-6
@@ -6,10 +6,7 @@
|
||||
package kotlin.script.experimental.jvmhost.test
|
||||
|
||||
import org.junit.Test
|
||||
import kotlin.script.experimental.api.EvaluationResult
|
||||
import kotlin.script.experimental.api.ResultWithDiagnostics
|
||||
import kotlin.script.experimental.api.implicitReceivers
|
||||
import kotlin.script.experimental.api.providedProperties
|
||||
import kotlin.script.experimental.api.*
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class ConstructorArgumentsOrderTest {
|
||||
@@ -51,6 +48,7 @@ class ConstructorArgumentsOrderTest {
|
||||
)
|
||||
}
|
||||
|
||||
private fun ResultWithDiagnostics<EvaluationResult>.render() =
|
||||
reports.joinToString("\n ") { it.message + if (it.exception == null) "" else ": ${it.exception!!.printStackTrace()}" }
|
||||
}
|
||||
|
||||
internal fun ResultWithDiagnostics<EvaluationResult>.render() =
|
||||
reports.joinToString("\n ") { it.message + if (it.exception == null) "" else ": ${it.exception!!.printStackTrace()}" }
|
||||
|
||||
Reference in New Issue
Block a user