From a0ed03a121616ed7ed69d0e24009082bdd525370 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Mon, 7 Oct 2019 14:17:39 +0300 Subject: [PATCH] FIR: apply overload conflict resolver even for variable (due to receivers) --- .../jetbrains/kotlin/fir/FirCallResolver.kt | 15 ++++++++++----- ...ver.kt => ConeOverloadConflictResolver.kt} | 19 +++++++++++++++++-- .../testData/resolve/stdlib/problems.txt | 4 ++-- 3 files changed, 29 insertions(+), 9 deletions(-) rename compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/{OverloadConflictResolver.kt => ConeOverloadConflictResolver.kt} (92%) diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/FirCallResolver.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/FirCallResolver.kt index 772fd66164c..ed7272cccae 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/FirCallResolver.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/FirCallResolver.kt @@ -166,15 +166,20 @@ class FirCallResolver( ) val result = towerResolver.runResolver(consumer, implicitReceiverStack.receiversAsReversed()) - val candidates = result.bestCandidates() + val bestCandidates = result.bestCandidates() + val reducedCandidates = if (result.currentApplicability < CandidateApplicability.SYNTHETIC_RESOLVED) { + bestCandidates.toSet() + } else { + conflictResolver.chooseMaximallySpecificCandidates(bestCandidates, discriminateGenerics = false) + } val nameReference = createResolvedNamedReference( callee, - candidates, + reducedCandidates, result.currentApplicability ) if (qualifiedAccess.explicitReceiver == null && - (candidates.size <= 1 && result.currentApplicability < CandidateApplicability.SYNTHETIC_RESOLVED) + (reducedCandidates.size <= 1 && result.currentApplicability < CandidateApplicability.SYNTHETIC_RESOLVED) ) { qualifiedResolver.tryResolveAsQualifier()?.let { return it } } @@ -196,8 +201,8 @@ class FirCallResolver( @Suppress("UNCHECKED_CAST") var resultExpression = qualifiedAccess.transformCalleeReference(StoreNameReference, nameReference) as T - if (candidates.size == 1) { - val candidate = candidates.single() + if (reducedCandidates.size == 1) { + val candidate = reducedCandidates.single() resultExpression = resultExpression.transformDispatchReceiver(StoreReceiver, candidate.dispatchReceiverExpression()) as T resultExpression = resultExpression.transformExtensionReceiver(StoreReceiver, candidate.extensionReceiverExpression()) as T } 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/ConeOverloadConflictResolver.kt similarity index 92% rename from compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/OverloadConflictResolver.kt rename to compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ConeOverloadConflictResolver.kt index ef91f6a147a..75ec5007223 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/ConeOverloadConflictResolver.kt @@ -5,10 +5,11 @@ package org.jetbrains.kotlin.fir.resolve.calls -import org.jetbrains.kotlin.fir.FirSession import org.jetbrains.kotlin.fir.declarations.FirConstructor import org.jetbrains.kotlin.fir.declarations.FirNamedFunction +import org.jetbrains.kotlin.fir.declarations.FirProperty import org.jetbrains.kotlin.fir.declarations.FirValueParameter +import org.jetbrains.kotlin.fir.expressions.FirVariable import org.jetbrains.kotlin.fir.resolve.substitution.substitutorByMap import org.jetbrains.kotlin.fir.symbols.impl.FirTypeParameterSymbol import org.jetbrains.kotlin.fir.types.ConeKotlinType @@ -55,6 +56,7 @@ class ConeOverloadConflictResolver( return when (val declaration = call.symbol.fir) { is FirNamedFunction -> createFlatSignature(call, declaration) is FirConstructor -> createFlatSignature(call, declaration) + is FirVariable<*> -> createFlatSignature(call, declaration) else -> error("Not supported: $declaration") } } @@ -84,7 +86,7 @@ class ConeOverloadConflictResolver( call, function.typeParameters.map { it.symbol }, listOfNotNull(function.receiverTypeRef?.coneTypeUnsafe()) + - call.argumentMapping!!.map { it.value.argumentType() }, + call.argumentMapping?.map { it.value.argumentType() }.orEmpty(), function.receiverTypeRef != null, function.valueParameters.any { it.isVararg }, function.valueParameters.count { it.defaultValue != null }, @@ -93,6 +95,19 @@ class ConeOverloadConflictResolver( ) } + private fun createFlatSignature(call: Candidate, variable: FirVariable<*>): FlatSignature { + return FlatSignature( + call, + (variable as? FirProperty)?.typeParameters?.map { it.symbol }.orEmpty(), + listOfNotNull(variable.receiverTypeRef?.coneTypeUnsafe()), + variable.receiverTypeRef != null, + false, + 0, + (variable as? FirProperty)?.isExpect == true, + false // TODO + ) + } + private fun createEmptyConstraintSystem(): SimpleConstraintSystem { return ConeSimpleConstraintSystemImpl(inferenceComponents.createConstraintSystem()) } diff --git a/compiler/fir/resolve/testData/resolve/stdlib/problems.txt b/compiler/fir/resolve/testData/resolve/stdlib/problems.txt index d6dd51cb6af..112c753373a 100644 --- a/compiler/fir/resolve/testData/resolve/stdlib/problems.txt +++ b/compiler/fir/resolve/testData/resolve/stdlib/problems.txt @@ -34,8 +34,8 @@ FILE: problems.kt public get(): R|kotlin/String| { ^ String(987) } - public final val t: = String().# - public get(): + public final val t: R|kotlin/String| = String().R|/bar| + public get(): R|kotlin/String| public final val p: R|kotlin/Pair| = R|kotlin/Pair.Pair|(Int(0), String()) public get(): R|kotlin/Pair| public final class Base : R|kotlin/Any| {