[FIR] Calculate lazy initializers in properties with backing fields

More tests should be added in the future
This commit is contained in:
Roman Golyshev
2021-09-14 18:10:51 +03:00
committed by Space
parent bb2b0107c6
commit 642591770e
13 changed files with 119 additions and 1 deletions
@@ -7,7 +7,7 @@ package org.jetbrains.kotlin.analysis.low.level.api.fir.lazy.resolve
import org.jetbrains.kotlin.fir.FirElement
import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.expressions.FirExpression
import org.jetbrains.kotlin.fir.declarations.utils.getExplicitBackingField
import org.jetbrains.kotlin.fir.expressions.FirFunctionCall
import org.jetbrains.kotlin.fir.expressions.FirWrappedDelegateExpression
import org.jetbrains.kotlin.fir.expressions.impl.FirLazyBlock
@@ -92,6 +92,11 @@ internal object FirLazyBodiesCalculator {
firProperty.replaceInitializer(newProperty.initializer)
}
firProperty.getExplicitBackingField()?.takeIf { it.initializer is FirLazyExpression }?.let { backingField ->
val newInitializer = newProperty.getExplicitBackingField()?.initializer
backingField.replaceInitializer(newInitializer)
}
val delegate = firProperty.delegate as? FirWrappedDelegateExpression
val delegateExpression = delegate?.expression
if (delegateExpression is FirLazyExpression) {
@@ -114,6 +119,7 @@ internal object FirLazyBodiesCalculator {
|| firProperty.setter?.body is FirLazyBlock
|| firProperty.initializer is FirLazyExpression
|| (firProperty.delegate as? FirWrappedDelegateExpression)?.expression is FirLazyExpression
|| firProperty.getExplicitBackingField()?.initializer is FirLazyExpression
}
private object FirLazyBodiesCalculatorTransformer : FirTransformer<MutableList<FirDeclaration>>() {
@@ -165,6 +165,18 @@ public class FirLazyBodiesCalculatorTestGenerated extends AbstractFirLazyBodiesC
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/noPrimaryConstructor.kt");
}
@Test
@TestMetadata("propertyWithBackingField.kt")
public void testPropertyWithBackingField() throws Exception {
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/propertyWithBackingField.kt");
}
@Test
@TestMetadata("propertyWithBackingFieldDifferentTypes.kt")
public void testPropertyWithBackingFieldDifferentTypes() throws Exception {
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/propertyWithBackingFieldDifferentTypes.kt");
}
@Test
@TestMetadata("simpleClass.kt")
public void testSimpleClass() throws Exception {
@@ -151,6 +151,16 @@ public class LightTree2FirConverterTestCaseGenerated extends AbstractLightTree2F
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/noPrimaryConstructor.kt");
}
@TestMetadata("propertyWithBackingField.kt")
public void testPropertyWithBackingField() throws Exception {
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/propertyWithBackingField.kt");
}
@TestMetadata("propertyWithBackingFieldDifferentTypes.kt")
public void testPropertyWithBackingFieldDifferentTypes() throws Exception {
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/propertyWithBackingFieldDifferentTypes.kt");
}
@TestMetadata("simpleClass.kt")
public void testSimpleClass() throws Exception {
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/simpleClass.kt");
@@ -0,0 +1,8 @@
fun resolveMe() {
receive(property)
}
val property: Int
field: Int = 10
fun receive(s: Int) {}
@@ -0,0 +1,6 @@
FILE: propertyWithBackingField.kt
public? final? fun resolveMe(): R|kotlin/Unit| { LAZY_BLOCK }
public? final? val property: Int
private <explicit backing field>: Int = LAZY_EXPRESSION
public? get(): Int
public? final? fun receive(s: Int): R|kotlin/Unit| { LAZY_BLOCK }
@@ -0,0 +1,9 @@
FILE: propertyWithBackingField.kt
public? final? fun resolveMe(): R|kotlin/Unit| {
receive#(property#)
}
public? final? val property: Int
private <explicit backing field>: Int = IntegerLiteral(10)
public? get(): Int
public? final? fun receive(s: Int): R|kotlin/Unit| {
}
@@ -0,0 +1,8 @@
fun resolveMe() {
receive(property)
}
val property: Number
field: Int = 10
fun receive(s: Int) {}
@@ -0,0 +1,6 @@
FILE: propertyWithBackingFieldDifferentTypes.kt
public? final? fun resolveMe(): R|kotlin/Unit| { LAZY_BLOCK }
public? final? val property: Number
private <explicit backing field>: Int = LAZY_EXPRESSION
public? get(): Number
public? final? fun receive(s: Int): R|kotlin/Unit| { LAZY_BLOCK }
@@ -0,0 +1,9 @@
FILE: propertyWithBackingFieldDifferentTypes.kt
public? final? fun resolveMe(): R|kotlin/Unit| {
receive#(property#)
}
public? final? val property: Number
private <explicit backing field>: Int = IntegerLiteral(10)
public? get(): Number
public? final? fun receive(s: Int): R|kotlin/Unit| {
}
@@ -151,6 +151,16 @@ public class RawFirBuilderLazyBodiesTestCaseGenerated extends AbstractRawFirBuil
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/noPrimaryConstructor.kt");
}
@TestMetadata("propertyWithBackingField.kt")
public void testPropertyWithBackingField() throws Exception {
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/propertyWithBackingField.kt");
}
@TestMetadata("propertyWithBackingFieldDifferentTypes.kt")
public void testPropertyWithBackingFieldDifferentTypes() throws Exception {
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/propertyWithBackingFieldDifferentTypes.kt");
}
@TestMetadata("simpleClass.kt")
public void testSimpleClass() throws Exception {
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/simpleClass.kt");
@@ -151,6 +151,16 @@ public class RawFirBuilderTestCaseGenerated extends AbstractRawFirBuilderTestCas
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/noPrimaryConstructor.kt");
}
@TestMetadata("propertyWithBackingField.kt")
public void testPropertyWithBackingField() throws Exception {
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/propertyWithBackingField.kt");
}
@TestMetadata("propertyWithBackingFieldDifferentTypes.kt")
public void testPropertyWithBackingFieldDifferentTypes() throws Exception {
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/propertyWithBackingFieldDifferentTypes.kt");
}
@TestMetadata("simpleClass.kt")
public void testSimpleClass() throws Exception {
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/simpleClass.kt");
@@ -165,6 +165,18 @@ public class FirVisualizerForRawFirDataGenerated extends AbstractFirVisualizerTe
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/noPrimaryConstructor.kt");
}
@Test
@TestMetadata("propertyWithBackingField.kt")
public void testPropertyWithBackingField() throws Exception {
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/propertyWithBackingField.kt");
}
@Test
@TestMetadata("propertyWithBackingFieldDifferentTypes.kt")
public void testPropertyWithBackingFieldDifferentTypes() throws Exception {
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/propertyWithBackingFieldDifferentTypes.kt");
}
@Test
@TestMetadata("simpleClass.kt")
public void testSimpleClass() throws Exception {
@@ -165,6 +165,18 @@ public class PsiVisualizerForRawFirDataGenerated extends AbstractPsiVisualizerTe
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/noPrimaryConstructor.kt");
}
@Test
@TestMetadata("propertyWithBackingField.kt")
public void testPropertyWithBackingField() throws Exception {
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/propertyWithBackingField.kt");
}
@Test
@TestMetadata("propertyWithBackingFieldDifferentTypes.kt")
public void testPropertyWithBackingFieldDifferentTypes() throws Exception {
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/propertyWithBackingFieldDifferentTypes.kt");
}
@Test
@TestMetadata("simpleClass.kt")
public void testSimpleClass() throws Exception {