From 6897e89bbc61c8ba16bc9a112df5fdee76fcfa85 Mon Sep 17 00:00:00 2001 From: pyos Date: Wed, 30 Jun 2021 20:34:39 +0200 Subject: [PATCH] JVM: add tests for optimized getDelegate --- .../FirBlackBoxCodegenTestGenerated.java | 12 +++++ .../delegateToAnotherCustom.kt | 14 +++++ .../delegateToAnotherReflection.kt | 51 +++++++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 12 +++++ .../IrBlackBoxCodegenTestGenerated.java | 12 +++++ .../LightAnalysisModeTestGenerated.java | 10 ++++ .../IrJsCodegenBoxES6TestGenerated.java | 5 ++ .../IrJsCodegenBoxTestGenerated.java | 5 ++ .../semantics/JsCodegenBoxTestGenerated.java | 5 ++ 9 files changed, 126 insertions(+) create mode 100644 compiler/testData/codegen/box/delegatedProperty/delegateToAnotherCustom.kt create mode 100644 compiler/testData/codegen/box/delegatedProperty/delegateToAnotherReflection.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 d4ab8066c48..bb9fb4558a8 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 @@ -13198,12 +13198,24 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnother.kt"); } + @Test + @TestMetadata("delegateToAnotherCustom.kt") + public void testDelegateToAnotherCustom() throws Exception { + runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnotherCustom.kt"); + } + @Test @TestMetadata("delegateToAnotherMutable.kt") public void testDelegateToAnotherMutable() throws Exception { runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnotherMutable.kt"); } + @Test + @TestMetadata("delegateToAnotherReflection.kt") + public void testDelegateToAnotherReflection() throws Exception { + runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnotherReflection.kt"); + } + @Test @TestMetadata("delegateToGenericJavaProperty.kt") public void testDelegateToGenericJavaProperty() throws Exception { diff --git a/compiler/testData/codegen/box/delegatedProperty/delegateToAnotherCustom.kt b/compiler/testData/codegen/box/delegatedProperty/delegateToAnotherCustom.kt new file mode 100644 index 00000000000..8d911fdf17c --- /dev/null +++ b/compiler/testData/codegen/box/delegatedProperty/delegateToAnotherCustom.kt @@ -0,0 +1,14 @@ +// WITH_RUNTIME +var x: String + get() = throw Exception("x's getter shouldn't be called") + set(_) { throw Exception("x's setter shouldn't be called") } +var y: String by ::x + +var storage = "Fail" +operator fun Any.getValue(thiz: Any?, property: Any?): String = storage +operator fun Any.setValue(thiz: Any?, property: Any?, value: String) { storage = value } + +fun box(): String { + y = "OK" + return y +} diff --git a/compiler/testData/codegen/box/delegatedProperty/delegateToAnotherReflection.kt b/compiler/testData/codegen/box/delegatedProperty/delegateToAnotherReflection.kt new file mode 100644 index 00000000000..46977f37e2f --- /dev/null +++ b/compiler/testData/codegen/box/delegatedProperty/delegateToAnotherReflection.kt @@ -0,0 +1,51 @@ +// WITH_REFLECT +// TARGET_BACKEND: JVM +import kotlin.reflect.* +import kotlin.reflect.jvm.isAccessible +import kotlin.reflect.full.getExtensionDelegate + +class C(var x: Int) { + var y by C::x + var z by ::x +} + +class D(val c: C) { + var y by c::x +} + +var x = 1 +var y by ::x +var z by C(1)::x + +var _x = -99 +var Int.x + get() = this + _x + set(v: Int) { _x = v - this } +var Int.y by Int::x + +inline fun

, R> P.test(delegate: P.() -> R, get: R.() -> Int, set: R.(Int) -> Unit) { + val ref = apply { isAccessible = true }.delegate() + require(ref.get() == 1) { "$ref initial value is ${ref.get()}, not 1" } + ref.set(2) + require(ref.get() == 2) { "$ref after set(2) is ${ref.get()}, not 2" } + ref.set(1) +} + +fun KMutableProperty0.test() = + test({ getDelegate() as KMutableProperty0 }, { get() }, { set(it) }) + +fun KMutableProperty1.test(receiver: T) = + test({ getDelegate(receiver) as KMutableProperty0 }, { get() }, { set(it) }) + +fun KMutableProperty1.test(receiver: T, receiver2: T) = + test({ getDelegate(receiver) as KMutableProperty1 }, { get(receiver2) }, { set(receiver2, it) }) + +fun box(): String { + C::y.test(C(100), C(1)) + C::z.test(C(1)) + D::y.test(D(C(1))) + ::y.test() + ::z.test() + Int::y.test({ getExtensionDelegate() as KMutableProperty1 }, { get(100) }, { set(100, it) }) + return "OK" +} diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java index b9ee78a9ab5..a72666dfc4b 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java @@ -13198,12 +13198,24 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnother.kt"); } + @Test + @TestMetadata("delegateToAnotherCustom.kt") + public void testDelegateToAnotherCustom() throws Exception { + runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnotherCustom.kt"); + } + @Test @TestMetadata("delegateToAnotherMutable.kt") public void testDelegateToAnotherMutable() throws Exception { runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnotherMutable.kt"); } + @Test + @TestMetadata("delegateToAnotherReflection.kt") + public void testDelegateToAnotherReflection() throws Exception { + runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnotherReflection.kt"); + } + @Test @TestMetadata("delegateToGenericJavaProperty.kt") public void testDelegateToGenericJavaProperty() throws Exception { 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 7e9bf8f6f75..5f67feccf18 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 @@ -13198,12 +13198,24 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnother.kt"); } + @Test + @TestMetadata("delegateToAnotherCustom.kt") + public void testDelegateToAnotherCustom() throws Exception { + runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnotherCustom.kt"); + } + @Test @TestMetadata("delegateToAnotherMutable.kt") public void testDelegateToAnotherMutable() throws Exception { runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnotherMutable.kt"); } + @Test + @TestMetadata("delegateToAnotherReflection.kt") + public void testDelegateToAnotherReflection() throws Exception { + runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnotherReflection.kt"); + } + @Test @TestMetadata("delegateToGenericJavaProperty.kt") public void testDelegateToGenericJavaProperty() throws Exception { diff --git a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index e04aa82b798..f20c3ba613d 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -10722,11 +10722,21 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnother.kt"); } + @TestMetadata("delegateToAnotherCustom.kt") + public void testDelegateToAnotherCustom() throws Exception { + runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnotherCustom.kt"); + } + @TestMetadata("delegateToAnotherMutable.kt") public void testDelegateToAnotherMutable() throws Exception { runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnotherMutable.kt"); } + @TestMetadata("delegateToAnotherReflection.kt") + public void testDelegateToAnotherReflection() throws Exception { + runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnotherReflection.kt"); + } + @TestMetadata("delegateToGenericJavaProperty.kt") public void testDelegateToGenericJavaProperty() throws Exception { runTest("compiler/testData/codegen/box/delegatedProperty/delegateToGenericJavaProperty.kt"); diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java index 5203a703949..5c421a3ab7e 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java @@ -9541,6 +9541,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnother.kt"); } + @TestMetadata("delegateToAnotherCustom.kt") + public void testDelegateToAnotherCustom() throws Exception { + runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnotherCustom.kt"); + } + @TestMetadata("delegateToAnotherMutable.kt") public void testDelegateToAnotherMutable() throws Exception { runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnotherMutable.kt"); diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java index 759517e618a..af47c377b62 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java @@ -8947,6 +8947,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnother.kt"); } + @TestMetadata("delegateToAnotherCustom.kt") + public void testDelegateToAnotherCustom() throws Exception { + runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnotherCustom.kt"); + } + @TestMetadata("delegateToAnotherMutable.kt") public void testDelegateToAnotherMutable() throws Exception { runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnotherMutable.kt"); diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java index 9c05a1a8b48..5e75e99b256 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java @@ -8947,6 +8947,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnother.kt"); } + @TestMetadata("delegateToAnotherCustom.kt") + public void testDelegateToAnotherCustom() throws Exception { + runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnotherCustom.kt"); + } + @TestMetadata("delegateToAnotherMutable.kt") public void testDelegateToAnotherMutable() throws Exception { runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnotherMutable.kt");