From 1ce99db83040965367e15b9412490962d1ef9176 Mon Sep 17 00:00:00 2001 From: Ivan Kylchik Date: Mon, 17 Oct 2022 17:17:55 +0200 Subject: [PATCH] Change class loader in ir interpreter that is used to create proxy obj #KT-54509 Fixed --- .../FirBlackBoxCodegenTestGenerated.java | 6 ++++++ .../kotlin/ir/interpreter/proxy/CommonProxy.kt | 2 +- .../codegen/box/evaluate/thisPlusString.kt | 17 +++++++++++++++++ .../testData/launcher/interpreterClassLoader.kt | 14 ++++++++++++++ .../codegen/IrBlackBoxCodegenTestGenerated.java | 6 ++++++ .../jetbrains/kotlin/cli/LauncherScriptTest.kt | 6 ++++++ 6 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/codegen/box/evaluate/thisPlusString.kt create mode 100644 compiler/testData/launcher/interpreterClassLoader.kt diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java index 2c56c3a6c67..63d40073f5e 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java @@ -17015,6 +17015,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/evaluate/simpleCallBinary.kt"); } + @Test + @TestMetadata("thisPlusString.kt") + public void testThisPlusString() throws Exception { + runTest("compiler/testData/codegen/box/evaluate/thisPlusString.kt"); + } + @Test @TestMetadata("unaryMinus.kt") public void testUnaryMinus() throws Exception { diff --git a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/proxy/CommonProxy.kt b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/proxy/CommonProxy.kt index 83b392054b8..62985995f0c 100644 --- a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/proxy/CommonProxy.kt +++ b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/proxy/CommonProxy.kt @@ -77,7 +77,7 @@ internal class CommonProxy private constructor(override val state: Common, overr else -> arrayOf(extendFrom, Proxy::class.java) } - return java.lang.reflect.Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(), interfaces) + return java.lang.reflect.Proxy.newProxyInstance(this::class.java.classLoader, interfaces) { /*proxy*/_, method, args -> when { method.declaringClass == Proxy::class.java && method.name == "getState" -> commonProxy.state diff --git a/compiler/testData/codegen/box/evaluate/thisPlusString.kt b/compiler/testData/codegen/box/evaluate/thisPlusString.kt new file mode 100644 index 00000000000..96254ecfdfc --- /dev/null +++ b/compiler/testData/codegen/box/evaluate/thisPlusString.kt @@ -0,0 +1,17 @@ +// TARGET_BACKEND: JVM_IR +// WITH_STDLIB + +object Test { + fun foo(): String = "foo " + this + + fun bar(): String = "bar $this" + + fun baz(): String = "baz " + this.toString() +} + +fun box(): String { + if (!Test.foo().startsWith("foo ")) return "Fail ${Test.foo()}" + if (!Test.bar().startsWith("bar ")) return "Fail ${Test.bar()}" + if (!Test.baz().startsWith("baz ")) return "Fail ${Test.baz()}" + return "OK" +} diff --git a/compiler/testData/launcher/interpreterClassLoader.kt b/compiler/testData/launcher/interpreterClassLoader.kt new file mode 100644 index 00000000000..2db5da9cf66 --- /dev/null +++ b/compiler/testData/launcher/interpreterClassLoader.kt @@ -0,0 +1,14 @@ +package test + +// for more information see KT-54509 +object Test { + fun foo(): String = "foo " + this + + fun bar(): String = "bar $this" + + fun baz(): String = "baz " + this.toString() +} + +fun main() { + println(Test.foo() + Test.bar() + Test.baz()) +} diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java index 2460a077d58..019d4eba86f 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java @@ -17015,6 +17015,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/evaluate/simpleCallBinary.kt"); } + @Test + @TestMetadata("thisPlusString.kt") + public void testThisPlusString() throws Exception { + runTest("compiler/testData/codegen/box/evaluate/thisPlusString.kt"); + } + @Test @TestMetadata("unaryMinus.kt") public void testUnaryMinus() throws Exception { diff --git a/compiler/tests/org/jetbrains/kotlin/cli/LauncherScriptTest.kt b/compiler/tests/org/jetbrains/kotlin/cli/LauncherScriptTest.kt index b9893ede8f0..8fb80617c91 100644 --- a/compiler/tests/org/jetbrains/kotlin/cli/LauncherScriptTest.kt +++ b/compiler/tests/org/jetbrains/kotlin/cli/LauncherScriptTest.kt @@ -472,4 +472,10 @@ println(42) environment = jdk11, ) } + + fun testInterpreterClassLoader() { + runProcess( + "kotlinc", "$testDataDirectory/interpreterClassLoader.kt", "-d", tmpdir.path + ) + } }