From 6f6e37f55227d34482f5e84fea434767e8735651 Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Mon, 26 Feb 2024 13:44:03 +0200 Subject: [PATCH] [FIR] Report about recursion in implicit types for receivers of invoke ^KT-65576 Fixed --- .../tower/FirInvokeResolveTowerExtension.kt | 14 ++++--- .../tests/delegation/kt44843.fir.kt | 4 +- .../specialCallsWithCallableReferences.fir.kt | 38 +++++++++---------- ...citInvokeOnPropertyInItsInitializer.fir.kt | 4 +- 4 files changed, 32 insertions(+), 28 deletions(-) diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/tower/FirInvokeResolveTowerExtension.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/tower/FirInvokeResolveTowerExtension.kt index f0515c5489b..39b01973fc9 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/tower/FirInvokeResolveTowerExtension.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/tower/FirInvokeResolveTowerExtension.kt @@ -17,10 +17,7 @@ import org.jetbrains.kotlin.fir.resolve.* import org.jetbrains.kotlin.fir.resolve.calls.* import org.jetbrains.kotlin.fir.resolve.diagnostics.ConePropertyAsOperator import org.jetbrains.kotlin.fir.symbols.impl.* -import org.jetbrains.kotlin.fir.types.ConeClassLikeType -import org.jetbrains.kotlin.fir.types.coneTypeUnsafe -import org.jetbrains.kotlin.fir.types.isExtensionFunctionType -import org.jetbrains.kotlin.fir.types.typeApproximator +import org.jetbrains.kotlin.fir.types.* import org.jetbrains.kotlin.resolve.calls.tasks.ExplicitReceiverKind import org.jetbrains.kotlin.resolve.calls.tower.CandidateApplicability import org.jetbrains.kotlin.types.AbstractTypeChecker @@ -317,15 +314,22 @@ private fun BodyResolveComponents.createExplicitReceiverForInvokeByCallable( ): FirExpression { return FirPropertyAccessExpressionBuilder().apply { val fakeSource = info.fakeSourceForImplicitInvokeCallReceiver + val returnTypeRef = returnTypeCalculator.tryCalculateReturnType(symbol.fir) calleeReference = when { + returnTypeRef is FirErrorTypeRef -> FirErrorReferenceWithCandidate( + fakeSource, symbol.callableId.callableName, candidate, returnTypeRef.diagnostic, + ) + candidate.isSuccessful -> FirNamedReferenceWithCandidate(fakeSource, symbol.callableId.callableName, candidate) + else -> FirErrorReferenceWithCandidate( fakeSource, symbol.callableId.callableName, candidate, createConeDiagnosticForCandidateWithError(candidate.applicability, candidate), ) } dispatchReceiver = candidate.dispatchReceiverExpression() - coneTypeOrNull = returnTypeCalculator.tryCalculateReturnType(symbol.fir).type + + coneTypeOrNull = returnTypeRef.type if (!invokeBuiltinExtensionMode) { extensionReceiver = extensionReceiverExpression diff --git a/compiler/testData/diagnostics/tests/delegation/kt44843.fir.kt b/compiler/testData/diagnostics/tests/delegation/kt44843.fir.kt index b00f001dd65..245ba420481 100644 --- a/compiler/testData/diagnostics/tests/delegation/kt44843.fir.kt +++ b/compiler/testData/diagnostics/tests/delegation/kt44843.fir.kt @@ -1,13 +1,13 @@ // WITH_STDLIB // FILE: test.kt -val bar2 by bar2() +val bar2 by bar2() // FILE: lt/neworld/compiler/Foo.kt package lt.neworld.compiler class Foo { - val bar by bar() + val bar by bar() } // FILE: lt/neworld/compiler/bar/Bar.kt diff --git a/compiler/testData/diagnostics/tests/inference/specialCallsWithCallableReferences.fir.kt b/compiler/testData/diagnostics/tests/inference/specialCallsWithCallableReferences.fir.kt index 8cb33e95476..49f9132003b 100644 --- a/compiler/testData/diagnostics/tests/inference/specialCallsWithCallableReferences.fir.kt +++ b/compiler/testData/diagnostics/tests/inference/specialCallsWithCallableReferences.fir.kt @@ -35,7 +35,7 @@ fun poll11(flag: Boolean) { fun poll12(flag: Boolean) { val inv = if (flag) { ::bar3 } else { ::foo3 } - inv() + inv() } fun poll13(flag: Boolean) { @@ -45,12 +45,12 @@ fun poll13(flag: Boolean) { fun poll14(flag: Boolean) { val inv = if (flag) { ::bar4 } else { ::foo4 } - inv() + inv() } fun poll15(flag: Boolean) { val inv = if (flag) { ::bar5 } else { ::foo5 } - inv() + inv() } fun poll16(flag: Boolean) { @@ -65,7 +65,7 @@ fun poll17(flag: Boolean) { fun poll2(flag: Boolean) { val inv = when (flag) { true -> ::bar else -> ::foo } - inv() + inv() } fun poll21(flag: Boolean) { @@ -75,12 +75,12 @@ fun poll21(flag: Boolean) { fun poll22(flag: Boolean) { val inv = when (flag) { true -> ::bar3 else -> ::foo3 } - inv() + inv() } fun poll23(flag: Boolean) { val inv = when (flag) { true -> ::bar4 else -> ::foo4 } - inv() + inv() } fun poll24(flag: Boolean) { @@ -100,7 +100,7 @@ fun poll26(flag: Boolean) { fun poll3(flag: Boolean) { val inv = when (flag) { true -> ::bar false -> ::foo } - inv() + inv() } fun poll31(flag: Boolean) { @@ -110,12 +110,12 @@ fun poll31(flag: Boolean) { fun poll32(flag: Boolean) { val inv = when (flag) { true -> ::bar3 false -> ::foo3 } - inv() + inv() } fun poll33(flag: Boolean) { val inv = when (flag) { true -> ::bar4 false -> ::foo4 } - inv() + inv() } fun poll34(flag: Boolean) { @@ -135,7 +135,7 @@ fun poll36(flag: Boolean) { fun poll4() { val inv = try { ::bar } finally { ::foo } - inv() + inv() } fun poll41() { @@ -145,17 +145,17 @@ fun poll41() { fun poll42() { val inv = try { ::bar3 } finally { ::foo3 } - inv() + inv() } fun poll43() { val inv = try { ::bar4 } finally { ::foo4 } - inv() + inv() } fun poll44() { val inv = try { ::bar5 } finally { ::foo5 } - inv() + inv() } fun poll45() { @@ -170,7 +170,7 @@ fun poll46() { fun poll5() { val inv = try { ::bar } catch (e: Exception) { ::foo } finally { ::foo } - inv() + inv() } fun poll51() { @@ -180,17 +180,17 @@ fun poll51() { fun poll52() { val inv = try { ::bar3 } catch (e: Exception) { ::foo3 } finally { ::foo3 } - inv() + inv() } fun poll53() { val inv = try { ::bar4 } catch (e: Exception) { ::foo4 } finally { ::foo4 } - inv() + inv() } fun poll54() { val inv = try { ::bar5 } catch (e: Exception) { ::foo5 } finally { ::foo5 } - inv() + inv() } fun poll55() { @@ -240,7 +240,7 @@ fun poll66() { fun poll7() { val inv = ::bar!! - inv() + inv() } fun poll71() { @@ -250,7 +250,7 @@ fun poll71() { fun poll72() { val inv = ::bar3!! - inv() + inv() } fun poll73() { diff --git a/compiler/testData/diagnostics/tests/operatorsOverloading/implicitInvokeOnPropertyInItsInitializer.fir.kt b/compiler/testData/diagnostics/tests/operatorsOverloading/implicitInvokeOnPropertyInItsInitializer.fir.kt index 96f93668bbe..e3555c48c1e 100644 --- a/compiler/testData/diagnostics/tests/operatorsOverloading/implicitInvokeOnPropertyInItsInitializer.fir.kt +++ b/compiler/testData/diagnostics/tests/operatorsOverloading/implicitInvokeOnPropertyInItsInitializer.fir.kt @@ -5,7 +5,7 @@ fun foo(): Int = 0 object Implicit { operator fun Any.invoke(): String = "Fail" - val foo = foo() + val foo = foo() } object Explicit { @@ -27,7 +27,7 @@ object ImplicitIndirect { val foo get() = bar() val bar get() = baz() - val baz get() = foo() + val baz get() = foo() } fun takeInt(x: Int) {}