[FE 1.0] Take care callable reference candidates with recursive candidate return type

^KT-51844 Fixed
This commit is contained in:
Victor Petukhov
2022-04-17 11:35:28 +03:00
committed by teamcity
parent ec6ec20728
commit 9f31f074da
34 changed files with 218 additions and 55 deletions
@@ -8,9 +8,11 @@ package org.jetbrains.kotlin.resolve.calls.components
import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemBuilder
import org.jetbrains.kotlin.resolve.calls.inference.components.NewTypeSubstitutor
import org.jetbrains.kotlin.resolve.calls.model.*
import org.jetbrains.kotlin.resolve.calls.tower.RecursiveCallableReferenceType
import org.jetbrains.kotlin.resolve.calls.tower.VisibilityError
import org.jetbrains.kotlin.resolve.calls.tower.VisibilityErrorOnArgument
import org.jetbrains.kotlin.resolve.calls.tower.isInapplicable
import org.jetbrains.kotlin.resolve.calls.model.TypeCheckerHasRanIntoRecursion
class CallableReferenceArgumentResolver(val callableReferenceOverloadConflictResolver: CallableReferenceOverloadConflictResolver) {
fun processCallableReferenceArgument(
@@ -46,6 +48,7 @@ class CallableReferenceArgumentResolver(val callableReferenceOverloadConflictRes
val transformedDiagnostic = when (it) {
is CompatibilityWarning -> CompatibilityWarningOnArgument(argument, it.candidate)
is VisibilityError -> VisibilityErrorOnArgument(argument, it.invisibleMember)
is RecursiveCallableReferenceType -> TypeCheckerHasRanIntoRecursion(argument)
else -> it
}
diagnosticsHolder.addDiagnostic(transformedDiagnostic)
@@ -273,7 +273,7 @@ class KotlinCallCompleter(
constraintSystem.errors.forEach(diagnosticsHolder::addError)
if (returnType is ErrorType && returnType.kind == ErrorTypeKind.RECURSIVE_TYPE) {
diagnosticsHolder.addDiagnostic(TypeCheckerHasRanIntoRecursion)
diagnosticsHolder.addDiagnostic(TypeCheckerHasRanIntoRecursion())
}
}
@@ -18,15 +18,15 @@ import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintStorage
import org.jetbrains.kotlin.resolve.calls.inference.model.TypeVariableTypeConstructor
import org.jetbrains.kotlin.resolve.calls.tasks.ExplicitReceiverKind
import org.jetbrains.kotlin.resolve.calls.tower.*
import org.jetbrains.kotlin.resolve.calls.util.ErrorCandidateReason
import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
import org.jetbrains.kotlin.resolve.descriptorUtil.isCompanionObject
import org.jetbrains.kotlin.resolve.scopes.receivers.DetailedReceiver
import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue
import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValueWithSmartCastInfo
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.error.ErrorScopeKind
import org.jetbrains.kotlin.types.error.*
import org.jetbrains.kotlin.types.error.ErrorUtils
import org.jetbrains.kotlin.types.error.ErrorTypeKind
import org.jetbrains.kotlin.types.expressions.CoercionStrategy
import org.jetbrains.kotlin.types.typeUtil.isUnit
import org.jetbrains.kotlin.utils.SmartList
@@ -43,7 +43,7 @@ class CallableReferencesCandidateFactory(
private val CallableReceiver.asReceiverValueForVisibilityChecks: ReceiverValue
get() = receiver.receiverValue
override fun createErrorCandidate(): CallableReferenceResolutionCandidate {
override fun createErrorCandidate(reason: ErrorCandidateReason): CallableReferenceResolutionCandidate {
val errorScope = ErrorUtils.createErrorScope(ErrorScopeKind.SCOPE_FOR_ERROR_RESOLUTION_CANDIDATE, kotlinCall.toString())
val errorDescriptor = errorScope.getContributedFunctions(kotlinCall.rhsName, scopeTower.location).first()
@@ -56,21 +56,36 @@ class CallableReferencesCandidateFactory(
buildTypeWithConversions = kotlinCall is CallableReferenceKotlinCallArgument
)
return CallableReferenceResolutionCandidate(
val candidate = CallableReferenceResolutionCandidate(
errorDescriptor, dispatchReceiver = null, extensionReceiver = null,
ExplicitReceiverKind.NO_EXPLICIT_RECEIVER, reflectionCandidateType, callableReferenceAdaptation,
kotlinCall, expectedType, callComponents, scopeTower, resolutionCallbacks, baseSystem
)
when (reason) {
ErrorCandidateReason.TYPE_COMPUTATION_RECURSION -> candidate.addDiagnostic(RecursiveCallableReferenceType)
ErrorCandidateReason.OTHER -> {}
}
return candidate
}
override fun createCandidate(
private fun KotlinType.isErrorRecursiveType(): Boolean {
val unwrapped = if (this is WrappedType && isComputed()) unwrap() else this
return unwrapped is ErrorType && unwrapped.kind == ErrorTypeKind.RECURSIVE_TYPE
}
private fun createCandidateInternal(
towerCandidate: CandidateWithBoundDispatchReceiver,
explicitReceiverKind: ExplicitReceiverKind,
extensionReceiver: ReceiverValueWithSmartCastInfo?
): CallableReferenceResolutionCandidate {
val dispatchCallableReceiver =
towerCandidate.dispatchReceiver?.let { toCallableReceiver(it, explicitReceiverKind == ExplicitReceiverKind.DISPATCH_RECEIVER) }
val extensionCallableReceiver = extensionReceiver?.let { toCallableReceiver(it, explicitReceiverKind == ExplicitReceiverKind.EXTENSION_RECEIVER) }
val dispatchCallableReceiver = towerCandidate.dispatchReceiver?.let {
toCallableReceiver(it, explicitReceiverKind == ExplicitReceiverKind.DISPATCH_RECEIVER)
}
val extensionCallableReceiver = extensionReceiver?.let {
toCallableReceiver(it, explicitReceiverKind == ExplicitReceiverKind.EXTENSION_RECEIVER)
}
val candidateDescriptor = towerCandidate.descriptor
val diagnostics = SmartList<KotlinCallDiagnostic>()
@@ -122,6 +137,30 @@ class CallableReferencesCandidateFactory(
return createCallableReferenceCallCandidate(diagnostics)
}
override fun createCandidate(
towerCandidate: CandidateWithBoundDispatchReceiver,
explicitReceiverKind: ExplicitReceiverKind,
extensionReceiver: ReceiverValueWithSmartCastInfo?
): CallableReferenceResolutionCandidate =
createRecursionTolerantCandidate(towerCandidate, explicitReceiverKind, extensionReceiver) {
createErrorCandidate(ErrorCandidateReason.TYPE_COMPUTATION_RECURSION)
}
private fun createRecursionTolerantCandidate(
towerCandidate: CandidateWithBoundDispatchReceiver,
explicitReceiverKind: ExplicitReceiverKind,
extensionReceiver: ReceiverValueWithSmartCastInfo?,
onRecursion: () -> CallableReferenceResolutionCandidate,
): CallableReferenceResolutionCandidate =
try {
val resolutionCandidate = createCandidateInternal(towerCandidate, explicitReceiverKind, extensionReceiver)
val returnType = resolutionCandidate.candidate.returnType
if (returnType == null || !returnType.isErrorRecursiveType()) resolutionCandidate else onRecursion()
} catch (e: LazyWrappedTypeComputationException) {
onRecursion()
}
/**
* The function is called only inside [NoExplicitReceiverScopeTowerProcessor] with [TowerData.BothTowerLevelAndContextReceiversGroup].
* This case involves only [SimpleCandidateFactory].
@@ -116,8 +116,14 @@ class WrongCountOfTypeArguments(
override fun report(reporter: DiagnosticReporter) = reporter.onTypeArguments(this)
}
object TypeCheckerHasRanIntoRecursion : KotlinCallDiagnostic(INAPPLICABLE) {
override fun report(reporter: DiagnosticReporter) = reporter.onCall(this)
class TypeCheckerHasRanIntoRecursion(val onArgument: KotlinCallArgument? = null) : KotlinCallDiagnostic(INAPPLICABLE) {
override fun report(reporter: DiagnosticReporter) {
return if (onArgument != null) {
reporter.onCallArgument(onArgument, this)
} else {
reporter.onCall(this)
}
}
}
// Callable reference resolution
@@ -15,6 +15,7 @@ import org.jetbrains.kotlin.resolve.calls.inference.addSubsystemFromArgument
import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintStorage
import org.jetbrains.kotlin.resolve.calls.tasks.ExplicitReceiverKind
import org.jetbrains.kotlin.resolve.calls.tower.*
import org.jetbrains.kotlin.resolve.calls.util.ErrorCandidateReason
import org.jetbrains.kotlin.resolve.descriptorUtil.hasDynamicExtensionAnnotation
import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValueWithSmartCastInfo
import org.jetbrains.kotlin.types.error.ErrorUtils
@@ -163,7 +164,7 @@ class SimpleCandidateFactory(
return candidate
}
override fun createErrorCandidate(): SimpleResolutionCandidate {
override fun createErrorCandidate(reason: ErrorCandidateReason): SimpleResolutionCandidate {
val errorScope = ErrorUtils.createErrorScope(ErrorScopeKind.SCOPE_FOR_ERROR_RESOLUTION_CANDIDATE, kotlinCall.toString())
val errorDescriptor = if (kotlinCall.callKind == KotlinCallKind.VARIABLE) {
errorScope.getContributedVariables(kotlinCall.name, scopeTower.location)
@@ -154,6 +154,7 @@ object ResolvedUsingNewFeatures : ResolutionDiagnostic(RESOLVED_NEED_PRESERVE_CO
object UnstableSmartCastDiagnostic : ResolutionDiagnostic(UNSTABLE_SMARTCAST)
object HiddenExtensionRelatedToDynamicTypes : ResolutionDiagnostic(HIDDEN)
object HiddenDescriptor : ResolutionDiagnostic(HIDDEN)
object RecursiveCallableReferenceType : ResolutionDiagnostic(INAPPLICABLE)
object InvokeConventionCallNoOperatorModifier : ResolutionDiagnostic(CONVENTION_ERROR)
object InfixCallNoInfixModifier : ResolutionDiagnostic(CONVENTION_ERROR)
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.resolve.calls.tower
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.progress.ProgressIndicatorAndCompilationCanceledStatus
import org.jetbrains.kotlin.resolve.calls.tasks.ExplicitReceiverKind
import org.jetbrains.kotlin.resolve.calls.util.ErrorCandidateReason
import org.jetbrains.kotlin.resolve.descriptorUtil.HIDES_MEMBERS_NAME_LIST
import org.jetbrains.kotlin.resolve.scopes.HierarchicalScope
import org.jetbrains.kotlin.resolve.scopes.ImportingScope
@@ -47,7 +48,7 @@ interface CandidateFactory<out C : Candidate> {
extensionReceiver: ReceiverValueWithSmartCastInfo?
): C
fun createErrorCandidate(): C
fun createErrorCandidate(reason: ErrorCandidateReason = ErrorCandidateReason.OTHER): C
fun createCandidate(
towerCandidate: CandidateWithBoundDispatchReceiver,
@@ -0,0 +1,8 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.resolve.calls.util
enum class ErrorCandidateReason { TYPE_COMPUTATION_RECURSION, OTHER }