FIR: resolve conflicts around SAM calls properly
This commit is contained in:
+6
-4
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.fir.resolve.calls.jvm
|
|||||||
import org.jetbrains.kotlin.fir.containingClass
|
import org.jetbrains.kotlin.fir.containingClass
|
||||||
import org.jetbrains.kotlin.fir.declarations.*
|
import org.jetbrains.kotlin.fir.declarations.*
|
||||||
import org.jetbrains.kotlin.fir.declarations.utils.isExpect
|
import org.jetbrains.kotlin.fir.declarations.utils.isExpect
|
||||||
|
import org.jetbrains.kotlin.fir.resolve.BodyResolveComponents
|
||||||
import org.jetbrains.kotlin.fir.resolve.calls.AbstractConeCallConflictResolver
|
import org.jetbrains.kotlin.fir.resolve.calls.AbstractConeCallConflictResolver
|
||||||
import org.jetbrains.kotlin.fir.resolve.calls.Candidate
|
import org.jetbrains.kotlin.fir.resolve.calls.Candidate
|
||||||
import org.jetbrains.kotlin.fir.resolve.inference.InferenceComponents
|
import org.jetbrains.kotlin.fir.resolve.inference.InferenceComponents
|
||||||
@@ -19,8 +20,9 @@ import org.jetbrains.kotlin.resolve.calls.results.TypeSpecificityComparator
|
|||||||
// like emptyArray() from intrinsics and built-ins
|
// like emptyArray() from intrinsics and built-ins
|
||||||
class ConeEquivalentCallConflictResolver(
|
class ConeEquivalentCallConflictResolver(
|
||||||
specificityComparator: TypeSpecificityComparator,
|
specificityComparator: TypeSpecificityComparator,
|
||||||
inferenceComponents: InferenceComponents
|
inferenceComponents: InferenceComponents,
|
||||||
) : AbstractConeCallConflictResolver(specificityComparator, inferenceComponents) {
|
transformerComponents: BodyResolveComponents
|
||||||
|
) : AbstractConeCallConflictResolver(specificityComparator, inferenceComponents, transformerComponents) {
|
||||||
override fun chooseMaximallySpecificCandidates(
|
override fun chooseMaximallySpecificCandidates(
|
||||||
candidates: Set<Candidate>,
|
candidates: Set<Candidate>,
|
||||||
discriminateGenerics: Boolean,
|
discriminateGenerics: Boolean,
|
||||||
@@ -64,8 +66,8 @@ class ConeEquivalentCallConflictResolver(
|
|||||||
}
|
}
|
||||||
val firstSignature = createFlatSignature(firstCandidate, first)
|
val firstSignature = createFlatSignature(firstCandidate, first)
|
||||||
val secondSignature = createFlatSignature(secondCandidate, second)
|
val secondSignature = createFlatSignature(secondCandidate, second)
|
||||||
return compareCallsByUsedArguments(firstSignature, secondSignature, false) &&
|
return compareCallsByUsedArguments(firstSignature, secondSignature, discriminateGenerics = false, useOriginalSamTypes = false) &&
|
||||||
compareCallsByUsedArguments(secondSignature, firstSignature, false)
|
compareCallsByUsedArguments(secondSignature, firstSignature, discriminateGenerics = false, useOriginalSamTypes = false)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun createFlatSignature(call: Candidate, declaration: FirCallableDeclaration): FlatSignature<Candidate> {
|
private fun createFlatSignature(call: Candidate, declaration: FirCallableDeclaration): FlatSignature<Candidate> {
|
||||||
|
|||||||
+8
-5
@@ -6,11 +6,13 @@
|
|||||||
package org.jetbrains.kotlin.fir.resolve.calls.jvm
|
package org.jetbrains.kotlin.fir.resolve.calls.jvm
|
||||||
|
|
||||||
import org.jetbrains.kotlin.fir.NoMutableState
|
import org.jetbrains.kotlin.fir.NoMutableState
|
||||||
|
import org.jetbrains.kotlin.fir.resolve.BodyResolveComponents
|
||||||
import org.jetbrains.kotlin.fir.resolve.calls.ConeCallConflictResolverFactory
|
import org.jetbrains.kotlin.fir.resolve.calls.ConeCallConflictResolverFactory
|
||||||
import org.jetbrains.kotlin.fir.resolve.calls.ConeCompositeConflictResolver
|
import org.jetbrains.kotlin.fir.resolve.calls.ConeCompositeConflictResolver
|
||||||
import org.jetbrains.kotlin.fir.resolve.calls.ConeIntegerOperatorConflictResolver
|
import org.jetbrains.kotlin.fir.resolve.calls.ConeIntegerOperatorConflictResolver
|
||||||
import org.jetbrains.kotlin.fir.resolve.calls.ConeOverloadConflictResolver
|
import org.jetbrains.kotlin.fir.resolve.calls.ConeOverloadConflictResolver
|
||||||
import org.jetbrains.kotlin.fir.resolve.inference.InferenceComponents
|
import org.jetbrains.kotlin.fir.resolve.inference.InferenceComponents
|
||||||
|
import org.jetbrains.kotlin.fir.resolve.transformers.body.resolve.FirAbstractBodyResolveTransformer
|
||||||
import org.jetbrains.kotlin.fir.types.typeContext
|
import org.jetbrains.kotlin.fir.types.typeContext
|
||||||
import org.jetbrains.kotlin.resolve.calls.results.TypeSpecificityComparator
|
import org.jetbrains.kotlin.resolve.calls.results.TypeSpecificityComparator
|
||||||
import org.jetbrains.kotlin.resolve.jvm.JvmTypeSpecificityComparator
|
import org.jetbrains.kotlin.resolve.jvm.JvmTypeSpecificityComparator
|
||||||
@@ -19,14 +21,15 @@ import org.jetbrains.kotlin.resolve.jvm.JvmTypeSpecificityComparator
|
|||||||
object JvmCallConflictResolverFactory : ConeCallConflictResolverFactory() {
|
object JvmCallConflictResolverFactory : ConeCallConflictResolverFactory() {
|
||||||
override fun create(
|
override fun create(
|
||||||
typeSpecificityComparator: TypeSpecificityComparator,
|
typeSpecificityComparator: TypeSpecificityComparator,
|
||||||
components: InferenceComponents
|
components: InferenceComponents,
|
||||||
|
transformerComponents: BodyResolveComponents
|
||||||
): ConeCompositeConflictResolver {
|
): ConeCompositeConflictResolver {
|
||||||
val specificityComparator = JvmTypeSpecificityComparator(components.session.typeContext)
|
val specificityComparator = JvmTypeSpecificityComparator(components.session.typeContext)
|
||||||
return ConeCompositeConflictResolver(
|
return ConeCompositeConflictResolver(
|
||||||
ConeOverloadConflictResolver(specificityComparator, components),
|
ConeOverloadConflictResolver(specificityComparator, components, transformerComponents),
|
||||||
ConeEquivalentCallConflictResolver(specificityComparator, components),
|
ConeEquivalentCallConflictResolver(specificityComparator, components, transformerComponents),
|
||||||
JvmPlatformOverloadsConflictResolver(specificityComparator, components),
|
JvmPlatformOverloadsConflictResolver(specificityComparator, components, transformerComponents),
|
||||||
ConeIntegerOperatorConflictResolver(specificityComparator, components)
|
ConeIntegerOperatorConflictResolver(specificityComparator, components, transformerComponents)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-2
@@ -10,15 +10,18 @@ import org.jetbrains.kotlin.fir.containingClass
|
|||||||
import org.jetbrains.kotlin.fir.declarations.FirField
|
import org.jetbrains.kotlin.fir.declarations.FirField
|
||||||
import org.jetbrains.kotlin.fir.declarations.FirProperty
|
import org.jetbrains.kotlin.fir.declarations.FirProperty
|
||||||
import org.jetbrains.kotlin.fir.languageVersionSettings
|
import org.jetbrains.kotlin.fir.languageVersionSettings
|
||||||
|
import org.jetbrains.kotlin.fir.resolve.BodyResolveComponents
|
||||||
import org.jetbrains.kotlin.fir.resolve.calls.AbstractConeCallConflictResolver
|
import org.jetbrains.kotlin.fir.resolve.calls.AbstractConeCallConflictResolver
|
||||||
import org.jetbrains.kotlin.fir.resolve.calls.Candidate
|
import org.jetbrains.kotlin.fir.resolve.calls.Candidate
|
||||||
import org.jetbrains.kotlin.fir.resolve.inference.InferenceComponents
|
import org.jetbrains.kotlin.fir.resolve.inference.InferenceComponents
|
||||||
|
import org.jetbrains.kotlin.fir.resolve.transformers.body.resolve.FirAbstractBodyResolveTransformer
|
||||||
import org.jetbrains.kotlin.resolve.calls.results.TypeSpecificityComparator
|
import org.jetbrains.kotlin.resolve.calls.results.TypeSpecificityComparator
|
||||||
|
|
||||||
class JvmPlatformOverloadsConflictResolver(
|
class JvmPlatformOverloadsConflictResolver(
|
||||||
specificityComparator: TypeSpecificityComparator,
|
specificityComparator: TypeSpecificityComparator,
|
||||||
inferenceComponents: InferenceComponents
|
inferenceComponents: InferenceComponents,
|
||||||
) : AbstractConeCallConflictResolver(specificityComparator, inferenceComponents) {
|
transformerComponents: BodyResolveComponents
|
||||||
|
) : AbstractConeCallConflictResolver(specificityComparator, inferenceComponents, transformerComponents) {
|
||||||
override fun chooseMaximallySpecificCandidates(
|
override fun chooseMaximallySpecificCandidates(
|
||||||
candidates: Set<Candidate>,
|
candidates: Set<Candidate>,
|
||||||
discriminateGenerics: Boolean,
|
discriminateGenerics: Boolean,
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ class FirCallResolver(
|
|||||||
)
|
)
|
||||||
|
|
||||||
val conflictResolver: ConeCallConflictResolver =
|
val conflictResolver: ConeCallConflictResolver =
|
||||||
session.callConflictResolverFactory.create(TypeSpecificityComparator.NONE, session.inferenceComponents)
|
session.callConflictResolverFactory.create(TypeSpecificityComparator.NONE, session.inferenceComponents, components)
|
||||||
|
|
||||||
@PrivateForInline
|
@PrivateForInline
|
||||||
var needTransformArguments: Boolean = true
|
var needTransformArguments: Boolean = true
|
||||||
|
|||||||
+52
-35
@@ -7,6 +7,8 @@ package org.jetbrains.kotlin.fir.resolve.calls
|
|||||||
|
|
||||||
import org.jetbrains.kotlin.fir.declarations.*
|
import org.jetbrains.kotlin.fir.declarations.*
|
||||||
import org.jetbrains.kotlin.fir.declarations.utils.isExpect
|
import org.jetbrains.kotlin.fir.declarations.utils.isExpect
|
||||||
|
import org.jetbrains.kotlin.fir.resolve.BodyResolveComponents
|
||||||
|
import org.jetbrains.kotlin.fir.resolve.FirSamResolver
|
||||||
import org.jetbrains.kotlin.fir.resolve.inference.InferenceComponents
|
import org.jetbrains.kotlin.fir.resolve.inference.InferenceComponents
|
||||||
import org.jetbrains.kotlin.fir.types.*
|
import org.jetbrains.kotlin.fir.types.*
|
||||||
import org.jetbrains.kotlin.name.StandardClassIds
|
import org.jetbrains.kotlin.name.StandardClassIds
|
||||||
@@ -17,8 +19,12 @@ import org.jetbrains.kotlin.utils.addIfNotNull
|
|||||||
|
|
||||||
abstract class AbstractConeCallConflictResolver(
|
abstract class AbstractConeCallConflictResolver(
|
||||||
private val specificityComparator: TypeSpecificityComparator,
|
private val specificityComparator: TypeSpecificityComparator,
|
||||||
protected val inferenceComponents: InferenceComponents
|
protected val inferenceComponents: InferenceComponents,
|
||||||
|
private val transformerComponents: BodyResolveComponents
|
||||||
) : ConeCallConflictResolver() {
|
) : ConeCallConflictResolver() {
|
||||||
|
|
||||||
|
private val samResolver: FirSamResolver get() = transformerComponents.samResolver
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns `true` if [call1] is definitely more or equally specific [call2],
|
* Returns `true` if [call1] is definitely more or equally specific [call2],
|
||||||
* `false` otherwise.
|
* `false` otherwise.
|
||||||
@@ -26,7 +32,8 @@ abstract class AbstractConeCallConflictResolver(
|
|||||||
protected fun compareCallsByUsedArguments(
|
protected fun compareCallsByUsedArguments(
|
||||||
call1: FlatSignature<Candidate>,
|
call1: FlatSignature<Candidate>,
|
||||||
call2: FlatSignature<Candidate>,
|
call2: FlatSignature<Candidate>,
|
||||||
discriminateGenerics: Boolean
|
discriminateGenerics: Boolean,
|
||||||
|
useOriginalSamTypes: Boolean
|
||||||
): Boolean {
|
): Boolean {
|
||||||
if (discriminateGenerics) {
|
if (discriminateGenerics) {
|
||||||
val isGeneric1 = call1.isGeneric
|
val isGeneric1 = call1.isGeneric
|
||||||
@@ -48,7 +55,8 @@ abstract class AbstractConeCallConflictResolver(
|
|||||||
call1,
|
call1,
|
||||||
call2,
|
call2,
|
||||||
SpecificityComparisonWithNumerics,
|
SpecificityComparisonWithNumerics,
|
||||||
specificityComparator
|
specificityComparator,
|
||||||
|
useOriginalSamTypes
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -113,44 +121,44 @@ abstract class AbstractConeCallConflictResolver(
|
|||||||
|
|
||||||
protected fun createFlatSignature(call: Candidate, variable: FirVariable): FlatSignature<Candidate> {
|
protected fun createFlatSignature(call: Candidate, variable: FirVariable): FlatSignature<Candidate> {
|
||||||
return FlatSignature(
|
return FlatSignature(
|
||||||
call,
|
origin = call,
|
||||||
(variable as? FirProperty)?.typeParameters?.map { it.symbol.toLookupTag() }.orEmpty(),
|
typeParameters = (variable as? FirProperty)?.typeParameters?.map { it.symbol.toLookupTag() }.orEmpty(),
|
||||||
computeSignatureTypes(call, variable),
|
valueParameterTypes = computeSignatureTypes(call, variable),
|
||||||
variable.receiverTypeRef != null,
|
hasExtensionReceiver = variable.receiverTypeRef != null,
|
||||||
variable.contextReceivers.size,
|
contextReceiverCount = variable.contextReceivers.size,
|
||||||
false,
|
hasVarargs = false,
|
||||||
0,
|
numDefaults = 0,
|
||||||
(variable as? FirProperty)?.isExpect == true,
|
isExpect = (variable as? FirProperty)?.isExpect == true,
|
||||||
false // TODO
|
isSyntheticMember = false // TODO
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
protected fun createFlatSignature(call: Candidate, constructor: FirConstructor): FlatSignature<Candidate> {
|
protected fun createFlatSignature(call: Candidate, constructor: FirConstructor): FlatSignature<Candidate> {
|
||||||
return FlatSignature(
|
return FlatSignature(
|
||||||
call,
|
origin = call,
|
||||||
constructor.typeParameters.map { it.symbol.toLookupTag() },
|
typeParameters = constructor.typeParameters.map { it.symbol.toLookupTag() },
|
||||||
computeSignatureTypes(call, constructor),
|
valueParameterTypes = computeSignatureTypes(call, constructor),
|
||||||
//constructor.receiverTypeRef != null,
|
//constructor.receiverTypeRef != null,
|
||||||
false,
|
hasExtensionReceiver = false,
|
||||||
constructor.contextReceivers.size,
|
contextReceiverCount = constructor.contextReceivers.size,
|
||||||
constructor.valueParameters.any { it.isVararg },
|
hasVarargs = constructor.valueParameters.any { it.isVararg },
|
||||||
call.numDefaults,
|
numDefaults = call.numDefaults,
|
||||||
constructor.isExpect,
|
isExpect = constructor.isExpect,
|
||||||
false // TODO
|
isSyntheticMember = false // TODO
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
protected fun createFlatSignature(call: Candidate, function: FirSimpleFunction): FlatSignature<Candidate> {
|
protected fun createFlatSignature(call: Candidate, function: FirSimpleFunction): FlatSignature<Candidate> {
|
||||||
return FlatSignature(
|
return FlatSignature(
|
||||||
call,
|
origin = call,
|
||||||
function.typeParameters.map { it.symbol.toLookupTag() },
|
typeParameters = function.typeParameters.map { it.symbol.toLookupTag() },
|
||||||
computeSignatureTypes(call, function),
|
valueParameterTypes = computeSignatureTypes(call, function),
|
||||||
function.receiverTypeRef != null,
|
hasExtensionReceiver = function.receiverTypeRef != null,
|
||||||
function.contextReceivers.size,
|
contextReceiverCount = function.contextReceivers.size,
|
||||||
function.valueParameters.any { it.isVararg },
|
hasVarargs = function.valueParameters.any { it.isVararg },
|
||||||
call.numDefaults,
|
numDefaults = call.numDefaults,
|
||||||
function.isExpect,
|
isExpect = function.isExpect,
|
||||||
false // TODO
|
isSyntheticMember = false // TODO
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -163,19 +171,28 @@ abstract class AbstractConeCallConflictResolver(
|
|||||||
private fun computeSignatureTypes(
|
private fun computeSignatureTypes(
|
||||||
call: Candidate,
|
call: Candidate,
|
||||||
called: FirCallableDeclaration
|
called: FirCallableDeclaration
|
||||||
): List<ConeKotlinType> {
|
): List<TypeWithConversion> {
|
||||||
return buildList {
|
return buildList {
|
||||||
addIfNotNull(called.receiverTypeRef?.coneType)
|
addIfNotNull(called.receiverTypeRef?.coneType?.let { TypeWithConversion(it) })
|
||||||
val typeForCallableReference = call.resultingTypeForCallableReference
|
val typeForCallableReference = call.resultingTypeForCallableReference
|
||||||
if (typeForCallableReference != null) {
|
if (typeForCallableReference != null) {
|
||||||
// Return type isn't needed here v
|
// Return type isn't needed here v
|
||||||
typeForCallableReference.typeArguments.dropLast(1)
|
typeForCallableReference.typeArguments.dropLast(1)
|
||||||
.mapTo(this) {
|
.mapTo(this) {
|
||||||
(it as ConeKotlinType).removeTypeVariableTypes(inferenceComponents.session.typeContext)
|
TypeWithConversion((it as ConeKotlinType).removeTypeVariableTypes(inferenceComponents.session.typeContext))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
called.contextReceivers.mapTo(this) { it.typeRef.coneType }
|
called.contextReceivers.mapTo(this) { TypeWithConversion(it.typeRef.coneType) }
|
||||||
call.argumentMapping?.mapTo(this) { it.value.argumentType() }
|
call.argumentMapping?.mapTo(this) { (_, parameter) ->
|
||||||
|
val argumentType = parameter.argumentType()
|
||||||
|
if (!call.usesSAM) {
|
||||||
|
TypeWithConversion(argumentType)
|
||||||
|
} else {
|
||||||
|
val functionType = samResolver.getFunctionTypeForPossibleSamType(argumentType)?.second
|
||||||
|
if (functionType == null) TypeWithConversion(argumentType)
|
||||||
|
else TypeWithConversion(functionType, argumentType)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -507,12 +507,10 @@ private fun Candidate.getExpectedTypeWithSAMConversion(
|
|||||||
// TODO: resolvedCall.registerArgumentWithSamConversion(argument, SamConversionDescription(convertedTypeByOriginal, convertedTypeByCandidate!!))
|
// TODO: resolvedCall.registerArgumentWithSamConversion(argument, SamConversionDescription(convertedTypeByOriginal, convertedTypeByCandidate!!))
|
||||||
|
|
||||||
val expectedFunctionType = context.bodyResolveComponents.samResolver.getFunctionTypeForPossibleSamType(candidateExpectedType)
|
val expectedFunctionType = context.bodyResolveComponents.samResolver.getFunctionTypeForPossibleSamType(candidateExpectedType)
|
||||||
|
?: return null
|
||||||
return runIf(argument.isFunctional(session, scopeSession, expectedFunctionType)) {
|
return runIf(argument.isFunctional(session, scopeSession, expectedFunctionType)) {
|
||||||
expectedFunctionType.apply {
|
usesSAM = true
|
||||||
// Even though the `expectedFunctionalType` could be `null`, we should mark the flag to indicate that the argument is a
|
expectedFunctionType
|
||||||
// functional type. That will help avoid ambiguous `invoke` resolutions. See KT-39824
|
|
||||||
usesSAM = true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+6
-1
@@ -7,6 +7,7 @@ package org.jetbrains.kotlin.fir.resolve.calls
|
|||||||
|
|
||||||
import org.jetbrains.kotlin.fir.FirSession
|
import org.jetbrains.kotlin.fir.FirSession
|
||||||
import org.jetbrains.kotlin.fir.FirSessionComponent
|
import org.jetbrains.kotlin.fir.FirSessionComponent
|
||||||
|
import org.jetbrains.kotlin.fir.resolve.BodyResolveComponents
|
||||||
import org.jetbrains.kotlin.fir.resolve.inference.InferenceComponents
|
import org.jetbrains.kotlin.fir.resolve.inference.InferenceComponents
|
||||||
import org.jetbrains.kotlin.resolve.calls.results.TypeSpecificityComparator
|
import org.jetbrains.kotlin.resolve.calls.results.TypeSpecificityComparator
|
||||||
|
|
||||||
@@ -25,7 +26,11 @@ abstract class ConeCallConflictResolver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
abstract class ConeCallConflictResolverFactory : FirSessionComponent {
|
abstract class ConeCallConflictResolverFactory : FirSessionComponent {
|
||||||
abstract fun create(typeSpecificityComparator: TypeSpecificityComparator, components: InferenceComponents): ConeCallConflictResolver
|
abstract fun create(
|
||||||
|
typeSpecificityComparator: TypeSpecificityComparator,
|
||||||
|
components: InferenceComponents,
|
||||||
|
transformerComponents: BodyResolveComponents
|
||||||
|
): ConeCallConflictResolver
|
||||||
}
|
}
|
||||||
|
|
||||||
val FirSession.callConflictResolverFactory: ConeCallConflictResolverFactory by FirSession.sessionComponentAccessor()
|
val FirSession.callConflictResolverFactory: ConeCallConflictResolverFactory by FirSession.sessionComponentAccessor()
|
||||||
+4
-2
@@ -5,14 +5,16 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.fir.resolve.calls
|
package org.jetbrains.kotlin.fir.resolve.calls
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.fir.resolve.BodyResolveComponents
|
||||||
import org.jetbrains.kotlin.fir.resolve.inference.InferenceComponents
|
import org.jetbrains.kotlin.fir.resolve.inference.InferenceComponents
|
||||||
import org.jetbrains.kotlin.fir.scopes.impl.isWrappedIntegerOperator
|
import org.jetbrains.kotlin.fir.scopes.impl.isWrappedIntegerOperator
|
||||||
import org.jetbrains.kotlin.resolve.calls.results.TypeSpecificityComparator
|
import org.jetbrains.kotlin.resolve.calls.results.TypeSpecificityComparator
|
||||||
|
|
||||||
class ConeIntegerOperatorConflictResolver(
|
class ConeIntegerOperatorConflictResolver(
|
||||||
specificityComparator: TypeSpecificityComparator,
|
specificityComparator: TypeSpecificityComparator,
|
||||||
inferenceComponents: InferenceComponents
|
inferenceComponents: InferenceComponents,
|
||||||
) : AbstractConeCallConflictResolver(specificityComparator, inferenceComponents) {
|
transformerComponents: BodyResolveComponents
|
||||||
|
) : AbstractConeCallConflictResolver(specificityComparator, inferenceComponents, transformerComponents) {
|
||||||
override fun chooseMaximallySpecificCandidates(
|
override fun chooseMaximallySpecificCandidates(
|
||||||
candidates: Set<Candidate>,
|
candidates: Set<Candidate>,
|
||||||
discriminateGenerics: Boolean,
|
discriminateGenerics: Boolean,
|
||||||
|
|||||||
+16
-6
@@ -10,9 +10,11 @@ import org.jetbrains.kotlin.fir.FirSession
|
|||||||
import org.jetbrains.kotlin.fir.declarations.FirMemberDeclaration
|
import org.jetbrains.kotlin.fir.declarations.FirMemberDeclaration
|
||||||
import org.jetbrains.kotlin.fir.declarations.utils.modality
|
import org.jetbrains.kotlin.fir.declarations.utils.modality
|
||||||
import org.jetbrains.kotlin.fir.render
|
import org.jetbrains.kotlin.fir.render
|
||||||
|
import org.jetbrains.kotlin.fir.resolve.BodyResolveComponents
|
||||||
import org.jetbrains.kotlin.fir.resolve.inference.ConeTypeParameterBasedTypeVariable
|
import org.jetbrains.kotlin.fir.resolve.inference.ConeTypeParameterBasedTypeVariable
|
||||||
import org.jetbrains.kotlin.fir.resolve.inference.InferenceComponents
|
import org.jetbrains.kotlin.fir.resolve.inference.InferenceComponents
|
||||||
import org.jetbrains.kotlin.fir.resolve.substitution.substitutorByMap
|
import org.jetbrains.kotlin.fir.resolve.substitution.substitutorByMap
|
||||||
|
import org.jetbrains.kotlin.fir.resolve.transformers.body.resolve.FirAbstractBodyResolveTransformer
|
||||||
import org.jetbrains.kotlin.fir.symbols.ConeTypeParameterLookupTag
|
import org.jetbrains.kotlin.fir.symbols.ConeTypeParameterLookupTag
|
||||||
import org.jetbrains.kotlin.fir.types.coneType
|
import org.jetbrains.kotlin.fir.types.coneType
|
||||||
import org.jetbrains.kotlin.resolve.calls.inference.model.NewConstraintSystemImpl
|
import org.jetbrains.kotlin.resolve.calls.inference.model.NewConstraintSystemImpl
|
||||||
@@ -29,8 +31,9 @@ typealias CandidateSignature = FlatSignature<Candidate>
|
|||||||
|
|
||||||
class ConeOverloadConflictResolver(
|
class ConeOverloadConflictResolver(
|
||||||
specificityComparator: TypeSpecificityComparator,
|
specificityComparator: TypeSpecificityComparator,
|
||||||
inferenceComponents: InferenceComponents
|
inferenceComponents: InferenceComponents,
|
||||||
) : AbstractConeCallConflictResolver(specificityComparator, inferenceComponents) {
|
transformerComponents: BodyResolveComponents
|
||||||
|
) : AbstractConeCallConflictResolver(specificityComparator, inferenceComponents, transformerComponents) {
|
||||||
|
|
||||||
override fun chooseMaximallySpecificCandidates(
|
override fun chooseMaximallySpecificCandidates(
|
||||||
candidates: Set<Candidate>,
|
candidates: Set<Candidate>,
|
||||||
@@ -123,12 +126,18 @@ class ConeOverloadConflictResolver(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val filtered = candidates.filterTo(mutableSetOf()) { it.usesSAM }
|
||||||
|
if (filtered.isNotEmpty()) {
|
||||||
|
findMaximallySpecificCall(candidates, discriminateGenerics = false, useOriginalSamTypes = true)?.let { return setOf(it) }
|
||||||
|
}
|
||||||
|
|
||||||
return candidates
|
return candidates
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun findMaximallySpecificCall(
|
private fun findMaximallySpecificCall(
|
||||||
candidates: Set<Candidate>,
|
candidates: Set<Candidate>,
|
||||||
discriminateGenerics: Boolean
|
discriminateGenerics: Boolean,
|
||||||
|
useOriginalSamTypes: Boolean = false
|
||||||
): Candidate? {
|
): Candidate? {
|
||||||
if (candidates.size <= 1) return candidates.singleOrNull()
|
if (candidates.size <= 1) return candidates.singleOrNull()
|
||||||
|
|
||||||
@@ -138,7 +147,7 @@ class ConeOverloadConflictResolver(
|
|||||||
|
|
||||||
val bestCandidatesByParameterTypes = candidateSignatures.filter { signature ->
|
val bestCandidatesByParameterTypes = candidateSignatures.filter { signature ->
|
||||||
candidateSignatures.all { other ->
|
candidateSignatures.all { other ->
|
||||||
signature === other || isNotLessSpecificCallWithArgumentMapping(signature, other, discriminateGenerics)
|
signature === other || isNotLessSpecificCallWithArgumentMapping(signature, other, discriminateGenerics, useOriginalSamTypes)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -151,9 +160,10 @@ class ConeOverloadConflictResolver(
|
|||||||
private fun isNotLessSpecificCallWithArgumentMapping(
|
private fun isNotLessSpecificCallWithArgumentMapping(
|
||||||
call1: CandidateSignature,
|
call1: CandidateSignature,
|
||||||
call2: CandidateSignature,
|
call2: CandidateSignature,
|
||||||
discriminateGenerics: Boolean
|
discriminateGenerics: Boolean,
|
||||||
|
useOriginalSamTypes: Boolean = false
|
||||||
): Boolean {
|
): Boolean {
|
||||||
return compareCallsByUsedArguments(call1, call2, discriminateGenerics)
|
return compareCallsByUsedArguments(call1, call2, discriminateGenerics, useOriginalSamTypes)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun List<CandidateSignature>.exactMaxWith(): CandidateSignature? {
|
private fun List<CandidateSignature>.exactMaxWith(): CandidateSignature? {
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
// TARGET_BACKEND: JVM
|
// TARGET_BACKEND: JVM
|
||||||
// IGNORE_BACKEND_FIR: JVM_IR
|
|
||||||
// FIR status: OVERLOAD_RESOLUTION_AMBIGUITY on sub.foo {}
|
|
||||||
// WITH_STDLIB
|
// WITH_STDLIB
|
||||||
// MODULE: lib
|
// MODULE: lib
|
||||||
// FILE: Super.java
|
// FILE: Super.java
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
// FULL_JDK
|
// FULL_JDK
|
||||||
// IGNORE_BACKEND_FIR: JVM_IR
|
|
||||||
// IGNORE_BACKEND_FIR_MULTI_MODULE: JVM_IR JVM_IR_SERIALIZE
|
|
||||||
// TARGET_BACKEND: JVM
|
// TARGET_BACKEND: JVM
|
||||||
// FILE: 1.kt
|
// FILE: 1.kt
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
// FULL_JDK
|
// FULL_JDK
|
||||||
// IGNORE_BACKEND_FIR: JVM_IR
|
|
||||||
// IGNORE_BACKEND_FIR_MULTI_MODULE: JVM_IR JVM_IR_SERIALIZE
|
|
||||||
// TARGET_BACKEND: JVM
|
// TARGET_BACKEND: JVM
|
||||||
// FILE: 1.kt
|
// FILE: 1.kt
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
// FULL_JDK
|
// FULL_JDK
|
||||||
// IGNORE_BACKEND_FIR: JVM_IR
|
|
||||||
// IGNORE_BACKEND_FIR_MULTI_MODULE: JVM_IR JVM_IR_SERIALIZE
|
|
||||||
// TARGET_BACKEND: JVM
|
// TARGET_BACKEND: JVM
|
||||||
// FILE: 1.kt
|
// FILE: 1.kt
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
// FULL_JDK
|
// FULL_JDK
|
||||||
// NO_CHECK_LAMBDA_INLINING
|
// NO_CHECK_LAMBDA_INLINING
|
||||||
// IGNORE_BACKEND_FIR: JVM_IR
|
|
||||||
// IGNORE_BACKEND_FIR_MULTI_MODULE: JVM_IR JVM_IR_SERIALIZE
|
|
||||||
// TARGET_BACKEND: JVM
|
// TARGET_BACKEND: JVM
|
||||||
// FILE: 1.kt
|
// FILE: 1.kt
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -6,4 +6,4 @@ object X2
|
|||||||
fun <T1> foo(x: T1, f: (T1) -> T1) = X1
|
fun <T1> foo(x: T1, f: (T1) -> T1) = X1
|
||||||
fun <T2> foo(xf: () -> T2, f: (T2) -> T2) = X2
|
fun <T2> foo(xf: () -> T2, f: (T2) -> T2) = X2
|
||||||
|
|
||||||
val test: X2 = foo({ 0 }, { it -> it + 1 })
|
val test: X2 = <!OVERLOAD_RESOLUTION_AMBIGUITY!>foo<!>({ 0 }, { it -> it + 1 })
|
||||||
|
|||||||
@@ -1,30 +0,0 @@
|
|||||||
// !LANGUAGE: +EliminateAmbiguitiesOnInheritedSamInterfaces
|
|
||||||
// !CHECK_TYPE
|
|
||||||
// FILE: Fn.java
|
|
||||||
public interface Fn<T, R> {
|
|
||||||
R apply(T t);
|
|
||||||
}
|
|
||||||
|
|
||||||
// FILE: Fn2.java
|
|
||||||
public interface Fn2<T, R> extends Fn<T, R> {}
|
|
||||||
|
|
||||||
// FILE: J.java
|
|
||||||
public interface J {
|
|
||||||
String foo(Fn<String, Object> f, Object o);
|
|
||||||
int foo(Fn<Object, Object> f, String s); // (Any) -> Any <: (String) -> Any <=> String <: Any
|
|
||||||
|
|
||||||
String bas(Fn<Object, Object> f, Object o);
|
|
||||||
int bas(Fn<Object, String> f, String s); // (Any) -> String <: (Any) -> Any <=> String <: Any
|
|
||||||
|
|
||||||
String bar(Fn<String, Object> f);
|
|
||||||
int bar(Fn2<String, Object> f); // Fn2 seems more specific one even function type same
|
|
||||||
}
|
|
||||||
|
|
||||||
// FILE: 1.kt
|
|
||||||
fun test(j: J) {
|
|
||||||
j.<!OVERLOAD_RESOLUTION_AMBIGUITY!>foo<!>({ <!UNRESOLVED_REFERENCE!>it<!> checkType { _<Any>() }; "" }, "") <!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>checkType<!> { _<Int>() }
|
|
||||||
|
|
||||||
j.<!OVERLOAD_RESOLUTION_AMBIGUITY!>bas<!>({ <!UNRESOLVED_REFERENCE!>it<!> checkType { _<Any>() }; "" }, "") <!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>checkType<!> { _<Int>() }
|
|
||||||
|
|
||||||
j.bar { it checkType { _<String>() }; "" } checkType { _<Int>() }
|
|
||||||
}
|
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
// FIR_IDENTICAL
|
||||||
// !LANGUAGE: +EliminateAmbiguitiesOnInheritedSamInterfaces
|
// !LANGUAGE: +EliminateAmbiguitiesOnInheritedSamInterfaces
|
||||||
// !CHECK_TYPE
|
// !CHECK_TYPE
|
||||||
// FILE: Fn.java
|
// FILE: Fn.java
|
||||||
|
|||||||
@@ -1,29 +0,0 @@
|
|||||||
// !LANGUAGE: +EliminateAmbiguitiesOnInheritedSamInterfaces +SamConversionForKotlinFunctions +SamConversionPerArgument
|
|
||||||
// !CHECK_TYPE
|
|
||||||
// FILE: Fn.java
|
|
||||||
public interface Fn<T, R> {
|
|
||||||
R apply(T t);
|
|
||||||
}
|
|
||||||
|
|
||||||
// FILE: Fn2.java
|
|
||||||
public interface Fn2<T, R> extends Fn<T, R> {}
|
|
||||||
|
|
||||||
// FILE: 1.kt
|
|
||||||
interface K {
|
|
||||||
fun foo(f: Fn<String, Any>): String
|
|
||||||
fun foo(f: Fn<Any, Any>): Int
|
|
||||||
|
|
||||||
fun bas(f: Fn<Any, Any>): String
|
|
||||||
fun bas(f: Fn<Any, String>): Int
|
|
||||||
|
|
||||||
fun bar(f: Fn<String, Any>): String
|
|
||||||
fun bar(f: Fn2<String, Any>): Int
|
|
||||||
}
|
|
||||||
|
|
||||||
fun test(k: K) {
|
|
||||||
k.<!OVERLOAD_RESOLUTION_AMBIGUITY!>foo<!> { <!UNRESOLVED_REFERENCE!>it<!> checkType { _<Any>() }; "" } <!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>checkType<!> { _<Int>() }
|
|
||||||
|
|
||||||
k.<!OVERLOAD_RESOLUTION_AMBIGUITY!>bas<!> { <!UNRESOLVED_REFERENCE!>it<!> checkType { _<Any?>() }; "" } <!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>checkType<!> { _<Int>() }
|
|
||||||
|
|
||||||
k.bar { it checkType { _<String>() }; "" } checkType { _<Int>() }
|
|
||||||
}
|
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
// FIR_IDENTICAL
|
||||||
// !LANGUAGE: +EliminateAmbiguitiesOnInheritedSamInterfaces +SamConversionForKotlinFunctions +SamConversionPerArgument
|
// !LANGUAGE: +EliminateAmbiguitiesOnInheritedSamInterfaces +SamConversionForKotlinFunctions +SamConversionPerArgument
|
||||||
// !CHECK_TYPE
|
// !CHECK_TYPE
|
||||||
// FILE: Fn.java
|
// FILE: Fn.java
|
||||||
|
|||||||
-29
@@ -1,29 +0,0 @@
|
|||||||
// !CHECK_TYPE
|
|
||||||
|
|
||||||
// FILE: StaticOverrides.java
|
|
||||||
|
|
||||||
import kotlin.Unit;
|
|
||||||
import kotlin.jvm.functions.Function0;
|
|
||||||
|
|
||||||
public class StaticOverrides {
|
|
||||||
public static class A {
|
|
||||||
public static int foo(Runnable x) { return 0; }
|
|
||||||
public static boolean foo(Function0<Unit> x) { return true; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class B {
|
|
||||||
public static String foo(Runnable x) { return ""; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class C extends A {
|
|
||||||
public static String foo(Runnable x) { return ""; }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// FILE: test.kt
|
|
||||||
|
|
||||||
fun test() {
|
|
||||||
StaticOverrides.A.<!OVERLOAD_RESOLUTION_AMBIGUITY!>foo<!> {} <!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>checkType<!> { _<Boolean>() }
|
|
||||||
StaticOverrides.B.foo {} checkType { _<String>() }
|
|
||||||
StaticOverrides.C.<!OVERLOAD_RESOLUTION_AMBIGUITY!>foo<!> {} <!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>checkType<!> { _<Boolean>() }
|
|
||||||
}
|
|
||||||
+1
@@ -1,3 +1,4 @@
|
|||||||
|
// FIR_IDENTICAL
|
||||||
// !CHECK_TYPE
|
// !CHECK_TYPE
|
||||||
|
|
||||||
// FILE: StaticOverrides.java
|
// FILE: StaticOverrides.java
|
||||||
|
|||||||
-24
@@ -1,24 +0,0 @@
|
|||||||
// !DIAGNOSTICS: -UNUSED_PARAMETER
|
|
||||||
// !CHECK_TYPE
|
|
||||||
|
|
||||||
// FILE: Foo.java
|
|
||||||
import kotlin.Unit;
|
|
||||||
import kotlin.jvm.functions.Function1;
|
|
||||||
|
|
||||||
class Foo {
|
|
||||||
interface FObject<T> {
|
|
||||||
void invoke(T i);
|
|
||||||
}
|
|
||||||
|
|
||||||
public String foo(FObject<Integer> f) { return ""; }
|
|
||||||
public int foo(Function1<Integer, Unit> f) { return 1; }
|
|
||||||
|
|
||||||
public String bar(FObject<Object> f) { return ""; }
|
|
||||||
public int bar(Function1<Integer, Unit> f) { return 1; }
|
|
||||||
}
|
|
||||||
|
|
||||||
// FILE: 1.kt
|
|
||||||
fun test() {
|
|
||||||
Foo().<!OVERLOAD_RESOLUTION_AMBIGUITY!>foo<!> {} <!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>checkType<!> { _<Int>() }
|
|
||||||
Foo().<!OVERLOAD_RESOLUTION_AMBIGUITY!>bar<!> {} <!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>checkType<!> { _<String>() }
|
|
||||||
}
|
|
||||||
+1
@@ -1,3 +1,4 @@
|
|||||||
|
// FIR_IDENTICAL
|
||||||
// !DIAGNOSTICS: -UNUSED_PARAMETER
|
// !DIAGNOSTICS: -UNUSED_PARAMETER
|
||||||
// !CHECK_TYPE
|
// !CHECK_TYPE
|
||||||
|
|
||||||
|
|||||||
-20
@@ -1,20 +0,0 @@
|
|||||||
// !DIAGNOSTICS: -UNUSED_PARAMETER
|
|
||||||
// !CHECK_TYPE
|
|
||||||
|
|
||||||
// FILE: Foo.java
|
|
||||||
import kotlin.Unit;
|
|
||||||
import kotlin.jvm.functions.Function1;
|
|
||||||
|
|
||||||
public class Foo {
|
|
||||||
interface FObject {
|
|
||||||
void invoke(Object i);
|
|
||||||
}
|
|
||||||
|
|
||||||
public String test(FObject f) { return ""; }
|
|
||||||
public int test(Function1<Integer, Unit> f) { return 1; }
|
|
||||||
}
|
|
||||||
|
|
||||||
// FILE: 1.kt
|
|
||||||
fun bar() {
|
|
||||||
Foo().<!OVERLOAD_RESOLUTION_AMBIGUITY!>test<!> {} <!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>checkType<!> { _<String>() }
|
|
||||||
}
|
|
||||||
+1
@@ -1,3 +1,4 @@
|
|||||||
|
// FIR_IDENTICAL
|
||||||
// !DIAGNOSTICS: -UNUSED_PARAMETER
|
// !DIAGNOSTICS: -UNUSED_PARAMETER
|
||||||
// !CHECK_TYPE
|
// !CHECK_TYPE
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user