K2: Propagate explicit getter type to the property without initializer

In K1, we have the rules like:
- if there's explicit type of a property, then use it
- if there's an initializer, obtain its expression-type
- Otherwise, use getter's return type

The case when getter's type is implicit is handled at
FirDeclarationsResolveTransformer.transformProperty

^KT-56707 Fixed
This commit is contained in:
Denis.Zharkov
2023-02-20 16:53:33 +01:00
committed by Space Team
parent c09607c791
commit 3f052af517
10 changed files with 96 additions and 20 deletions
@@ -25056,12 +25056,24 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag
runTest("compiler/testData/diagnostics/tests/properties/extensionPropertyMustHaveAccessorsOrBeAbstract.kt");
}
@Test
@TestMetadata("inferPropertyTypeFromGetter.kt")
public void testInferPropertyTypeFromGetter() throws Exception {
runTest("compiler/testData/diagnostics/tests/properties/inferPropertyTypeFromGetter.kt");
}
@Test
@TestMetadata("kt47621.kt")
public void testKt47621() throws Exception {
runTest("compiler/testData/diagnostics/tests/properties/kt47621.kt");
}
@Test
@TestMetadata("kt56707.kt")
public void testKt56707() throws Exception {
runTest("compiler/testData/diagnostics/tests/properties/kt56707.kt");
}
@Test
@TestMetadata("lateinitOnTopLevel.kt")
public void testLateinitOnTopLevel() throws Exception {
@@ -25062,12 +25062,24 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti
runTest("compiler/testData/diagnostics/tests/properties/extensionPropertyMustHaveAccessorsOrBeAbstract.kt");
}
@Test
@TestMetadata("inferPropertyTypeFromGetter.kt")
public void testInferPropertyTypeFromGetter() throws Exception {
runTest("compiler/testData/diagnostics/tests/properties/inferPropertyTypeFromGetter.kt");
}
@Test
@TestMetadata("kt47621.kt")
public void testKt47621() throws Exception {
runTest("compiler/testData/diagnostics/tests/properties/kt47621.kt");
}
@Test
@TestMetadata("kt56707.kt")
public void testKt56707() throws Exception {
runTest("compiler/testData/diagnostics/tests/properties/kt56707.kt");
}
@Test
@TestMetadata("lateinitOnTopLevel.kt")
public void testLateinitOnTopLevel() throws Exception {
@@ -25056,12 +25056,24 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac
runTest("compiler/testData/diagnostics/tests/properties/extensionPropertyMustHaveAccessorsOrBeAbstract.kt");
}
@Test
@TestMetadata("inferPropertyTypeFromGetter.kt")
public void testInferPropertyTypeFromGetter() throws Exception {
runTest("compiler/testData/diagnostics/tests/properties/inferPropertyTypeFromGetter.kt");
}
@Test
@TestMetadata("kt47621.kt")
public void testKt47621() throws Exception {
runTest("compiler/testData/diagnostics/tests/properties/kt47621.kt");
}
@Test
@TestMetadata("kt56707.kt")
public void testKt56707() throws Exception {
runTest("compiler/testData/diagnostics/tests/properties/kt56707.kt");
}
@Test
@TestMetadata("lateinitOnTopLevel.kt")
public void testLateinitOnTopLevel() throws Exception {
@@ -6,7 +6,9 @@
package org.jetbrains.kotlin.fir.resolve.transformers
import kotlinx.collections.immutable.toImmutableList
import org.jetbrains.kotlin.KtFakeSourceElementKind
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.copyWithNewSourceKind
import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.declarations.utils.isFromVararg
import org.jetbrains.kotlin.fir.expressions.*
@@ -154,8 +156,16 @@ open class FirTypeResolveTransformer(
setAccessorTypesByPropertyType(property)
}
if (property.returnTypeRef is FirResolvedTypeRef && property.delegate != null) {
setAccessorTypesByPropertyType(property)
when {
property.returnTypeRef is FirResolvedTypeRef && property.delegate != null -> {
setAccessorTypesByPropertyType(property)
}
property.returnTypeRef !is FirResolvedTypeRef && property.initializer == null &&
property.getter?.returnTypeRef is FirResolvedTypeRef -> {
property.replaceReturnTypeRef(
property.getter!!.returnTypeRef.copyWithNewSourceKind(KtFakeSourceElementKind.PropertyTypeFromGetterReturnType)
)
}
}
unboundCyclesInTypeParametersSupertypes(property)
@@ -262,6 +262,9 @@ sealed class KtFakeSourceElementKind(final override val shouldSkipErrorTypeRepor
// Synthetic calls for if/when/try/etc.
object SyntheticCall : KtFakeSourceElementKind()
// When property doesn't have an initializer and explicit return type, but its getter's return type is specified
object PropertyTypeFromGetterReturnType : KtFakeSourceElementKind()
}
sealed class AbstractKtSourceElement {
@@ -0,0 +1,19 @@
// FIR_IDENTICAL
// ISSUE: KT-56707
class Foo0 {
val child = 1
val allChildren
get(): Int = child + allChildren // Should not be TYPECHECKER_HAS_RUN_INTO_RECURSIVE_PROBLEM on `allChildren` reference
}
class Foo1 {
val child = 1
val allChildren
get() = child + 1
}
fun use() {
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Int")!>Foo0().allChildren<!>
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Int")!>Foo1().allChildren<!>
}
@@ -1,18 +0,0 @@
// !CHECK_TYPE
val x get(): String = foo()
val y get(): List<Int> = bar()
val z get(): List<Int> {
return bar()
}
val u get(): String = <!UNRESOLVED_REFERENCE!>field<!>
fun <E> foo(): E = null!!
fun <E> bar(): List<E> = null!!
fun baz() {
x checkType { _<String>() }
y checkType { _<List<Int>>() }
z checkType { _<List<Int>>() }
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !CHECK_TYPE
val x get(): String = foo()
val y get(): List<Int> = bar()
@@ -0,0 +1,13 @@
// FIR_IDENTICAL
// WITH_STDLIB
// ISSUE: KT-56707
class Foo {
val children = mutableSetOf<Foo>()
val allChildren
get() : Set<Foo> = (children + children.flatMap { it.allChildren }).toSet() // Should not be TYPECHECKER_HAS_RUN_INTO_RECURSIVE_PROBLEM on `allChildren` reference
}
fun use() {
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.collections.Set<Foo>")!>Foo().allChildren<!>
}
@@ -25062,12 +25062,24 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest {
runTest("compiler/testData/diagnostics/tests/properties/extensionPropertyMustHaveAccessorsOrBeAbstract.kt");
}
@Test
@TestMetadata("inferPropertyTypeFromGetter.kt")
public void testInferPropertyTypeFromGetter() throws Exception {
runTest("compiler/testData/diagnostics/tests/properties/inferPropertyTypeFromGetter.kt");
}
@Test
@TestMetadata("kt47621.kt")
public void testKt47621() throws Exception {
runTest("compiler/testData/diagnostics/tests/properties/kt47621.kt");
}
@Test
@TestMetadata("kt56707.kt")
public void testKt56707() throws Exception {
runTest("compiler/testData/diagnostics/tests/properties/kt56707.kt");
}
@Test
@TestMetadata("lateinitOnTopLevel.kt")
public void testLateinitOnTopLevel() throws Exception {