From 85b867343412c8fd98e82d899b6dcb0e458e8d32 Mon Sep 17 00:00:00 2001 From: Denis Zharkov Date: Tue, 29 Sep 2020 19:16:42 +0300 Subject: [PATCH] FIR: Fix overload resolution with defaults --- .../calls/AbstractConeCallConflictResolver.kt | 4 ++-- .../kotlin/fir/resolve/calls/Candidate.kt | 1 + .../calls/FirArgumentsToParametersMapper.kt | 4 ++++ .../kotlin/fir/resolve/calls/ResolutionStages.kt | 1 + .../secondaryConstructors/redeclarations.fir.kt | 14 +++++++------- 5 files changed, 15 insertions(+), 9 deletions(-) diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/AbstractConeCallConflictResolver.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/AbstractConeCallConflictResolver.kt index ae8d9fc19cd..f3f700736b5 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/AbstractConeCallConflictResolver.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/AbstractConeCallConflictResolver.kt @@ -129,7 +129,7 @@ abstract class AbstractConeCallConflictResolver( //constructor.receiverTypeRef != null, false, constructor.valueParameters.any { it.isVararg }, - constructor.valueParameters.count { it.defaultValue != null }, + call.numDefaults, constructor.isExpect, false // TODO ) @@ -142,7 +142,7 @@ abstract class AbstractConeCallConflictResolver( computeParameterTypes(call, function), function.receiverTypeRef != null, function.valueParameters.any { it.isVararg }, - function.valueParameters.count { it.defaultValue != null }, + call.numDefaults, function.isExpect, false // TODO ) diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/Candidate.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/Candidate.kt index 5a727ad1e5c..3b28df7f6e4 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/Candidate.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/Candidate.kt @@ -103,6 +103,7 @@ class Candidate( var usesSuspendConversion: Boolean = false var argumentMapping: Map? = null + var numDefaults: Int = 0 lateinit var typeArgumentMapping: TypeArgumentMapping val postponedAtoms = mutableListOf() diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/FirArgumentsToParametersMapper.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/FirArgumentsToParametersMapper.kt index 53ae64fe373..5e86ad0cdf1 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/FirArgumentsToParametersMapper.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/FirArgumentsToParametersMapper.kt @@ -44,6 +44,10 @@ data class ArgumentMapping( } return argumentToParameterMapping } + + fun numDefaults(): Int { + return parameterToCallArgumentMap.values.count { it == ResolvedCallArgument.DefaultArgument } + } } private val EmptyArgumentMapping = ArgumentMapping(emptyMap(), emptyList()) diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ResolutionStages.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ResolutionStages.kt index b2241d49c81..c184071e083 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ResolutionStages.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ResolutionStages.kt @@ -171,6 +171,7 @@ internal object MapArguments : ResolutionStage() { val mapping = context.bodyResolveComponents.mapArguments(callInfo.arguments, function, candidate.originScope) candidate.argumentMapping = mapping.toArgumentToParameterMapping() + candidate.numDefaults = mapping.numDefaults() mapping.diagnostics.forEach(sink::reportDiagnostic) sink.yieldIfNeed() diff --git a/compiler/testData/diagnostics/tests/secondaryConstructors/redeclarations.fir.kt b/compiler/testData/diagnostics/tests/secondaryConstructors/redeclarations.fir.kt index bedfc82ef2c..d7e777985f1 100644 --- a/compiler/testData/diagnostics/tests/secondaryConstructors/redeclarations.fir.kt +++ b/compiler/testData/diagnostics/tests/secondaryConstructors/redeclarations.fir.kt @@ -1,9 +1,9 @@ // !DIAGNOSTICS: -UNUSED_PARAMETER class A(x: String = "", y: String = "") { - constructor(x: String, y: String): this(x, y) - constructor(): this("", "") - constructor(): this("", "") + constructor(x: String, y: String): this(x, y) + constructor(): this("", "") + constructor(): this("", "") } class B { @@ -14,9 +14,9 @@ fun B(x: Int) {} class Outer { class A(x: String = "", y: String = "") { - constructor(x: String, y: String): this(x, y) - constructor(): this("", "") - constructor(): this("", "") + constructor(x: String, y: String): this(x, y) + constructor(): this("", "") + constructor(): this("", "") } class B { @@ -24,4 +24,4 @@ class Outer { } fun B(x: Int) {} -} \ No newline at end of file +}