diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/OverloadConflictResolver.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/OverloadConflictResolver.kt index c4743c323a6..5d84ab332d5 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/OverloadConflictResolver.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/OverloadConflictResolver.kt @@ -8,7 +8,10 @@ package org.jetbrains.kotlin.fir.resolve.calls import org.jetbrains.kotlin.fir.declarations.FirCallableDeclaration import org.jetbrains.kotlin.fir.declarations.FirConstructor import org.jetbrains.kotlin.fir.declarations.FirNamedFunction +import org.jetbrains.kotlin.fir.resolve.substitution.ConeSubstitutorByMap import org.jetbrains.kotlin.fir.resolve.transformers.firUnsafe +import org.jetbrains.kotlin.fir.symbols.impl.FirTypeParameterSymbol +import org.jetbrains.kotlin.fir.types.ConeKotlinType import org.jetbrains.kotlin.fir.types.coneTypeUnsafe import org.jetbrains.kotlin.resolve.OverloadabilitySpecificityCallbacks import org.jetbrains.kotlin.resolve.calls.inference.model.NewConstraintSystemImpl @@ -21,6 +24,7 @@ import org.jetbrains.kotlin.types.model.KotlinTypeMarker import org.jetbrains.kotlin.types.model.TypeParameterMarker import org.jetbrains.kotlin.types.model.TypeSubstitutorMarker import org.jetbrains.kotlin.types.model.TypeSystemInferenceExtensionContext +import org.jetbrains.kotlin.utils.addToStdlib.cast class ConeOverloadConflictResolver( val specificityComparator: TypeSpecificityComparator, @@ -73,7 +77,8 @@ class ConeOverloadConflictResolver( return FlatSignature( call, function.typeParameters.map { it.symbol }, - call.argumentMapping!!.map { it.value.returnTypeRef.coneTypeUnsafe() }, + listOfNotNull(function.receiverTypeRef?.coneTypeUnsafe()) + + call.argumentMapping!!.map { it.value.returnTypeRef.coneTypeUnsafe() }, function.receiverTypeRef != null, function.valueParameters.any { it.isVararg }, function.valueParameters.count { it.defaultValue != null }, @@ -199,8 +204,24 @@ class ConeOverloadConflictResolver( object NoSubstitutor : TypeSubstitutorMarker class ConeSimpleConstraintSystemImpl(val system: NewConstraintSystemImpl) : SimpleConstraintSystem { - override fun registerTypeVariables(typeParameters: Collection): TypeSubstitutorMarker { - return NoSubstitutor + override fun registerTypeVariables(typeParameters: Collection): TypeSubstitutorMarker = with(context) { + val csBuilder = system.getBuilder() + val substitutionMap = typeParameters.associate { + require(it is FirTypeParameterSymbol) + val variable = TypeParameterBasedTypeVariable(it) + csBuilder.registerVariable(variable) + + + it to variable.defaultType + } + val substitutor = ConeSubstitutorByMap(substitutionMap.cast()) + for (typeParameter in typeParameters) { + require(typeParameter is FirTypeParameterSymbol) + for (upperBound in typeParameter.fir.bounds) { + addSubtypeConstraint(substitutionMap[typeParameter]!!, substitutor.substituteOrSelf(upperBound.coneTypeUnsafe())) + } + } + return substitutor } override fun addSubtypeConstraint(subType: KotlinTypeMarker, superType: KotlinTypeMarker) { @@ -209,6 +230,9 @@ class ConeSimpleConstraintSystemImpl(val system: NewConstraintSystemImpl) : Simp override fun hasContradiction(): Boolean = system.hasContradiction + override val captureFromArgument: Boolean + get() = true + override val context: TypeSystemInferenceExtensionContext get() = system diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/substitution/ConeSubstitutor.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/substitution/ConeSubstitutor.kt index 314e471fcab..7361f5d812b 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/substitution/ConeSubstitutor.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/substitution/ConeSubstitutor.kt @@ -10,9 +10,10 @@ import org.jetbrains.kotlin.fir.symbols.ConeTypeParameterSymbol import org.jetbrains.kotlin.fir.types.* import org.jetbrains.kotlin.fir.types.impl.ConeAbbreviatedTypeImpl import org.jetbrains.kotlin.fir.types.impl.ConeClassTypeImpl +import org.jetbrains.kotlin.types.model.TypeSubstitutorMarker -interface ConeSubstitutor { +interface ConeSubstitutor : TypeSubstitutorMarker { fun substituteOrSelf(type: ConeKotlinType): ConeKotlinType fun substituteOrNull(type: ConeKotlinType): ConeKotlinType? diff --git a/compiler/fir/resolve/testData/resolve/stdlib/components.txt b/compiler/fir/resolve/testData/resolve/stdlib/components.txt index 5965126e8c2..a12ef4d776f 100644 --- a/compiler/fir/resolve/testData/resolve/stdlib/components.txt +++ b/compiler/fir/resolve/testData/resolve/stdlib/components.txt @@ -28,9 +28,9 @@ FILE: components.kt lval y: R|kotlin/String| = R|/|.R|/D.component2|() } - lval : = R|/list|.#() - lval x: = R|/|.component1() - lval y: = R|/|.component2() + lval : R|D| = R|/list|.R|kotlin/collections/first|() + lval x: R|kotlin/Int| = R|/|.R|/D.component1|() + lval y: R|kotlin/String| = R|/|.R|/D.component2|() R|/list|.R|kotlin/collections/forEach|( = forEach@fun (: R|D|): R|kotlin/Unit| { lval x: R|kotlin/Int| = R|/|.R|/D.component1|() lval y: R|kotlin/String| = R|/|.R|/D.component2|()