diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/NewOverloadingConflictResolver.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/NewOverloadingConflictResolver.kt index e335398f03f..6c34dc17a2f 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/NewOverloadingConflictResolver.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/NewOverloadingConflictResolver.kt @@ -38,8 +38,9 @@ class NewOverloadingConflictResolver( builtIns, specificityComparator, { - (it as? VariableAsFunctionKotlinResolutionCandidate)?.invokeCandidate?.descriptorWithFreshTypes ?: - (it as SimpleKotlinResolutionCandidate).descriptorWithFreshTypes + // todo investigate + (it as? VariableAsFunctionKotlinResolutionCandidate)?.invokeCandidate?.candidateDescriptor ?: + (it as SimpleKotlinResolutionCandidate).candidateDescriptor }, { SimpleConstraintSystemImpl(constraintInjector, typeResolver) }, Companion::createFlatSignature, @@ -72,7 +73,6 @@ class NewOverloadingConflictResolver( return FlatSignature.create(candidate, originalDescriptor, numDefaults, - listOfNotNull(originalDescriptor.extensionReceiverParameter?.type) + simpleCandidate.kotlinCall.argumentsInParenthesis.map { valueArgumentToParameterType[it] } + listOfNotNull(simpleCandidate.kotlinCall.externalArgument?.let { valueArgumentToParameterType[it] }) ) diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/SimpleConstraintSystemImpl.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/SimpleConstraintSystemImpl.kt index 08f5afdd7a1..b487b3edcb6 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/SimpleConstraintSystemImpl.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/SimpleConstraintSystemImpl.kt @@ -18,15 +18,12 @@ package org.jetbrains.kotlin.resolve.calls.inference.components import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor import org.jetbrains.kotlin.name.Name -import org.jetbrains.kotlin.resolve.calls.model.KotlinCallKind import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemBuilder import org.jetbrains.kotlin.resolve.calls.inference.model.NewConstraintSystemImpl import org.jetbrains.kotlin.resolve.calls.inference.model.SimpleConstraintSystemConstraintPosition import org.jetbrains.kotlin.resolve.calls.inference.model.TypeVariableFromCallableDescriptor -import org.jetbrains.kotlin.resolve.calls.model.KotlinCall -import org.jetbrains.kotlin.resolve.calls.model.KotlinCallArgument -import org.jetbrains.kotlin.resolve.calls.model.ReceiverKotlinCallArgument -import org.jetbrains.kotlin.resolve.calls.model.TypeArgument +import org.jetbrains.kotlin.resolve.calls.inference.substitute +import org.jetbrains.kotlin.resolve.calls.model.* import org.jetbrains.kotlin.resolve.calls.results.SimpleConstraintSystem import org.jetbrains.kotlin.types.TypeConstructorSubstitution import org.jetbrains.kotlin.types.TypeSubstitutor @@ -45,7 +42,13 @@ class SimpleConstraintSystemImpl(constraintInjector: ConstraintInjector, resultT it.defaultType.constructor to variable.defaultType.asTypeProjection() } - return TypeConstructorSubstitution.createByConstructorsMap(substitutionMap).buildSubstitutor() + val substitutor = TypeConstructorSubstitution.createByConstructorsMap(substitutionMap).buildSubstitutor() + for (typeParameter in typeParameters) { + for (upperBound in typeParameter.upperBounds) { + addSubtypeConstraint(substitutor.substitute(typeParameter.defaultType), substitutor.substitute(upperBound.unwrap())) + } + } + return substitutor } override fun addSubtypeConstraint(subType: UnwrappedType, superType: UnwrappedType) { @@ -53,6 +56,7 @@ class SimpleConstraintSystemImpl(constraintInjector: ConstraintInjector, resultT } override fun hasContradiction() = csBuilder.hasContradiction + override val captureFromArgument get() = true private object ThrowableKotlinCall : KotlinCall { override val callKind: KotlinCallKind get() = throw UnsupportedOperationException() diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/results/FlatSignature.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/results/FlatSignature.kt index 0e3957b42a0..9e06407887e 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/results/FlatSignature.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/results/FlatSignature.kt @@ -24,6 +24,7 @@ import org.jetbrains.kotlin.descriptors.synthetic.SyntheticMemberDescriptor import org.jetbrains.kotlin.resolve.descriptorUtil.hasDefaultValue import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.checker.KotlinTypeChecker +import org.jetbrains.kotlin.types.checker.captureFromExpression interface SpecificityComparisonCallbacks { fun isNonSubtypeNotLessSpecific(specific: KotlinType, general: KotlinType): Boolean @@ -95,6 +96,9 @@ interface SimpleConstraintSystem { fun registerTypeVariables(typeParameters: Collection): TypeSubstitutor fun addSubtypeConstraint(subType: UnwrappedType, superType: UnwrappedType) fun hasContradiction(): Boolean + + // todo hack for migration + val captureFromArgument get() = false } fun SimpleConstraintSystem.isSignatureNotLessSpecific( @@ -125,7 +129,16 @@ fun SimpleConstraintSystem.isSignatureNotLessSpecific( } else { val substitutedGeneralType = typeSubstitutor.safeSubstitute(generalType, Variance.INVARIANT) - addSubtypeConstraint(specificType.unwrap(), substitutedGeneralType.unwrap()) + + /** + * Example: + * fun Array.sort(): Unit {} + * fun > Array.sort(): Unit {} + * Here, when we try solve this CS(Y is variables) then Array <: Array and this system impossible to solve, + * so we capture types from receiver and value parameters. + */ + val specificCapturedType = specificType.unwrap().let { if (captureFromArgument) captureFromExpression(it) ?: it else it } + addSubtypeConstraint(specificCapturedType, substitutedGeneralType.unwrap()) } }