From d4c6e55e0ab6e17ecb7f706193dae5aba57fa395 Mon Sep 17 00:00:00 2001 From: Nikolay Lunyak Date: Thu, 6 Jul 2023 09:53:52 +0300 Subject: [PATCH] [FIR] Require equivalent candidates to have the same parameters count --- .../jvm/ConeEquivalentCallConflictResolver.kt | 20 ++++++++++++++++--- .../overloadConflicts/varargsMixed.fir.kt | 2 +- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/compiler/fir/java/src/org/jetbrains/kotlin/fir/resolve/calls/jvm/ConeEquivalentCallConflictResolver.kt b/compiler/fir/java/src/org/jetbrains/kotlin/fir/resolve/calls/jvm/ConeEquivalentCallConflictResolver.kt index b6f53e60ce9..f9be90c5740 100644 --- a/compiler/fir/java/src/org/jetbrains/kotlin/fir/resolve/calls/jvm/ConeEquivalentCallConflictResolver.kt +++ b/compiler/fir/java/src/org/jetbrains/kotlin/fir/resolve/calls/jvm/ConeEquivalentCallConflictResolver.kt @@ -68,7 +68,7 @@ class ConeEquivalentCallConflictResolver( if (first is FirVariable != second is FirVariable) { return false } - if (!firstCandidate.orderOfMappedArguments.contentEquals(secondCandidate.orderOfMappedArguments)) { + if (!firstCandidate.mappedArgumentsOrderRepresentation.contentEquals(secondCandidate.mappedArgumentsOrderRepresentation)) { return false } val firstSignature = createFlatSignature(firstCandidate, first) @@ -77,11 +77,25 @@ class ConeEquivalentCallConflictResolver( compareCallsByUsedArguments(secondSignature, firstSignature, discriminateGenerics = false, useOriginalSamTypes = false) } - private val Candidate.orderOfMappedArguments: IntArray? + /** + * If the candidate is a function, then the arguments + * order representation is an array containing the + * parameters count and the indices of the parameters + * that the call arguments correspond to in the order + * the call arguments happen to be. + * + * Otherwise, null. + */ + private val Candidate.mappedArgumentsOrderRepresentation: IntArray? get() { val function = symbol.fir as? FirFunction ?: return null val parametersToIndices = function.valueParameters.mapIndexed { index, it -> it to index }.toMap() - return argumentMapping?.map { parametersToIndices[it.value] ?: error("Unmapped argument in arguments mapping") }?.toIntArray() + val mapping = argumentMapping ?: return null + val result = IntArray(mapping.size + 1) { function.valueParameters.size } + for ((index, parameter) in mapping.values.withIndex()) { + result[index + 1] = parametersToIndices[parameter] ?: error("Unmapped argument in arguments mapping") + } + return result } private fun createFlatSignature(call: Candidate, declaration: FirCallableDeclaration): FlatSignature { diff --git a/compiler/testData/diagnostics/tests/resolve/overloadConflicts/varargsMixed.fir.kt b/compiler/testData/diagnostics/tests/resolve/overloadConflicts/varargsMixed.fir.kt index bbd1b14884b..761e283d389 100644 --- a/compiler/testData/diagnostics/tests/resolve/overloadConflicts/varargsMixed.fir.kt +++ b/compiler/testData/diagnostics/tests/resolve/overloadConflicts/varargsMixed.fir.kt @@ -7,7 +7,7 @@ object X2 fun overloadedFun5(vararg ss: String) = X1 fun overloadedFun5(s: String, vararg ss: String) = X2 -val test1 = overloadedFun5("") +val test1 = overloadedFun5("") val test2 = overloadedFun5("", "") val test3: X2 = overloadedFun5(s = "", ss = "") val test4: X1 = overloadedFun5(ss = "")