JVM IR: keep property for $receiver field of optimized delegated properties

For properties which delegate to other property via the getValue
operator from stdlib (see KT-39054), we generate `$receiver` field which
stores the receiver of the property reference used in the delegate. The
problem was that this backing field was missing `correspondingProperty`.
It's needed because it is used as a map key to store static fields in
`JvmCachedDeclarations.getStaticBackingField`. If it's null, accesses to
the static $receiver fields are not remapped correctly in
`RemapObjectFieldAccesses` and
`MoveOrCopyCompanionObjectFieldsLowering`, which led to ICCE or NSFE.

 #KT-49793 Fixed
This commit is contained in:
Alexander Udalov
2021-11-26 02:48:51 +01:00
parent 29c1fe1be1
commit 4df937ff7f
11 changed files with 168 additions and 0 deletions
@@ -13997,6 +13997,24 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnother/genericJavaProperty.kt");
}
@Test
@TestMetadata("kt49793_companionObject.kt")
public void testKt49793_companionObject() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnother/kt49793_companionObject.kt");
}
@Test
@TestMetadata("kt49793_interfaceCompanionObject.kt")
public void testKt49793_interfaceCompanionObject() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnother/kt49793_interfaceCompanionObject.kt");
}
@Test
@TestMetadata("kt49793_object.kt")
public void testKt49793_object() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnother/kt49793_object.kt");
}
@Test
@TestMetadata("mutable.kt")
public void testMutable() throws Exception {
@@ -145,6 +145,7 @@ private class PropertyReferenceDelegationTransformer(val context: JvmBackendCont
}.apply {
parent = oldField.parent
initializer = context.irFactory.createExpressionBody(it)
correspondingPropertySymbol = oldField.correspondingPropertySymbol
}
}
val originalThis = parentAsClass.thisReceiver
@@ -0,0 +1,14 @@
// WITH_STDLIB
class Z(val x: String = "OK")
operator fun Z.getValue(x: Any?, y: Any?): Z = this
class O {
companion object {
val instance: Z by Z()
val y by instance::x
}
}
fun box(): String = O.y
@@ -0,0 +1,18 @@
// WITH_STDLIB
class Z(var x: String = "Fail")
operator fun Z.getValue(x: Any?, y: Any?): Z = this
operator fun Z.setValue(x: Any?, y: Any?, value: Z) { this.x = value.x }
interface O {
companion object {
val instance: Z by Z()
var y by instance::x
}
}
fun box(): String {
O.y = "OK"
return O.y
}
@@ -0,0 +1,12 @@
// WITH_STDLIB
class Z(val x: String = "OK")
operator fun Z.getValue(x: Any?, y: Any?): Z = this
object O {
val instance: Z by Z()
val y by instance::x
}
fun box(): String = O.y
@@ -13919,6 +13919,24 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnother/genericJavaProperty.kt");
}
@Test
@TestMetadata("kt49793_companionObject.kt")
public void testKt49793_companionObject() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnother/kt49793_companionObject.kt");
}
@Test
@TestMetadata("kt49793_interfaceCompanionObject.kt")
public void testKt49793_interfaceCompanionObject() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnother/kt49793_interfaceCompanionObject.kt");
}
@Test
@TestMetadata("kt49793_object.kt")
public void testKt49793_object() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnother/kt49793_object.kt");
}
@Test
@TestMetadata("mutable.kt")
public void testMutable() throws Exception {
@@ -13997,6 +13997,24 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnother/genericJavaProperty.kt");
}
@Test
@TestMetadata("kt49793_companionObject.kt")
public void testKt49793_companionObject() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnother/kt49793_companionObject.kt");
}
@Test
@TestMetadata("kt49793_interfaceCompanionObject.kt")
public void testKt49793_interfaceCompanionObject() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnother/kt49793_interfaceCompanionObject.kt");
}
@Test
@TestMetadata("kt49793_object.kt")
public void testKt49793_object() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnother/kt49793_object.kt");
}
@Test
@TestMetadata("mutable.kt")
public void testMutable() throws Exception {
@@ -11336,6 +11336,21 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnother/genericJavaProperty.kt");
}
@TestMetadata("kt49793_companionObject.kt")
public void testKt49793_companionObject() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnother/kt49793_companionObject.kt");
}
@TestMetadata("kt49793_interfaceCompanionObject.kt")
public void testKt49793_interfaceCompanionObject() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnother/kt49793_interfaceCompanionObject.kt");
}
@TestMetadata("kt49793_object.kt")
public void testKt49793_object() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnother/kt49793_object.kt");
}
@TestMetadata("mutable.kt")
public void testMutable() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnother/mutable.kt");
@@ -10663,6 +10663,24 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnother/custom.kt");
}
@Test
@TestMetadata("kt49793_companionObject.kt")
public void testKt49793_companionObject() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnother/kt49793_companionObject.kt");
}
@Test
@TestMetadata("kt49793_interfaceCompanionObject.kt")
public void testKt49793_interfaceCompanionObject() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnother/kt49793_interfaceCompanionObject.kt");
}
@Test
@TestMetadata("kt49793_object.kt")
public void testKt49793_object() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnother/kt49793_object.kt");
}
@Test
@TestMetadata("mutable.kt")
public void testMutable() throws Exception {
@@ -10705,6 +10705,24 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnother/custom.kt");
}
@Test
@TestMetadata("kt49793_companionObject.kt")
public void testKt49793_companionObject() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnother/kt49793_companionObject.kt");
}
@Test
@TestMetadata("kt49793_interfaceCompanionObject.kt")
public void testKt49793_interfaceCompanionObject() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnother/kt49793_interfaceCompanionObject.kt");
}
@Test
@TestMetadata("kt49793_object.kt")
public void testKt49793_object() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnother/kt49793_object.kt");
}
@Test
@TestMetadata("mutable.kt")
public void testMutable() throws Exception {
@@ -14148,6 +14148,24 @@ public class NativeExtBlackBoxTestGenerated extends AbstractNativeBlackBoxTest {
runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnother/genericJavaProperty.kt");
}
@Test
@TestMetadata("kt49793_companionObject.kt")
public void testKt49793_companionObject() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnother/kt49793_companionObject.kt");
}
@Test
@TestMetadata("kt49793_interfaceCompanionObject.kt")
public void testKt49793_interfaceCompanionObject() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnother/kt49793_interfaceCompanionObject.kt");
}
@Test
@TestMetadata("kt49793_object.kt")
public void testKt49793_object() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/delegateToAnother/kt49793_object.kt");
}
@Test
@TestMetadata("mutable.kt")
public void testMutable() throws Exception {