[FIR] Require equivalent candidates to have the same parameters order

NB: In general, it's unclear what to do in cases
like the following one, even when sometimes
we could, indeed, prefer something:

```
fun foo(a: Int, b: String, c: Boolean)
fun foo(b: String, c: Boolean, a: Int)

foo(c = false, b = "", a = 0)
```

^KT-55933 Fixed
This commit is contained in:
Nikolay Lunyak
2023-07-05 17:31:12 +03:00
committed by Space Team
parent 4f0563dee1
commit fe783d7121
4 changed files with 13 additions and 17 deletions
@@ -6,10 +6,7 @@
package org.jetbrains.kotlin.fir.resolve.calls.jvm
import org.jetbrains.kotlin.fir.containingClassLookupTag
import org.jetbrains.kotlin.fir.declarations.FirCallableDeclaration
import org.jetbrains.kotlin.fir.declarations.FirConstructor
import org.jetbrains.kotlin.fir.declarations.FirSimpleFunction
import org.jetbrains.kotlin.fir.declarations.FirVariable
import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.declarations.utils.isExpect
import org.jetbrains.kotlin.fir.resolve.BodyResolveComponents
import org.jetbrains.kotlin.fir.resolve.calls.AbstractConeCallConflictResolver
@@ -71,12 +68,22 @@ class ConeEquivalentCallConflictResolver(
if (first is FirVariable != second is FirVariable) {
return false
}
if (!firstCandidate.orderOfMappedArguments.contentEquals(secondCandidate.orderOfMappedArguments)) {
return false
}
val firstSignature = createFlatSignature(firstCandidate, first)
val secondSignature = createFlatSignature(secondCandidate, second)
return compareCallsByUsedArguments(firstSignature, secondSignature, discriminateGenerics = false, useOriginalSamTypes = false) &&
compareCallsByUsedArguments(secondSignature, firstSignature, discriminateGenerics = false, useOriginalSamTypes = false)
}
private val Candidate.orderOfMappedArguments: 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()
}
private fun createFlatSignature(call: Candidate, declaration: FirCallableDeclaration): FlatSignature<Candidate> {
return when (declaration) {
is FirSimpleFunction -> createFlatSignature(call, declaration)
@@ -1,12 +0,0 @@
// DIAGNOSTICS: -DEBUG_INFO_MISSING_UNRESOLVED
// ISSUE: KT-55933
fun swapVararg(vararg namedVararg: String, named: Int){}
fun swapVararg(named: Int, vararg namedVararg: String){}
val x = swapVararg(named = 42, namedVararg = arrayOf("1", "2"),)
fun swap(string: String, int: Int){}
fun swap(int: Int, string: String){}
val y = swap(int = 42, string = "2")
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// DIAGNOSTICS: -DEBUG_INFO_MISSING_UNRESOLVED
// ISSUE: KT-55933
@@ -8,6 +8,6 @@ fun overloadedFun5(vararg ss: String) = X1
fun overloadedFun5(s: String, vararg ss: String) = X2
val test1 = overloadedFun5("")
val test2 = overloadedFun5("", "")
val test2 = <!OVERLOAD_RESOLUTION_AMBIGUITY!>overloadedFun5<!>("", "")
val test3: X2 = overloadedFun5(s = "", ss = <!ARGUMENT_TYPE_MISMATCH, ASSIGNING_SINGLE_ELEMENT_TO_VARARG_IN_NAMED_FORM_FUNCTION_ERROR!>""<!>)
val test4: X1 = overloadedFun5(ss = <!ARGUMENT_TYPE_MISMATCH, ASSIGNING_SINGLE_ELEMENT_TO_VARARG_IN_NAMED_FORM_FUNCTION_ERROR!>""<!>)