Initial support of type inference for callable references
There are two main changes here: - In CallCompleter, there was a bug: we assumed that the return type of a candidate must be a subtype of the expected type and were adding a corresponding constraint to the system. However, this is not true for callable references where the type of the expression is KFunctionN<...> and the return type of the candidate must be a subtype of the _last generic argument_ of the functional type. - In CandidateResolver, we use a more correct (although still not precise) heuristic to determine if a candidate fits based on the non-substituted type of the callable reference expression which it would produce. This can be further improved, see TODOs in CallCompleter. Also this does not influence resolution of callable references being passed as arguments to generic calls (that happens in GenericCandidateResolver) #KT-10968 Fixed #KT-11075 Fixed #KT-12286 Fixed #KT-12963 Open #KT-12964 Open
This commit is contained in:
@@ -17,6 +17,8 @@
|
||||
package org.jetbrains.kotlin.resolve.calls
|
||||
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.builtins.getReturnTypeFromFunctionType
|
||||
import org.jetbrains.kotlin.builtins.isFunctionType
|
||||
import org.jetbrains.kotlin.config.LanguageFeatureSettings
|
||||
import org.jetbrains.kotlin.coroutines.controllerTypeIfCoroutine
|
||||
import org.jetbrains.kotlin.coroutines.resolveCoroutineHandleResultCallIfNeeded
|
||||
@@ -172,6 +174,15 @@ class CallCompleter(
|
||||
) {
|
||||
val returnType = candidateDescriptor.returnType
|
||||
|
||||
val expectedReturnType =
|
||||
if (isResolvingCallableReference(call)) {
|
||||
// TODO: compute generic type argument for R in the kotlin.Function<R> supertype (KT-12963)
|
||||
// TODO: also add constraints for parameter types (KT-12964)
|
||||
if (!TypeUtils.noExpectedType(expectedType) && expectedType.isFunctionType) getReturnTypeFromFunctionType(expectedType)
|
||||
else TypeUtils.NO_EXPECTED_TYPE
|
||||
}
|
||||
else expectedType
|
||||
|
||||
fun ConstraintSystem.Builder.returnTypeInSystem(): KotlinType? =
|
||||
returnType?.let {
|
||||
val substitutor = typeVariableSubstitutors[call.toHandle()] ?: error("No substitutor for call: $call")
|
||||
@@ -185,11 +196,11 @@ class CallCompleter(
|
||||
}
|
||||
}
|
||||
|
||||
if (returnType != null && !TypeUtils.noExpectedType(expectedType)) {
|
||||
if (returnType != null && !TypeUtils.noExpectedType(expectedReturnType)) {
|
||||
updateSystemIfNeeded { builder ->
|
||||
val returnTypeInSystem = builder.returnTypeInSystem()
|
||||
if (returnTypeInSystem != null) {
|
||||
builder.addSubtypeConstraint(returnTypeInSystem, expectedType, EXPECTED_TYPE_POSITION.position())
|
||||
builder.addSubtypeConstraint(returnTypeInSystem, expectedReturnType, EXPECTED_TYPE_POSITION.position())
|
||||
builder.build()
|
||||
}
|
||||
else null
|
||||
@@ -209,7 +220,7 @@ class CallCompleter(
|
||||
}
|
||||
}
|
||||
|
||||
if (returnType != null && expectedType === TypeUtils.UNIT_EXPECTED_TYPE) {
|
||||
if (returnType != null && expectedReturnType === TypeUtils.UNIT_EXPECTED_TYPE) {
|
||||
updateSystemIfNeeded { builder ->
|
||||
val returnTypeInSystem = builder.returnTypeInSystem()
|
||||
if (returnTypeInSystem != null) {
|
||||
@@ -230,6 +241,11 @@ class CallCompleter(
|
||||
setResultingSubstitutor(system.resultingSubstitutor)
|
||||
}
|
||||
|
||||
private fun isResolvingCallableReference(call: Call): Boolean {
|
||||
val callElement = call.callElement
|
||||
return (callElement.parent as? KtCallableReferenceExpression)?.callableReference == callElement
|
||||
}
|
||||
|
||||
private fun <D : CallableDescriptor> MutableResolvedCall<D>.updateResolutionStatusFromConstraintSystem(
|
||||
context: BasicCallResolutionContext,
|
||||
tracing: TracingStrategy
|
||||
|
||||
@@ -163,12 +163,24 @@ class CandidateResolver(
|
||||
},
|
||||
reflectionTypes, scope.ownerDescriptor
|
||||
)
|
||||
if (candidateKCallableType == null || !KotlinTypeChecker.DEFAULT.isSubtypeOf(candidateKCallableType, expectedType)) {
|
||||
if (candidateKCallableType == null ||
|
||||
!canBeSubtype(candidateKCallableType, expectedType, candidateCall.candidateDescriptor.typeParameters)) {
|
||||
candidateCall.addStatus(OTHER_ERROR)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun canBeSubtype(subType: KotlinType, superType: KotlinType, candidateTypeParameters: List<TypeParameterDescriptor>): Boolean {
|
||||
// Here we need to check that there exists a substitution from type parameters (used in types in candidate signature)
|
||||
// to arguments such that substituted candidateKCallableType would be a subtype of expectedType.
|
||||
// It looks like in general this can only be decided by constructing a constraint system and checking
|
||||
// if it has a contradiction. Currently we use a heuristic that may not work ideally in all cases.
|
||||
// TODO: use constraint system to check if candidateKCallableType can be a subtype of expectedType
|
||||
val substituteDontCare = makeConstantSubstitutor(candidateTypeParameters, TypeUtils.DONT_CARE)
|
||||
val subTypeSubstituted = substituteDontCare.substitute(subType, Variance.INVARIANT) ?: return true
|
||||
return KotlinTypeChecker.ERROR_TYPES_ARE_EQUAL_TO_ANYTHING.isSubtypeOf(subTypeSubstituted, superType)
|
||||
}
|
||||
|
||||
private fun CallCandidateResolutionContext<*>.checkVisibilityWithoutReceiver() = checkAndReport {
|
||||
checkVisibilityWithDispatchReceiver(Visibilities.ALWAYS_SUITABLE_RECEIVER, null)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user