From 97befd0520f3d60e6ca040faef75ab26ab7a41a1 Mon Sep 17 00:00:00 2001 From: "sebastian.sellmair" Date: Wed, 15 Sep 2021 14:21:50 +0200 Subject: [PATCH] [Commonizer] Consider negative type distances 'worse' ^KT-48288 --- .../commonizer/core/ClassOrTypeAliasTypeCommonizer.kt | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/native/commonizer/src/org/jetbrains/kotlin/commonizer/core/ClassOrTypeAliasTypeCommonizer.kt b/native/commonizer/src/org/jetbrains/kotlin/commonizer/core/ClassOrTypeAliasTypeCommonizer.kt index a485402459c..f9563f81a71 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/commonizer/core/ClassOrTypeAliasTypeCommonizer.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/commonizer/core/ClassOrTypeAliasTypeCommonizer.kt @@ -232,8 +232,14 @@ private fun resolveTypeSubstitutionCandidates( if (typeDistances.any { it.isNotReachable }) return@mapCandidateId null TypeSubstitutionCandidate( id = candidateId, - // TODO NOW: Any negative should outperform any positive substitution!, so negative will be chosen here as 'worst' - typeDistance = checkNotNull(typeDistances.maxByOrNull { distance -> distance.absoluteValue }) + typeDistance = checkNotNull(typeDistances.worstOrNull()) ) } } + +private fun List.worstOrNull(): CirTypeDistance? { + if (this.isEmpty()) return null + // Negative Type Distances are considered 'worse' + filter { it.isNegative }.minOrNull()?.let { return it } + return maxOrNull() +} \ No newline at end of file