[Commonizer] Consider negative type distances 'worse'

^KT-48288
This commit is contained in:
sebastian.sellmair
2021-09-15 14:21:50 +02:00
committed by Space
parent 24680144da
commit 97befd0520
@@ -232,8 +232,14 @@ private fun resolveTypeSubstitutionCandidates(
if (typeDistances.any { it.isNotReachable }) return@mapCandidateId null if (typeDistances.any { it.isNotReachable }) return@mapCandidateId null
TypeSubstitutionCandidate( TypeSubstitutionCandidate(
id = candidateId, id = candidateId,
// TODO NOW: Any negative should outperform any positive substitution!, so negative will be chosen here as 'worst' typeDistance = checkNotNull(typeDistances.worstOrNull())
typeDistance = checkNotNull(typeDistances.maxByOrNull { distance -> distance.absoluteValue })
) )
} }
} }
private fun List<CirTypeDistance>.worstOrNull(): CirTypeDistance? {
if (this.isEmpty()) return null
// Negative Type Distances are considered 'worse'
filter { it.isNegative }.minOrNull()?.let { return it }
return maxOrNull()
}