From fb871a55a392efa17830508ca24ed1f48ec74755 Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Tue, 29 Sep 2020 10:09:07 +0300 Subject: [PATCH] [FIR] Don't create backing field for property without explicit type #KT-41977 Fixed --- .../properties/noBackingFieldInProperty.kt | 15 ++++++++++ .../properties/noBackingFieldInProperty.txt | 29 +++++++++++++++++++ .../fir/FirDiagnosticsTestGenerated.java | 5 ++++ ...DiagnosticsWithLightTreeTestGenerated.java | 5 ++++ .../FirDeclarationsResolveTransformer.kt | 2 +- .../explicitGetterType.fir.kt | 2 +- .../recursiveGetter.fir.kt | 2 +- .../unsupportedInferenceFromGetters.fir.kt | 4 +-- 8 files changed, 59 insertions(+), 5 deletions(-) create mode 100644 compiler/fir/analysis-tests/testData/resolve/properties/noBackingFieldInProperty.kt create mode 100644 compiler/fir/analysis-tests/testData/resolve/properties/noBackingFieldInProperty.txt diff --git a/compiler/fir/analysis-tests/testData/resolve/properties/noBackingFieldInProperty.kt b/compiler/fir/analysis-tests/testData/resolve/properties/noBackingFieldInProperty.kt new file mode 100644 index 00000000000..092c73a2786 --- /dev/null +++ b/compiler/fir/analysis-tests/testData/resolve/properties/noBackingFieldInProperty.kt @@ -0,0 +1,15 @@ +// ISSUE: KT-41977 + +class A { + val field: String = "" // (1) + + val x + get() = field.length // should be ok, resolve to (1) +} + +class B { + val field: String = "" + + val x: Int + get() = field.length // should be an error +} diff --git a/compiler/fir/analysis-tests/testData/resolve/properties/noBackingFieldInProperty.txt b/compiler/fir/analysis-tests/testData/resolve/properties/noBackingFieldInProperty.txt new file mode 100644 index 00000000000..3a935e7dff4 --- /dev/null +++ b/compiler/fir/analysis-tests/testData/resolve/properties/noBackingFieldInProperty.txt @@ -0,0 +1,29 @@ +FILE: noBackingFieldInProperty.kt + public final class A : R|kotlin/Any| { + public constructor(): R|A| { + super() + } + + public final val field: R|kotlin/String| = String() + public get(): R|kotlin/String| + + public final val x: R|kotlin/Int| + public get(): R|kotlin/Int| { + ^ this@R|/A|.R|/A.field|.R|kotlin/String.length| + } + + } + public final class B : R|kotlin/Any| { + public constructor(): R|B| { + super() + } + + public final val field: R|kotlin/String| = String() + public get(): R|kotlin/String| + + public final val x: R|kotlin/Int| + public get(): R|kotlin/Int| { + ^ this@R|/B|.F|/B.x|.# + } + + } diff --git a/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirDiagnosticsTestGenerated.java index 52742ffa35b..bcf53399382 100644 --- a/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirDiagnosticsTestGenerated.java @@ -2062,6 +2062,11 @@ public class FirDiagnosticsTestGenerated extends AbstractFirDiagnosticsTest { runTest("compiler/fir/analysis-tests/testData/resolve/properties/noBackingFieldForExtension.kt"); } + @TestMetadata("noBackingFieldInProperty.kt") + public void testNoBackingFieldInProperty() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/properties/noBackingFieldInProperty.kt"); + } + @TestMetadata("syntheticPropertiesForJavaAnnotations.kt") public void testSyntheticPropertiesForJavaAnnotations() throws Exception { runTest("compiler/fir/analysis-tests/testData/resolve/properties/syntheticPropertiesForJavaAnnotations.kt"); diff --git a/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirDiagnosticsWithLightTreeTestGenerated.java b/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirDiagnosticsWithLightTreeTestGenerated.java index 11cac10af19..4af78f35bac 100644 --- a/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirDiagnosticsWithLightTreeTestGenerated.java +++ b/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirDiagnosticsWithLightTreeTestGenerated.java @@ -2062,6 +2062,11 @@ public class FirDiagnosticsWithLightTreeTestGenerated extends AbstractFirDiagnos runTest("compiler/fir/analysis-tests/testData/resolve/properties/noBackingFieldForExtension.kt"); } + @TestMetadata("noBackingFieldInProperty.kt") + public void testNoBackingFieldInProperty() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/properties/noBackingFieldInProperty.kt"); + } + @TestMetadata("syntheticPropertiesForJavaAnnotations.kt") public void testSyntheticPropertiesForJavaAnnotations() throws Exception { runTest("compiler/fir/analysis-tests/testData/resolve/properties/syntheticPropertiesForJavaAnnotations.kt"); diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirDeclarationsResolveTransformer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirDeclarationsResolveTransformer.kt index e300cbe41a1..135d0eb633b 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirDeclarationsResolveTransformer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirDeclarationsResolveTransformer.kt @@ -143,7 +143,7 @@ open class FirDeclarationsResolveTransformer(transformer: FirBodyResolveTransfor storeVariableReturnType(property) } withLocalScopeCleanup { - if (property.receiverTypeRef == null) { + if (property.receiverTypeRef == null && property.returnTypeRef !is FirImplicitTypeRef) { addLocalScope(FirLocalScope().storeBackingField(property)) } property.transformAccessors() diff --git a/compiler/testData/diagnostics/tests/properties/inferenceFromGetters/explicitGetterType.fir.kt b/compiler/testData/diagnostics/tests/properties/inferenceFromGetters/explicitGetterType.fir.kt index 3e5e552b101..138dc1ded67 100644 --- a/compiler/testData/diagnostics/tests/properties/inferenceFromGetters/explicitGetterType.fir.kt +++ b/compiler/testData/diagnostics/tests/properties/inferenceFromGetters/explicitGetterType.fir.kt @@ -5,7 +5,7 @@ val z get(): List { return bar() } -val u get(): String = field +val u get(): String = field fun foo(): E = null!! fun bar(): List = null!! diff --git a/compiler/testData/diagnostics/tests/properties/inferenceFromGetters/recursiveGetter.fir.kt b/compiler/testData/diagnostics/tests/properties/inferenceFromGetters/recursiveGetter.fir.kt index 672898f39a8..97deba12572 100644 --- a/compiler/testData/diagnostics/tests/properties/inferenceFromGetters/recursiveGetter.fir.kt +++ b/compiler/testData/diagnostics/tests/properties/inferenceFromGetters/recursiveGetter.fir.kt @@ -13,7 +13,7 @@ class A { val z1 get() = id(z1) val z2 get() = l(z2) - val u get() = field + val u get() = field } fun id(x: E) = x diff --git a/compiler/testData/diagnostics/tests/properties/inferenceFromGetters/unsupportedInferenceFromGetters.fir.kt b/compiler/testData/diagnostics/tests/properties/inferenceFromGetters/unsupportedInferenceFromGetters.fir.kt index ba0c9f422bf..68531f82a31 100644 --- a/compiler/testData/diagnostics/tests/properties/inferenceFromGetters/unsupportedInferenceFromGetters.fir.kt +++ b/compiler/testData/diagnostics/tests/properties/inferenceFromGetters/unsupportedInferenceFromGetters.fir.kt @@ -21,7 +21,7 @@ val z2 get(): List { return bar() } -val u get(): String = field +val u get(): String = field // members.kt class A { @@ -68,4 +68,4 @@ object Outer { set(q) { x = q } -} \ No newline at end of file +}