[FIR] Don't create backing field for property without explicit type

#KT-41977 Fixed
This commit is contained in:
Dmitriy Novozhilov
2020-09-29 10:09:07 +03:00
parent 0c0a8f9849
commit fb871a55a3
8 changed files with 59 additions and 5 deletions
@@ -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.<!UNRESOLVED_REFERENCE!>length<!> // should be an error
}
@@ -0,0 +1,29 @@
FILE: noBackingFieldInProperty.kt
public final class A : R|kotlin/Any| {
public constructor(): R|A| {
super<R|kotlin/Any|>()
}
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<R|kotlin/Any|>()
}
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|.<Unresolved name: length>#
}
}
@@ -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");
@@ -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");
@@ -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()
@@ -5,7 +5,7 @@ val z get(): List<Int> {
return bar()
}
val u get(): String = field
val u get(): String = <!UNRESOLVED_REFERENCE!>field<!>
fun <E> foo(): E = null!!
fun <E> bar(): List<E> = null!!
@@ -13,7 +13,7 @@ class A {
val z1 get() = <!INAPPLICABLE_CANDIDATE!>id<!>(z1)
val z2 get() = <!INAPPLICABLE_CANDIDATE!>l<!>(z2)
val u get() = field
val u get() = <!UNRESOLVED_REFERENCE!>field<!>
}
fun <E> id(x: E) = x
@@ -21,7 +21,7 @@ val z2 get(): List<Int> {
return bar()
}
val u get(): String = field
val u get(): String = <!UNRESOLVED_REFERENCE!>field<!>
// members.kt
class A {
@@ -68,4 +68,4 @@ object Outer {
set(q) {
x = q
}
}
}